Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure children are removed when stopped. Fixes #1457 #1559

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions packages/core/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -905,15 +905,23 @@ export class Interpreter<
return;
}

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

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

if (isFunction(child.stop)) {
child.stop();
}
}
private removeChild(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];
}
public spawn(entity: Spawnable, name: string, options?: SpawnOptions): Actor {
if (isPromiseLike(entity)) {
return this.spawnPromise(Promise.resolve(entity), name);
Expand Down Expand Up @@ -977,6 +985,12 @@ export class Interpreter<
})
.start();

const originalStop = actor.stop.bind(actor);
actor.stop = () => {
this.removeChild(childService.id);
return originalStop();
};

return actor;
}
private spawnPromise<T>(promise: Promise<T>, id: string): Actor<T, never> {
Expand Down Expand Up @@ -1045,6 +1059,7 @@ export class Interpreter<
},
stop: () => {
canceled = true;
this.removeChild(id);
},
toJSON() {
return { id };
Expand Down Expand Up @@ -1135,7 +1150,10 @@ export class Interpreter<
subscribe: (next, handleError, complete) => {
return source.subscribe(next, handleError, complete);
},
stop: () => subscription.unsubscribe(),
stop: () => {
subscription.unsubscribe();
this.removeChild(id);
},
toJSON() {
return { id };
}
Expand Down
55 changes: 55 additions & 0 deletions packages/core/test/interpreter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1937,5 +1937,60 @@ Event: {\\"type\\":\\"SOME_EVENT\\"}"
})
.start();
});

it('stopped spawned actors should be cleaned up in parent', (done) => {
const childMachine = Machine({
initial: 'idle',
states: {
idle: {}
}
});

const parentMachine = createMachine<any>({
id: 'form',
initial: 'present',
context: {},
entry: assign({
machineRef: () => spawn(childMachine, 'machineChild'),
promiseRef: () =>
spawn(
new Promise(() => {
// ...
}),
'promiseChild'
),
observableRef: () => spawn(interval(1000), 'observableChild')
}),
states: {
present: {
on: { NEXT: 'gone' }
},
gone: {
type: 'final'
}
}
});

const service = interpret(parentMachine)
.onDone(() => {
expect(service.children.get('machineChild')).toBeUndefined();
expect(service.children.get('promiseChild')).toBeUndefined();
expect(service.children.get('observableChild')).toBeUndefined();
done();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this really should be allowed. It's basically bypassing the machinery of state machine, allowing for arbitrary, imperative, manipulation of its internal state. This also doesn't quite deliver proper cleanup:

typeof service.state.context.machineRef === 'object'
typeof service.state.context.promiseRef === 'object'
typeof service.state.context. observableRef === 'object'

This is the common case - spawned actors are usually assigned to the context. Clearing context values would definitely be too magical (and not trivial - with the current APIs it would require a full crawl of the current context).

})
.start();

service.subscribe((state) => {
if (state.matches('present')) {
expect(state.children).toHaveProperty('machineChild');
expect(state.children).toHaveProperty('promiseChild');
expect(state.children).toHaveProperty('observableChild');
state.children.machineChild.stop?.();
state.children.promiseChild.stop?.();
state.children.observableChild.stop?.();
service.send('NEXT');
}
});
});
});
});