@@ -4,37 +4,37 @@ import pm from "picomatch";
44// Import Internal Dependencies
55import { Actor } from "./Actor.ts" ;
66
7- export type ActorTreeNode = {
8- actor : Actor ;
9- parent ?: Actor ;
7+ export type ActorTreeNode < TContext = Record < string , unknown > > = {
8+ actor : Actor < TContext > ;
9+ parent ?: Actor < TContext > ;
1010} ;
1111
12- export interface ActorTreeOptions {
13- addCallback ?: ( actor : Actor ) => void ;
14- removeCallback ?: ( actor : Actor ) => void ;
12+ export interface ActorTreeOptions < TContext = Record < string , unknown > > {
13+ addCallback ?: ( actor : Actor < TContext > ) => void ;
14+ removeCallback ?: ( actor : Actor < TContext > ) => void ;
1515}
1616
17- export class ActorTree {
18- #addCallback?: ( actor : Actor ) => void ;
19- #removeCallback?: ( actor : Actor ) => void ;
17+ export class ActorTree < TContext = Record < string , unknown > > {
18+ #addCallback?: ( actor : Actor < TContext > ) => void ;
19+ #removeCallback?: ( actor : Actor < TContext > ) => void ;
2020
21- children : Actor [ ] = [ ] ;
21+ children : Actor < TContext > [ ] = [ ] ;
2222
2323 constructor (
24- options : ActorTreeOptions = { }
24+ options : ActorTreeOptions < TContext > = { }
2525 ) {
2626 this . #addCallback = options . addCallback ;
2727 this . #removeCallback = options . removeCallback ;
2828 }
2929
3030 add (
31- actor : Actor
31+ actor : Actor < TContext >
3232 ) : void {
3333 this . children . push ( actor ) ;
3434 this . #addCallback?.( actor ) ;
3535 }
3636
37- remove ( actor : Actor ) : void {
37+ remove ( actor : Actor < TContext > ) : void {
3838 const index = this . children . indexOf ( actor ) ;
3939 if ( index !== - 1 ) {
4040 this . children . splice ( index , 1 ) ;
@@ -44,7 +44,7 @@ export class ActorTree {
4444
4545 * getActors (
4646 pattern : string
47- ) : IterableIterator < Actor > {
47+ ) : IterableIterator < Actor < TContext > > {
4848 if ( pattern . includes ( "/" ) ) {
4949 yield * this . #getActorsByPatternPath( pattern ) ;
5050
@@ -62,7 +62,7 @@ export class ActorTree {
6262
6363 * #getActorsByPatternPath(
6464 pattern : string
65- ) : IterableIterator < Actor > {
65+ ) : IterableIterator < Actor < TContext > > {
6666 const parts = pattern . split ( "/" ) . filter ( ( part ) => part !== "" ) ;
6767
6868 for ( const rootActor of this . children ) {
@@ -73,10 +73,10 @@ export class ActorTree {
7373 }
7474
7575 * #matchActorPath(
76- actor : Actor ,
76+ actor : Actor < TContext > ,
7777 patternParts : string [ ] ,
7878 patternIndex : number
79- ) : IterableIterator < Actor > {
79+ ) : IterableIterator < Actor < TContext > > {
8080 if ( patternIndex >= patternParts . length ) {
8181 return ;
8282 }
@@ -138,7 +138,7 @@ export class ActorTree {
138138 */
139139 getActor (
140140 name : string
141- ) : Actor | null {
141+ ) : Actor < TContext > | null {
142142 if ( name . includes ( "/" ) ) {
143143 return this . #getActorByPath( name ) ;
144144 }
@@ -154,14 +154,14 @@ export class ActorTree {
154154
155155 #getActorByPath(
156156 path : string
157- ) : Actor | null {
157+ ) : Actor < TContext > | null {
158158 const parts = path . split ( "/" ) . filter ( ( part ) => part !== "" ) ;
159159 const parentNode = this . getActor ( parts [ 0 ] ) ;
160160 if ( ! parentNode ) {
161161 return null ;
162162 }
163163
164- let currentNode : Actor | null = parentNode ;
164+ let currentNode : Actor < TContext > | null = parentNode ;
165165 for ( let i = 1 ; i < parts . length ; i ++ ) {
166166 if ( ! currentNode ) {
167167 break ;
@@ -175,22 +175,22 @@ export class ActorTree {
175175 return currentNode ;
176176 }
177177
178- * getRootActors ( ) : IterableIterator < Actor > {
178+ * getRootActors ( ) : IterableIterator < Actor < TContext > > {
179179 for ( const rootActor of this . children ) {
180180 if ( ! rootActor . pendingForDestruction ) {
181181 yield rootActor ;
182182 }
183183 }
184184 }
185185
186- * getAllActors ( ) : IterableIterator < Actor > {
186+ * getAllActors ( ) : IterableIterator < Actor < TContext > > {
187187 for ( const { actor } of this . walk ( ) ) {
188188 yield actor ;
189189 }
190190 }
191191
192192 destroyActor (
193- actor : Actor
193+ actor : Actor < TContext >
194194 ) {
195195 if ( ! actor . pendingForDestruction ) {
196196 actor . markDestructionPending ( ) ;
@@ -204,31 +204,31 @@ export class ActorTree {
204204 }
205205
206206 * #walkDepthFirstGenerator(
207- node : Actor ,
208- parentNode ?: Actor
209- ) : IterableIterator < ActorTreeNode > {
207+ node : Actor < TContext > ,
208+ parentNode ?: Actor < TContext >
209+ ) : IterableIterator < ActorTreeNode < TContext > > {
210210 yield { actor : node , parent : parentNode } ;
211211
212212 for ( const child of node . children ) {
213213 yield * this . #walkDepthFirstGenerator( child , node ) ;
214214 }
215215 }
216216
217- * walk ( ) : IterableIterator < ActorTreeNode > {
217+ * walk ( ) : IterableIterator < ActorTreeNode < TContext > > {
218218 for ( const child of this . children ) {
219219 yield * this . #walkDepthFirstGenerator( child , undefined ) ;
220220 }
221221 }
222222
223223 * walkFromNode (
224- rootNode : Actor
225- ) : IterableIterator < ActorTreeNode > {
224+ rootNode : Actor < TContext >
225+ ) : IterableIterator < ActorTreeNode < TContext > > {
226226 for ( const child of rootNode . children ) {
227227 yield * this . #walkDepthFirstGenerator( child , rootNode ) ;
228228 }
229229 }
230230
231- * [ Symbol . iterator ] ( ) : IterableIterator < Actor > {
231+ * [ Symbol . iterator ] ( ) : IterableIterator < Actor < TContext > > {
232232 for ( const actor of this . children ) {
233233 if ( ! actor . pendingForDestruction ) {
234234 yield actor ;
0 commit comments