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

Bug: xstate v5/React useActor/useActorRef not restarting parent machine and spawned children after restore #4148

Closed
murrowblue22 opened this issue Jul 14, 2023 · 3 comments

Comments

@murrowblue22
Copy link

Description

const [state, send, pam_service] = useActor(parentAppMachine, {
state: restoredState
})

When i use the above code to restore a machine after using the below code, to persist the machine state to localStorage. The restored machine does not got to a running state. Meaning if i send an event to the machine nothing happens no error nothing, also spawned machine actors that were running on the parent at the time the parent machine state was persisted are not restarted.

const modelMachineActorRef = useActorRef(machine)
const initialPersistedState = modelMachineActorRef.getPersistedState!()
localStorage.setItem('pam_service_state', JSON.stringify(initialPersistedState))

The documentation if i am reading it correctly says xstate v5 would provide this functionality
In XState v5 beta, actors are now deeply (recursively) persisted. Invoked/spawned actors will be persisted, as well as actors invoked/spawned from those actors, and so on.

In the following example, the state of the mainActor will be persisted, as well as the state of the invoked someCounter actor. When the restoredActor is started, it will start at the persisted state of mainActor, which includes the persisted state of someCounter:

Expected result

i expected the parent machine and the existing spawned child actor machines to be restored to their prior state and for them to be running

Actual result

The parent machine was restored but it only showed an id value for child spawned actors

Reproduction

Additional context

@davidkpiano
Copy link
Member

Can you please share a reproduction? Will reopen once that's provided.

@tsxoxo
Copy link
Contributor

tsxoxo commented Sep 1, 2023

This is tangentially related at best (it's not React, the hydration happens elsewhere) but it sounds like a similar result. When I try to rehydrate from localStorage, I get 'dead actors' that don't send events back to the parent.

I've noticed that the ActorRef objects that are retrieved from storage have listener: Set(0), whereas freshly added items have 1.

Maybe this can be a piece of the thread to figure this one out.

Live code -- this is the official Todo-MVC app for Xstate + Vue, just with hydration added in todos.machine.ts:

https://codesandbox.io/p/sandbox/need-8598mq?file=%2Fsrc%2Ftodos.machine.ts%3A5%2C48

I'd love to know some ways to debug something like this -- is there a way to show all spawned actors that are 'running'?

Hope I'm not hijacking this thread too much. I'm a bit too pressed for time to open a new one right now :).

@gregoralbrecht
Copy link

@tsxoxo I've run into the very same problem and after looking through various docs (one, two), I found a note here:

Persisting spawned actors isn't yet supported in XState.

The new beta docs for stately.ai say this, however:

Persisting & restoring state from machine actors is deep; all invoked & spawned actors will be persisted & restored recursively.

Maybe this is added in v5? I must be looking in all the wrong places, but I can't find actor.getPersistedState() anywhere when using xstate@4.38.2 and @xstate/react@3.2.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants