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

[v5] Persist & restore spawned actors #3921

Merged
merged 32 commits into from
Oct 16, 2023
Merged

[v5] Persist & restore spawned actors #3921

merged 32 commits into from
Oct 16, 2023

Conversation

davidkpiano
Copy link
Member

No description provided.

@changeset-bot
Copy link

changeset-bot bot commented Mar 25, 2023

🦋 Changeset detected

Latest commit: 67a87ad

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
xstate Major

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

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 25, 2023

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:

Sandbox Source
XState Example Template Configuration
XState React Template Configuration

@ghost
Copy link

ghost commented Mar 25, 2023

👇 Click on the image for a new way to code review

Review these changes using an interactive CodeSee Map

Legend

CodeSee Map legend

@davidkpiano davidkpiano marked this pull request as ready for review March 30, 2023 22:34
packages/core/src/State.ts Outdated Show resolved Hide resolved
@davidkpiano
Copy link
Member Author

@Andarist This is ready for final review

Comment on lines 453 to 460
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;
});
Copy link
Member

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()

return value;
});

restoredState.context = context;
Copy link
Member

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,
Copy link
Member Author

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 😂

Copy link
Member

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

@davidkpiano davidkpiano merged commit 0ca1b86 into next Oct 16, 2023
@davidkpiano davidkpiano deleted the v5/persistent-spawn branch October 16, 2023 11:17
audionerd pushed a commit to audionerd/xstate that referenced this pull request Oct 19, 2023
* 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]>
audionerd pushed a commit to audionerd/xstate that referenced this pull request Oct 19, 2023
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants