Skip to content

Commit

Permalink
Restore from persisted snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkpiano committed Feb 9, 2024
1 parent 91549ce commit 9bd98b6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
9 changes: 7 additions & 2 deletions packages/core/src/actions/spawnChild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ function resolveSpawn(
systemId,
src,
input,
syncSnapshot
syncSnapshot,
snapshot: persistedSnapshot
}: {
id: ResolvableActorId<MachineContext, EventObject, EventObject, string>;
systemId: string | undefined;
src: AnyActorLogic | string;
input?: unknown;
syncSnapshot: boolean;
snapshot?: any;
}
) {
const logic =
Expand All @@ -63,6 +65,7 @@ function resolveSpawn(
src,
parent: actorScope.self,
syncSnapshot,
snapshot: persistedSnapshot,
systemId,
input:
typeof input === 'function'
Expand Down Expand Up @@ -171,6 +174,7 @@ type SpawnArguments<
systemId?: string;
input?: unknown;
syncSnapshot?: boolean;
snapshot?: any;
}
];

Expand All @@ -183,7 +187,7 @@ export function spawnChild<
>(
...[
src,
{ id, systemId, input, syncSnapshot = false } = {} as any
{ id, systemId, input, syncSnapshot = false, snapshot } = {} as any

Check failure on line 190 in packages/core/src/actions/spawnChild.ts

View workflow job for this annotation

GitHub Actions / build

Property 'snapshot' does not exist on type '(SpawnActionOptions<TContext, TExpressionEvent, TEvent, ProvidedActor> & { input: unknown; }) | { id?: ResolvableActorId<...> | undefined; systemId?: string | undefined; input?: unknown; syncSnapshot?: boolean | undefined; snapshot?: any; }'.
]: SpawnArguments<TContext, TExpressionEvent, TEvent, TActor>
): SpawnAction<TContext, TExpressionEvent, TParams, TEvent, TActor> {
function spawnChild(
Expand All @@ -201,6 +205,7 @@ export function spawnChild<
spawnChild.src = src;
spawnChild.input = input;
spawnChild.syncSnapshot = syncSnapshot;
spawnChild.snapshot = snapshot;

spawnChild.resolve = resolveSpawn;
spawnChild.execute = executeSpawn;
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export type Spawner<TActor extends ProvidedActor> = IsLiteralString<
systemId?: string;
input?: TLogic extends string ? unknown : InputFrom<TLogic>;
syncSnapshot?: boolean;
snapshot?: any;
}
) => TLogic extends string ? AnyActorRef : ActorRefFrom<TLogic>;

Expand All @@ -67,7 +68,7 @@ export function createSpawner(
spawnedChildren: Record<string, AnyActorRef>
): Spawner<any> {
const spawn: Spawner<any> = (src, options = {}) => {
const { systemId, input } = options;
const { systemId, input, snapshot } = options;
if (typeof src === 'string') {
const logic = resolveReferencedActor(machine, src);

Expand All @@ -90,7 +91,8 @@ export function createSpawner(
})
: input,
src,
systemId
systemId,
snapshot
}) as any;

spawnedChildren[actorRef.id] = actorRef;
Expand Down
22 changes: 22 additions & 0 deletions packages/core/test/spawnChild.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
createMachine,
fromObservable,
fromPromise,
fromTransition,
sendTo,
spawnChild
} from '../src';
Expand Down Expand Up @@ -112,4 +113,25 @@ describe('spawnChild action', () => {

expect(spy).toHaveBeenCalledTimes(1);
});

it('can restore from a persisted snapshot', () => {
const countLogic = fromTransition((s) => s, 0);

const actor = createActor(
createMachine({
entry: spawnChild(countLogic, {
id: 'child',
snapshot: {
context: 100
}
})
})
);

actor.start();

expect(actor.getSnapshot().children.child.getSnapshot().context).toEqual(
100
);
});
});

0 comments on commit 9bd98b6

Please sign in to comment.