Scenario: the child needs the parentRef in its own context to send some stuff. And the parent has the childRef in its own context, cause I spawned via 'spawn' and not 'spawnChild' (maybe this is not recommended, but I wonder why there is a 'spawn' after all?)
Is it true, that you have to do two things, when spawning child machines, to prevent memory leaks:
- when stopping the child, setting the parentRef to null
- when saving the childRef in context of parent machine, setting the childRef after stopping machine to null, and not just using spawnChild
It all sounds like a lot of boilerplate code - am I missing something?
// in child machine, while spawning the child, saving the parentRef in context
createMachine({
id: "childMachine",
context: ({ input: { parentRef } }) => ({
parentRef
})
// in child machine before stopping the child setting the parentRef to null to prevent memory leaks
createMachine({
id: "childMachine",
on: {
STOP: {
actions: [
assign({
parentRef: null,
})]
}
})
})
// in parent machine setting the childRef to null after stopping machine to prevent memory leaks
createMachine({
id: "parentMachine",
on: {
STOP_CHLD: {
actions: [
stopChild("childMachine")
assign({
childref: null,
})]
}
})