-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[v5] Persist & restore spawned actors #3921
Conversation
🦋 Changeset detectedLatest commit: 67a87ad The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 67a87ad:
|
@Andarist This is ready for final review |
packages/core/src/StateMachine.ts
Outdated
const context = JSON.parse(JSON.stringify(state.context), (_, value) => { | ||
// if value is an object with $$type property, it is a reference to an actor | ||
if (typeof value === 'object' && value && value.$$type === 'ActorRef') { | ||
return children[value.id]; | ||
} | ||
|
||
return value; | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this "hydration" has to be system-wide to cover for cases like this:
const child = createMachine({
context: (input) => ({ parent: input.parent })
})
const parent = createMachine({
invoke: {
src: child,
input: ({ self }) => ({ parent: self })
}
})
interpret(parent).getPersistedState()
packages/core/src/StateMachine.ts
Outdated
return value; | ||
}); | ||
|
||
restoredState.context = context; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit-level but I think that it would just be better to create this rehydrated context first and create State
right away with it instead of re-assigning it after the fact
? (contextPart.slice() as typeof contextPart) | ||
: { ...contextPart }; | ||
copy[key] = { | ||
xstate$$type: $$ACTOR_TYPE, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to standardize these kinds of keys 😂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i hope we don't have to add any more special values but with this in place, a key like xstate$$type
is already pretty unique and its values can be extended if any need arises
* Restore spawned actors * Update snapshot tests * Fix behavior tests * Add changeset * Use .ts * Remove JSON.stringify * Update types * Typecheck * Fix tests * tweak things * throw on attempt to persist an inline child actor * Update .changeset/yellow-cups-obey.md --------- Co-authored-by: Mateusz Burzyński <[email protected]>
* Restore spawned actors * Update snapshot tests * Fix behavior tests * Add changeset * Use .ts * Remove JSON.stringify * Update types * Typecheck * Fix tests * tweak things * throw on attempt to persist an inline child actor * Update .changeset/yellow-cups-obey.md --------- Co-authored-by: Mateusz Burzyński <[email protected]>
No description provided.