Skip to content

Commit 22b3ce2

Browse files
committed
Add .stop() method
1 parent 79c77c4 commit 22b3ce2

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/index.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,17 @@ export interface MachineInstance extends Iterator<null | string | Record<string,
104104
current: null | string | Record<string, string>;
105105
results: null | Promise<unknown>;
106106
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>> &
110108
PromiseLike<any> & {
111109
actions: Array<EntryAction | ExitAction>;
112110
};
111+
stop(): void;
112+
}
113+
114+
declare global {
115+
interface SymbolConstructor {
116+
readonly hello: symbol;
117+
}
113118
}
114119

115120
class Handlers {
@@ -270,21 +275,24 @@ class InternalInstance {
270275
handleEvent(event: Event) {
271276
this.receive(event.type);
272277
}
273-
274-
consume(stateGenerator: (() => StateDefinition) | (() => Generator<Yielded, StateDefinition, never>)) {
278+
279+
cleanup() {
275280
for (const [event, target] of this.globalHandlers.eventsToListenTo) {
276281
// Not sure if we still need this if we have abort signals, and for what versions of Node/browsers?
277282
target.removeEventListener(event, this);
278283
}
279284
this.eventAborter.abort();
280-
281-
const initialReturn = stateGenerator();
282-
283-
this.willEnter();
284285

285286
this.globalHandlers.reset();
287+
}
288+
289+
consume(stateGenerator: (() => StateDefinition) | (() => Generator<Yielded, StateDefinition, never>)) {
290+
this.cleanup();
286291
this.eventAborter = new AbortController();
287292

293+
this.willEnter();
294+
295+
const initialReturn = stateGenerator();
288296
if (initialReturn[Symbol.iterator]) {
289297
const iterator = initialReturn[Symbol.iterator]();
290298
while (true) {
@@ -444,6 +452,9 @@ export function start(
444452
done: false,
445453
};
446454
},
455+
stop() {
456+
instance.cleanup();
457+
},
447458
get done() {
448459
return instance.child !== null;
449460
},

0 commit comments

Comments
 (0)