@@ -104,12 +104,17 @@ export interface MachineInstance extends Iterator<null | string | Record<string,
104
104
current : null | string | Record < string , string > ;
105
105
results : null | Promise < unknown > ;
106
106
done : boolean ;
107
- next (
108
- ...args : [ string | symbol ]
109
- ) : IteratorResult < null | string | Record < string , string > > &
107
+ next ( arg : string | symbol ) : IteratorResult < null | string | Record < string , string > > &
110
108
PromiseLike < any > & {
111
109
actions : Array < EntryAction | ExitAction > ;
112
110
} ;
111
+ stop ( ) : void ;
112
+ }
113
+
114
+ declare global {
115
+ interface SymbolConstructor {
116
+ readonly hello : symbol ;
117
+ }
113
118
}
114
119
115
120
class Handlers {
@@ -270,21 +275,24 @@ class InternalInstance {
270
275
handleEvent ( event : Event ) {
271
276
this . receive ( event . type ) ;
272
277
}
273
-
274
- consume ( stateGenerator : ( ( ) => StateDefinition ) | ( ( ) => Generator < Yielded , StateDefinition , never > ) ) {
278
+
279
+ cleanup ( ) {
275
280
for ( const [ event , target ] of this . globalHandlers . eventsToListenTo ) {
276
281
// Not sure if we still need this if we have abort signals, and for what versions of Node/browsers?
277
282
target . removeEventListener ( event , this ) ;
278
283
}
279
284
this . eventAborter . abort ( ) ;
280
-
281
- const initialReturn = stateGenerator ( ) ;
282
-
283
- this . willEnter ( ) ;
284
285
285
286
this . globalHandlers . reset ( ) ;
287
+ }
288
+
289
+ consume ( stateGenerator : ( ( ) => StateDefinition ) | ( ( ) => Generator < Yielded , StateDefinition , never > ) ) {
290
+ this . cleanup ( ) ;
286
291
this . eventAborter = new AbortController ( ) ;
287
292
293
+ this . willEnter ( ) ;
294
+
295
+ const initialReturn = stateGenerator ( ) ;
288
296
if ( initialReturn [ Symbol . iterator ] ) {
289
297
const iterator = initialReturn [ Symbol . iterator ] ( ) ;
290
298
while ( true ) {
@@ -444,6 +452,9 @@ export function start(
444
452
done : false ,
445
453
} ;
446
454
} ,
455
+ stop ( ) {
456
+ instance . cleanup ( ) ;
457
+ } ,
447
458
get done ( ) {
448
459
return instance . child !== null ;
449
460
} ,
0 commit comments