Skip to content

Commit

Permalink
Cleanup internal structures upon receiving termination events from sp…
Browse files Browse the repository at this point in the history
…awned actors
  • Loading branch information
Andarist committed Mar 14, 2020
1 parent 2471803 commit 1a129f0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-bats-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': patch
---

Cleanup internal structures upon receiving termination events from spawned actors.
29 changes: 19 additions & 10 deletions packages/core/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -912,16 +912,20 @@ export class Interpreter<

return undefined;
}
private removeChild(childId: string): void {
this.children.delete(childId);
this.forwardTo.delete(childId);

delete this.state.children[childId];
}

private stopChild(childId: string): void {
const child = this.children.get(childId);
if (!child) {
return;
}

this.children.delete(childId);
this.forwardTo.delete(childId);

delete this.state.children[childId];
this.removeChild(childId);

if (isFunction(child.stop)) {
child.stop();
Expand Down Expand Up @@ -972,12 +976,6 @@ export class Interpreter<
});
}

childService
.onDone(doneEvent => {
this.send(toSCXMLEvent(doneEvent as any, { origin: childService.id }));
})
.start();

const actor = childService;

this.children.set(childService.id, actor as Actor<
Expand All @@ -989,6 +987,13 @@ export class Interpreter<
this.forwardTo.add(childService.id);
}

childService
.onDone(doneEvent => {
this.removeChild(childService.id);
this.send(toSCXMLEvent(doneEvent as any, { origin: childService.id }));
})
.start();

return actor;
}
private spawnPromise<T>(promise: Promise<T>, id: string): Actor<T, never> {
Expand All @@ -997,13 +1002,15 @@ export class Interpreter<
promise.then(
response => {
if (!canceled) {
this.removeChild(id);
this.send(
toSCXMLEvent(doneInvoke(id, response) as any, { origin: id })
);
}
},
errorData => {
if (!canceled) {
this.removeChild(id);
const errorEvent = error(id, errorData);
try {
// Send "error.platform.id" to this (parent).
Expand Down Expand Up @@ -1130,9 +1137,11 @@ export class Interpreter<
this.send(toSCXMLEvent(value, { origin: id }));
},
err => {
this.removeChild(id);
this.send(toSCXMLEvent(error(id, err) as any, { origin: id }));
},
() => {
this.removeChild(id);
this.send(toSCXMLEvent(doneInvoke(id) as any, { origin: id }));
}
);
Expand Down

0 comments on commit 1a129f0

Please sign in to comment.