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

[core] Restore from persisted snapshot #4738

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
src,
parent: actorScope.self,
syncSnapshot,
snapshot: persistedSnapshot,
systemId,
input:
typeof input === 'function'
Expand Down Expand Up @@ -171,6 +174,7 @@
systemId?: string;
input?: unknown;
syncSnapshot?: boolean;
snapshot?: any;
}
];

Expand All @@ -183,7 +187,7 @@
>(
...[
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 @@
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
);
});
});
Loading