Skip to content

Commit

Permalink
Fixed an issue with rehydrated actors not registering themselves in t…
Browse files Browse the repository at this point in the history
…he system (#4403)
  • Loading branch information
Andarist authored Oct 27, 2023
1 parent a01169e commit 3f84bba
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/three-grapes-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': patch
---

Fixed an issue with rehydrated actors not registering themselves in the system.
3 changes: 2 additions & 1 deletion packages/core/src/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ export function getPersistedState<
}
childrenJson[id as keyof typeof childrenJson] = {
state: child.getPersistedState(),
src: child.src
src: child.src,
systemId: child._systemId
};
}

Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/StateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ export class StateMachine<
const children: Record<string, AnyActorRef> = {};
const snapshotChildren: Record<
string,
{ src: string; state: Snapshot<unknown> }
{ src: string; state: Snapshot<unknown>; systemId?: string }
> = (snapshot as any).children;

Object.keys(snapshotChildren).forEach((actorId) => {
Expand All @@ -605,7 +605,8 @@ export class StateMachine<
const actorRef = createActor(logic, {
id: actorId,
parent: _actorCtx?.self,
state: actorState
state: actorState,
systemId: actorData.systemId
});

children[actorId] = actorRef;
Expand Down
56 changes: 56 additions & 0 deletions packages/core/test/rehydration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,60 @@ describe('rehydration', () => {

expect(rehydratedActor.getSnapshot().value).toBe('c');
});

it('a rehydrated active child should be registered in the system', () => {
const machine = createMachine(
{
context: ({ spawn }) => {
spawn('foo', {
systemId: 'mySystemId'
});
return {};
}
},
{
actors: {
foo: createMachine({})
}
}
);

const actor = createActor(machine).start();
const persistedState = actor.getPersistedState();
actor.stop();

const rehydratedActor = createActor(machine, {
state: persistedState
}).start();

expect(rehydratedActor.system.get('mySystemId')).not.toBeUndefined();
});

it('a rehydrated done child should not be registered in the system', () => {
const machine = createMachine(
{
context: ({ spawn }) => {
spawn('foo', {
systemId: 'mySystemId'
});
return {};
}
},
{
actors: {
foo: createMachine({ type: 'final' })
}
}
);

const actor = createActor(machine).start();
const persistedState = actor.getPersistedState();
actor.stop();

const rehydratedActor = createActor(machine, {
state: persistedState
}).start();

expect(rehydratedActor.system.get('mySystemId')).toBeUndefined();
});
});

0 comments on commit 3f84bba

Please sign in to comment.