From ef3d950edbb7a8ee90d812fe5e87eb2ef02cddd7 Mon Sep 17 00:00:00 2001 From: David Khourshid Date: Sun, 10 Mar 2024 10:11:23 -0400 Subject: [PATCH 1/7] Add children to snapshot type --- packages/core/src/State.ts | 2 +- packages/core/src/actors/callback.ts | 3 ++- packages/core/src/actors/observable.ts | 6 ++++-- packages/core/src/actors/promise.ts | 3 ++- packages/core/src/actors/transition.ts | 3 ++- packages/core/src/types.ts | 4 ++++ packages/core/test/actor.test.ts | 3 ++- packages/core/test/errors.test.ts | 3 ++- packages/core/test/invoke.test.ts | 6 ++++-- packages/core/test/typeHelpers.test.ts | 3 ++- 10 files changed, 25 insertions(+), 11 deletions(-) diff --git a/packages/core/src/State.ts b/packages/core/src/State.ts index b2a96f0f93..1e68f87552 100644 --- a/packages/core/src/State.ts +++ b/packages/core/src/State.ts @@ -406,7 +406,7 @@ export function getPersistedSnapshot< const persisted = { ...jsonValues, context: persistContext(context) as any, - children: childrenJson + children: childrenJson as Record }; return persisted; diff --git a/packages/core/src/actors/callback.ts b/packages/core/src/actors/callback.ts index c8ed9df29d..e999a24901 100644 --- a/packages/core/src/actors/callback.ts +++ b/packages/core/src/actors/callback.ts @@ -199,7 +199,8 @@ export function fromCallback< status: 'active', output: undefined, error: undefined, - input + input, + children: {} }; }, getPersistedSnapshot: (snapshot) => snapshot, diff --git a/packages/core/src/actors/observable.ts b/packages/core/src/actors/observable.ts index 6fddbe8aa6..fddd2586ed 100644 --- a/packages/core/src/actors/observable.ts +++ b/packages/core/src/actors/observable.ts @@ -138,7 +138,8 @@ export function fromObservable( error: undefined, context: undefined, input, - _subscription: undefined + _subscription: undefined, + children: {} }; }, start: (state, { self, system }) => { @@ -278,7 +279,8 @@ export function fromEventObservable< error: undefined, context: undefined, input, - _subscription: undefined + _subscription: undefined, + children: {} }; }, start: (state, { self, system }) => { diff --git a/packages/core/src/actors/promise.ts b/packages/core/src/actors/promise.ts index 036ef9b1ae..f65f9a89ea 100644 --- a/packages/core/src/actors/promise.ts +++ b/packages/core/src/actors/promise.ts @@ -161,7 +161,8 @@ export function fromPromise( status: 'active', output: undefined, error: undefined, - input + input, + children: {} }; }, getPersistedSnapshot: (snapshot) => snapshot, diff --git a/packages/core/src/actors/transition.ts b/packages/core/src/actors/transition.ts index c474c95a57..e2f280520f 100644 --- a/packages/core/src/actors/transition.ts +++ b/packages/core/src/actors/transition.ts @@ -132,7 +132,8 @@ export function fromTransition< context: typeof initialContext === 'function' ? (initialContext as any)({ input }) - : initialContext + : initialContext, + children: {} }; }, getPersistedSnapshot: (snapshot) => snapshot, diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 6d4d9d1b92..1ecb01e239 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -2175,21 +2175,25 @@ export type Snapshot = status: 'active'; output: undefined; error: undefined; + children: Record; } | { status: 'done'; output: TOutput; error: undefined; + children: Record; } | { status: 'error'; output: undefined; error: unknown; + children: Record; } | { status: 'stopped'; output: undefined; error: undefined; + children: Record; }; /** diff --git a/packages/core/test/actor.test.ts b/packages/core/test/actor.test.ts index e355e80f83..5df88aa6a6 100644 --- a/packages/core/test/actor.test.ts +++ b/packages/core/test/actor.test.ts @@ -1158,7 +1158,8 @@ describe('actors', () => { getInitialSnapshot: () => ({ status: 'active', output: undefined, - error: undefined + error: undefined, + children: {} }), getPersistedSnapshot: (s) => s }; diff --git a/packages/core/test/errors.test.ts b/packages/core/test/errors.test.ts index 0a6cb0d1a1..5f1e021b3d 100644 --- a/packages/core/test/errors.test.ts +++ b/packages/core/test/errors.test.ts @@ -833,7 +833,8 @@ describe('error handling', () => { status: 'error', output: undefined, error: 'immediate error!', - context: undefined + context: undefined, + children: {} }); const machine = createMachine( diff --git a/packages/core/test/invoke.test.ts b/packages/core/test/invoke.test.ts index 5556de749b..26463d706c 100644 --- a/packages/core/test/invoke.test.ts +++ b/packages/core/test/invoke.test.ts @@ -2264,7 +2264,8 @@ describe('invoke', () => { status: 'active', output: undefined, error: undefined, - context: 0 + context: 0, + children: {} }), getPersistedSnapshot: (s) => s }; @@ -2305,7 +2306,8 @@ describe('invoke', () => { getInitialSnapshot: () => ({ status: 'active', output: undefined, - error: undefined + error: undefined, + children: {} }), getPersistedSnapshot: (s) => s }; diff --git a/packages/core/test/typeHelpers.test.ts b/packages/core/test/typeHelpers.test.ts index 32fda65a18..100d837229 100644 --- a/packages/core/test/typeHelpers.test.ts +++ b/packages/core/test/typeHelpers.test.ts @@ -362,7 +362,8 @@ describe('ActorRefFrom', () => { getInitialSnapshot: () => ({ status: 'active', output: undefined, - error: undefined + error: undefined, + children: {} }), getPersistedSnapshot: (s) => s }; From 024b81272fd1545657497129cb5f2665e6dd4983 Mon Sep 17 00:00:00 2001 From: David Khourshid Date: Sun, 10 Mar 2024 10:11:48 -0400 Subject: [PATCH 2/7] Changeset --- .changeset/chilly-carpets-draw.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/chilly-carpets-draw.md diff --git a/.changeset/chilly-carpets-draw.md b/.changeset/chilly-carpets-draw.md new file mode 100644 index 0000000000..6c6e124e8e --- /dev/null +++ b/.changeset/chilly-carpets-draw.md @@ -0,0 +1,5 @@ +--- +'xstate': patch +--- + +Add `children` to `Snapshot` interface From feacd3afb4809b0bf21f290c0845a9e3bb1f68cb Mon Sep 17 00:00:00 2001 From: David Khourshid Date: Sun, 10 Mar 2024 10:47:24 -0400 Subject: [PATCH 3/7] Update tests --- packages/core/test/actorLogic.test.ts | 11 ++++++++--- packages/core/test/inspect.test.ts | 2 ++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/core/test/actorLogic.test.ts b/packages/core/test/actorLogic.test.ts index 9cba36d698..f2e444315a 100644 --- a/packages/core/test/actorLogic.test.ts +++ b/packages/core/test/actorLogic.test.ts @@ -135,6 +135,7 @@ describe('promise logic (fromPromise)', () => { expect(resolvedPersistedState).toMatchInlineSnapshot(` { + "children": {}, "error": undefined, "input": undefined, "output": 42, @@ -164,6 +165,7 @@ describe('promise logic (fromPromise)', () => { const resolvedPersistedState = actor.getPersistedSnapshot(); expect(resolvedPersistedState).toMatchInlineSnapshot(` { + "children": {}, "error": undefined, "input": undefined, "output": 1, @@ -195,6 +197,7 @@ describe('promise logic (fromPromise)', () => { const rejectedPersistedState = actorRef.getPersistedSnapshot(); expect(rejectedPersistedState).toMatchInlineSnapshot(` { + "children": {}, "error": 1, "input": undefined, "output": undefined, @@ -281,7 +284,8 @@ describe('transition function logic (fromTransition)', () => { error: undefined, context: { enabled: 'on' - } + }, + children: {} }); const restoredActor = createActor(logic, { snapshot: persistedSnapshot }); @@ -608,6 +612,7 @@ describe('machine logic', () => { expect((persistedState as any).children.a.snapshot).toMatchInlineSnapshot(` { + "children": {}, "error": undefined, "input": undefined, "output": 42, @@ -623,9 +628,9 @@ describe('machine logic', () => { value: 'start', children: { reducer: expect.objectContaining({ - snapshot: { + snapshot: expect.objectContaining({ status: 'active' - } + }) }) } }) diff --git a/packages/core/test/inspect.test.ts b/packages/core/test/inspect.test.ts index b723366543..8c92296f82 100644 --- a/packages/core/test/inspect.test.ts +++ b/packages/core/test/inspect.test.ts @@ -327,6 +327,7 @@ describe('inspect', () => { "type": "xstate.init", }, "snapshot": { + "children": {}, "error": undefined, "input": undefined, "output": undefined, @@ -434,6 +435,7 @@ describe('inspect', () => { "type": "xstate.promise.resolve", }, "snapshot": { + "children": {}, "error": undefined, "input": undefined, "output": 42, From 67ef2d08cb8031d00c9220d29781956506d36f69 Mon Sep 17 00:00:00 2001 From: David Khourshid Date: Tue, 19 Mar 2024 13:18:45 -0400 Subject: [PATCH 4/7] Add spawnChild --- packages/core/src/actors/callback.ts | 10 +++++++--- packages/core/src/actors/observable.ts | 9 ++++++--- packages/core/src/actors/promise.ts | 6 ++++-- packages/core/src/createActor.ts | 16 ++++++++++++++++ packages/core/src/getNextSnapshot.ts | 7 ++++++- packages/core/src/types.ts | 4 ++++ packages/core/src/utils.ts | 9 +++++++++ 7 files changed, 52 insertions(+), 9 deletions(-) diff --git a/packages/core/src/actors/callback.ts b/packages/core/src/actors/callback.ts index e999a24901..89f143ba16 100644 --- a/packages/core/src/actors/callback.ts +++ b/packages/core/src/actors/callback.ts @@ -4,6 +4,7 @@ import { ActorLogic, ActorRefFrom, AnyActorRef, + AnyActorScope, AnyEventObject, EventObject, NonReducibleUnknown, @@ -55,7 +56,8 @@ export type InvokeCallback< system, self, sendBack, - receive + receive, + spawnChild }: { /** * Data that was provided to the callback actor @@ -79,6 +81,7 @@ export type InvokeCallback< * the listener is then called whenever events are received by the callback actor */ receive: Receiver; + spawnChild: AnyActorScope['spawnChild']; }) => (() => void) | void; /** @@ -147,7 +150,7 @@ export function fromCallback< const logic: CallbackActorLogic = { config: invokeCallback, start: (state, actorScope) => { - const { self, system } = actorScope; + const { self, system, spawnChild } = actorScope; const callbackState: CallbackInstanceState = { receivers: undefined, @@ -171,7 +174,8 @@ export function fromCallback< receive: (listener) => { callbackState.receivers ??= new Set(); callbackState.receivers.add(listener); - } + }, + spawnChild }); }, transition: (state, event, actorScope) => { diff --git a/packages/core/src/actors/observable.ts b/packages/core/src/actors/observable.ts index fddd2586ed..d8799c5a86 100644 --- a/packages/core/src/actors/observable.ts +++ b/packages/core/src/actors/observable.ts @@ -3,6 +3,7 @@ import { AnyActorSystem } from '../system.ts'; import { ActorLogic, ActorRefFrom, + AnyActorScope, EventObject, NonReducibleUnknown, Snapshot, @@ -86,12 +87,13 @@ export function fromObservable( input: TInput; system: AnyActorSystem; self: ObservableActorRef; + spawnChild: AnyActorScope['spawnChild']; }) => Subscribable ): ObservableActorLogic { // TODO: add event types const logic: ObservableActorLogic = { config: observableCreator, - transition: (snapshot, event, { self, id, defer, system }) => { + transition: (snapshot, event) => { if (snapshot.status !== 'active') { return snapshot; } @@ -142,7 +144,7 @@ export function fromObservable( children: {} }; }, - start: (state, { self, system }) => { + start: (state, { self, system, spawnChild }) => { if (state.status === 'done') { // Do not restart a completed observable return; @@ -150,7 +152,8 @@ export function fromObservable( state._subscription = observableCreator({ input: state.input!, system, - self + self, + spawnChild }).subscribe({ next: (value) => { system._relay(self, self, { diff --git a/packages/core/src/actors/promise.ts b/packages/core/src/actors/promise.ts index f65f9a89ea..1a2476e099 100644 --- a/packages/core/src/actors/promise.ts +++ b/packages/core/src/actors/promise.ts @@ -3,6 +3,7 @@ import { AnyActorSystem } from '../system.ts'; import { ActorLogic, ActorRefFrom, + AnyActorScope, EventObject, NonReducibleUnknown, Snapshot @@ -88,6 +89,7 @@ export function fromPromise( * The parent actor of the promise actor */ self: PromiseActorRef; + spawnChild: AnyActorScope['spawnChild']; }) => PromiseLike ): PromiseActorLogic { const logic: PromiseActorLogic = { @@ -124,7 +126,7 @@ export function fromPromise( return state; } }, - start: (state, { self, system }) => { + start: (state, { self, system, spawnChild }) => { // TODO: determine how to allow customizing this so that promises // can be restarted if necessary if (state.status !== 'active') { @@ -132,7 +134,7 @@ export function fromPromise( } const resolvedPromise = Promise.resolve( - promiseCreator({ input: state.input!, system, self }) + promiseCreator({ input: state.input!, system, self, spawnChild }) ); resolvedPromise.then( diff --git a/packages/core/src/createActor.ts b/packages/core/src/createActor.ts index fa5820f4c1..0e80fc8ff3 100644 --- a/packages/core/src/createActor.ts +++ b/packages/core/src/createActor.ts @@ -12,6 +12,7 @@ import { symbolObservable } from './symbolObservable.ts'; import { AnyActorSystem, Clock, createSystem } from './system.ts'; import type { + ActorRefFrom, ActorScope, AnyActorLogic, AnyActorRef, @@ -183,6 +184,21 @@ export class Actor this._deferred.push(fn); }, system: this.system, + spawnChild: ( + logic: T, + actorOptions?: ActorOptions + ) => { + const actor = createActor(logic, { + parent: this, + ...actorOptions + }); + + if (this._processingStatus === ProcessingStatus.Running) { + actor.start(); + } + + return actor as ActorRefFrom; + }, stopChild: (child) => { if (child._parent !== this) { throw new Error( diff --git a/packages/core/src/getNextSnapshot.ts b/packages/core/src/getNextSnapshot.ts index 611fcbaff0..07d1e411b8 100644 --- a/packages/core/src/getNextSnapshot.ts +++ b/packages/core/src/getNextSnapshot.ts @@ -27,7 +27,12 @@ export function createInertActorScope( sessionId: '', stopChild: () => {}, system: self.system, - emit: () => {} + emit: () => {}, + spawnChild(logic) { + const child = createActor(logic) as any; + + return child; + } }; return inertActorScope; diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 87c7cb5103..04c6a9e900 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -2167,6 +2167,10 @@ export interface ActorScope< emit: (event: TEmitted) => void; system: TSystem; stopChild: (child: AnyActorRef) => void; + spawnChild: ( + logic: T, + actorOptions?: ActorOptions + ) => ActorRefFrom; } export type AnyActorScope = ActorScope< diff --git a/packages/core/src/utils.ts b/packages/core/src/utils.ts index 3d7ef4c9b4..d47bfe4072 100644 --- a/packages/core/src/utils.ts +++ b/packages/core/src/utils.ts @@ -206,6 +206,15 @@ export function isActorLogic(value: any): value is AnyActorLogic { ); } +export function isActorRef(value: any): value is AnyActorRef { + return ( + value !== null && + typeof value === 'object' && + 'send' in value && + typeof value.send === 'function' + ); +} + export function isArray(value: any): value is readonly any[] { return Array.isArray(value); } From df1087e1e49cef8f958eed99289fd1ebdb460cab Mon Sep 17 00:00:00 2001 From: David Khourshid Date: Sat, 4 May 2024 10:02:12 -0400 Subject: [PATCH 5/7] Make things optional --- packages/core/src/actions/stopChild.ts | 4 ++-- packages/core/src/types.ts | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/core/src/actions/stopChild.ts b/packages/core/src/actions/stopChild.ts index 2f0f1b6917..60c0dea564 100644 --- a/packages/core/src/actions/stopChild.ts +++ b/packages/core/src/actions/stopChild.ts @@ -67,7 +67,7 @@ function executeStop( // this allows us to prevent an actor from being started if it gets stopped within the same macrostep // this can happen, for example, when the invoking state is being exited immediately by an always transition if (actorRef._processingStatus !== ProcessingStatus.Running) { - actorScope.stopChild(actorRef); + actorScope.stopChild?.(actorRef); return; } // stopping a child enqueues a stop event in the child actor's mailbox @@ -75,7 +75,7 @@ function executeStop( // the parent itself might want to send some events to a child (for example from exit actions on the invoking state) // and we don't want to ignore those events actorScope.defer(() => { - actorScope.stopChild(actorRef); + actorScope.stopChild?.(actorRef); }); } diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 75559a9060..bfc16be1e3 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -2345,8 +2345,8 @@ export interface ActorScope< defer: (fn: () => void) => void; emit: (event: TEmitted) => void; system: TSystem; - stopChild: (child: AnyActorRef) => void; - spawnChild: ( + stopChild?: (child: AnyActorRef) => void; + spawnChild?: ( logic: T, actorOptions?: ActorOptions ) => ActorRefFrom; @@ -2364,25 +2364,25 @@ export type Snapshot = status: 'active'; output: undefined; error: undefined; - children: Record; + children?: Record; } | { status: 'done'; output: TOutput; error: undefined; - children: Record; + children?: Record; } | { status: 'error'; output: undefined; error: unknown; - children: Record; + children?: Record; } | { status: 'stopped'; output: undefined; error: undefined; - children: Record; + children?: Record; }; /** From 666932404344db46d01f57545498d45d1ae4bd3e Mon Sep 17 00:00:00 2001 From: David Khourshid Date: Sat, 18 May 2024 23:46:55 -0400 Subject: [PATCH 6/7] Add context to snapshot --- packages/core/src/actors/callback.ts | 1 + packages/core/src/actors/promise.ts | 1 + packages/core/src/actors/transition.ts | 4 +--- packages/core/src/types.ts | 6 +++++- packages/core/test/actor.test.ts | 1 + packages/core/test/invoke.test.ts | 1 + packages/core/test/typeHelpers.test.ts | 1 + 7 files changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/core/src/actors/callback.ts b/packages/core/src/actors/callback.ts index 89f143ba16..e67d10e2f5 100644 --- a/packages/core/src/actors/callback.ts +++ b/packages/core/src/actors/callback.ts @@ -204,6 +204,7 @@ export function fromCallback< output: undefined, error: undefined, input, + context: undefined, children: {} }; }, diff --git a/packages/core/src/actors/promise.ts b/packages/core/src/actors/promise.ts index 00dd44391f..a09c660a53 100644 --- a/packages/core/src/actors/promise.ts +++ b/packages/core/src/actors/promise.ts @@ -180,6 +180,7 @@ export function fromPromise( output: undefined, error: undefined, input, + context: undefined, children: {} }; }, diff --git a/packages/core/src/actors/transition.ts b/packages/core/src/actors/transition.ts index e2f280520f..79a5d2e618 100644 --- a/packages/core/src/actors/transition.ts +++ b/packages/core/src/actors/transition.ts @@ -8,9 +8,7 @@ import { Snapshot } from '../types.ts'; -export type TransitionSnapshot = Snapshot & { - context: TContext; -}; +export type TransitionSnapshot = Snapshot; export type TransitionActorLogic< TContext, diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index bfc16be1e3..01ba6419c7 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -2359,29 +2359,33 @@ export type AnyActorScope = ActorScope< any // TEmitted >; -export type Snapshot = +export type Snapshot = | { status: 'active'; output: undefined; error: undefined; + context: TContext; children?: Record; } | { status: 'done'; output: TOutput; error: undefined; + context: TContext; children?: Record; } | { status: 'error'; output: undefined; error: unknown; + context: TContext; children?: Record; } | { status: 'stopped'; output: undefined; error: undefined; + context: TContext; children?: Record; }; diff --git a/packages/core/test/actor.test.ts b/packages/core/test/actor.test.ts index 5df88aa6a6..a9d425492f 100644 --- a/packages/core/test/actor.test.ts +++ b/packages/core/test/actor.test.ts @@ -1159,6 +1159,7 @@ describe('actors', () => { status: 'active', output: undefined, error: undefined, + context: undefined, children: {} }), getPersistedSnapshot: (s) => s diff --git a/packages/core/test/invoke.test.ts b/packages/core/test/invoke.test.ts index 26463d706c..b4809f823b 100644 --- a/packages/core/test/invoke.test.ts +++ b/packages/core/test/invoke.test.ts @@ -2307,6 +2307,7 @@ describe('invoke', () => { status: 'active', output: undefined, error: undefined, + context: undefined, children: {} }), getPersistedSnapshot: (s) => s diff --git a/packages/core/test/typeHelpers.test.ts b/packages/core/test/typeHelpers.test.ts index 100d837229..668f971de4 100644 --- a/packages/core/test/typeHelpers.test.ts +++ b/packages/core/test/typeHelpers.test.ts @@ -363,6 +363,7 @@ describe('ActorRefFrom', () => { status: 'active', output: undefined, error: undefined, + context: undefined, children: {} }), getPersistedSnapshot: (s) => s From ea380f5c1aa85dca1d7e9dbd4d96b1e43e38a1f3 Mon Sep 17 00:00:00 2001 From: David Khourshid Date: Sun, 18 Aug 2024 16:21:11 -0400 Subject: [PATCH 7/7] Test updates --- packages/core/test/actorLogic.test.ts | 68 +++-- packages/core/test/inspect.test.ts | 422 +++++++++++++------------- 2 files changed, 248 insertions(+), 242 deletions(-) diff --git a/packages/core/test/actorLogic.test.ts b/packages/core/test/actorLogic.test.ts index ad198fb602..9fdd514e0e 100644 --- a/packages/core/test/actorLogic.test.ts +++ b/packages/core/test/actorLogic.test.ts @@ -134,14 +134,15 @@ describe('promise logic (fromPromise)', () => { const resolvedPersistedState = actor.getPersistedSnapshot(); expect(resolvedPersistedState).toMatchInlineSnapshot(` - { - "children": {}, - "error": undefined, - "input": undefined, - "output": 42, - "status": "done", - } - `); +{ + "children": {}, + "context": undefined, + "error": undefined, + "input": undefined, + "output": 42, + "status": "done", +} +`); const restoredActor = createActor(promiseLogic, { snapshot: resolvedPersistedState @@ -164,14 +165,15 @@ describe('promise logic (fromPromise)', () => { const resolvedPersistedState = actor.getPersistedSnapshot(); expect(resolvedPersistedState).toMatchInlineSnapshot(` - { - "children": {}, - "error": undefined, - "input": undefined, - "output": 1, - "status": "done", - } - `); +{ + "children": {}, + "context": undefined, + "error": undefined, + "input": undefined, + "output": 1, + "status": "done", +} +`); expect(createdPromises).toBe(1); const restoredActor = createActor(promiseLogic, { @@ -196,14 +198,15 @@ describe('promise logic (fromPromise)', () => { const rejectedPersistedState = actorRef.getPersistedSnapshot(); expect(rejectedPersistedState).toMatchInlineSnapshot(` - { - "children": {}, - "error": 1, - "input": undefined, - "output": undefined, - "status": "error", - } - `); +{ + "children": {}, + "context": undefined, + "error": 1, + "input": undefined, + "output": undefined, + "status": "error", +} +`); expect(createdPromises).toBe(1); const actorRef2 = createActor(promiseLogic, { @@ -843,14 +846,15 @@ describe('machine logic', () => { const persistedState = actor.getPersistedSnapshot()!; expect((persistedState as any).children.a.snapshot).toMatchInlineSnapshot(` - { - "children": {}, - "error": undefined, - "input": undefined, - "output": 42, - "status": "done", - } - `); +{ + "children": {}, + "context": undefined, + "error": undefined, + "input": undefined, + "output": 42, + "status": "done", +} +`); expect((persistedState as any).children.b.snapshot).toEqual( expect.objectContaining({ diff --git a/packages/core/test/inspect.test.ts b/packages/core/test/inspect.test.ts index 00fce75597..e473c961ab 100644 --- a/packages/core/test/inspect.test.ts +++ b/packages/core/test/inspect.test.ts @@ -240,216 +240,218 @@ describe('inspect', () => { ['@xstate.actor', '@xstate.event', '@xstate.snapshot'].includes(ev.type) ) ).toMatchInlineSnapshot(` - [ - { - "actorId": "x:1", - "type": "@xstate.actor", - }, - { - "actorId": "x:2", - "type": "@xstate.actor", - }, - { - "event": { - "input": undefined, - "type": "xstate.init", - }, - "sourceId": undefined, - "targetId": "x:1", - "type": "@xstate.event", - }, - { - "event": { - "input": undefined, - "type": "xstate.init", - }, - "sourceId": "x:1", - "targetId": "x:2", - "type": "@xstate.event", - }, - { - "actorId": "x:2", - "event": { - "input": undefined, - "type": "xstate.init", - }, - "snapshot": { - "value": "start", - }, - "status": "active", - "type": "@xstate.snapshot", - }, - { - "actorId": "x:1", - "event": { - "input": undefined, - "type": "xstate.init", - }, - "snapshot": { - "value": "waiting", - }, - "status": "active", - "type": "@xstate.snapshot", - }, - { - "event": { - "type": "load", - }, - "sourceId": undefined, - "targetId": "x:1", - "type": "@xstate.event", - }, - { - "event": { - "type": "loadChild", - }, - "sourceId": "x:1", - "targetId": "x:2", - "type": "@xstate.event", - }, - { - "actorId": "x:3", - "type": "@xstate.actor", - }, - { - "event": { - "input": undefined, - "type": "xstate.init", - }, - "sourceId": "x:2", - "targetId": "x:3", - "type": "@xstate.event", - }, - { - "actorId": "x:3", - "event": { - "input": undefined, - "type": "xstate.init", - }, - "snapshot": { - "children": {}, - "error": undefined, - "input": undefined, - "output": undefined, - "status": "active", - }, - "status": "active", - "type": "@xstate.snapshot", - }, - { - "actorId": "x:2", - "event": { - "type": "loadChild", - }, - "snapshot": { - "value": "loading", - }, - "status": "active", - "type": "@xstate.snapshot", - }, - { - "actorId": "x:1", - "event": { - "type": "load", - }, - "snapshot": { - "value": "waiting", - }, - "status": "active", - "type": "@xstate.snapshot", - }, - { - "event": { - "data": 42, - "type": "xstate.promise.resolve", - }, - "sourceId": "x:3", - "targetId": "x:3", - "type": "@xstate.event", - }, - { - "event": { - "actorId": "0.(machine).loading", - "output": 42, - "type": "xstate.done.actor.0.(machine).loading", - }, - "sourceId": "x:3", - "targetId": "x:2", - "type": "@xstate.event", - }, - { - "event": { - "type": "toParent", - }, - "sourceId": "x:2", - "targetId": "x:1", - "type": "@xstate.event", - }, - { - "actorId": "x:1", - "event": { - "type": "toParent", - }, - "snapshot": { - "value": "waiting", - }, - "status": "active", - "type": "@xstate.snapshot", - }, - { - "event": { - "actorId": "child", - "output": undefined, - "type": "xstate.done.actor.child", - }, - "sourceId": "x:2", - "targetId": "x:1", - "type": "@xstate.event", - }, - { - "actorId": "x:1", - "event": { - "actorId": "child", - "output": undefined, - "type": "xstate.done.actor.child", - }, - "snapshot": { - "value": "success", - }, - "status": "active", - "type": "@xstate.snapshot", - }, - { - "actorId": "x:2", - "event": { - "actorId": "0.(machine).loading", - "output": 42, - "type": "xstate.done.actor.0.(machine).loading", - }, - "snapshot": { - "value": "loaded", - }, - "status": "done", - "type": "@xstate.snapshot", - }, - { - "actorId": "x:3", - "event": { - "data": 42, - "type": "xstate.promise.resolve", - }, - "snapshot": { - "children": {}, - "error": undefined, - "input": undefined, - "output": 42, - "status": "done", - }, - "status": "done", - "type": "@xstate.snapshot", - }, - ] - `); +[ + { + "actorId": "x:1", + "type": "@xstate.actor", + }, + { + "actorId": "x:2", + "type": "@xstate.actor", + }, + { + "event": { + "input": undefined, + "type": "xstate.init", + }, + "sourceId": undefined, + "targetId": "x:1", + "type": "@xstate.event", + }, + { + "event": { + "input": undefined, + "type": "xstate.init", + }, + "sourceId": "x:1", + "targetId": "x:2", + "type": "@xstate.event", + }, + { + "actorId": "x:2", + "event": { + "input": undefined, + "type": "xstate.init", + }, + "snapshot": { + "value": "start", + }, + "status": "active", + "type": "@xstate.snapshot", + }, + { + "actorId": "x:1", + "event": { + "input": undefined, + "type": "xstate.init", + }, + "snapshot": { + "value": "waiting", + }, + "status": "active", + "type": "@xstate.snapshot", + }, + { + "event": { + "type": "load", + }, + "sourceId": undefined, + "targetId": "x:1", + "type": "@xstate.event", + }, + { + "event": { + "type": "loadChild", + }, + "sourceId": "x:1", + "targetId": "x:2", + "type": "@xstate.event", + }, + { + "actorId": "x:3", + "type": "@xstate.actor", + }, + { + "event": { + "input": undefined, + "type": "xstate.init", + }, + "sourceId": "x:2", + "targetId": "x:3", + "type": "@xstate.event", + }, + { + "actorId": "x:3", + "event": { + "input": undefined, + "type": "xstate.init", + }, + "snapshot": { + "children": {}, + "context": undefined, + "error": undefined, + "input": undefined, + "output": undefined, + "status": "active", + }, + "status": "active", + "type": "@xstate.snapshot", + }, + { + "actorId": "x:2", + "event": { + "type": "loadChild", + }, + "snapshot": { + "value": "loading", + }, + "status": "active", + "type": "@xstate.snapshot", + }, + { + "actorId": "x:1", + "event": { + "type": "load", + }, + "snapshot": { + "value": "waiting", + }, + "status": "active", + "type": "@xstate.snapshot", + }, + { + "event": { + "data": 42, + "type": "xstate.promise.resolve", + }, + "sourceId": "x:3", + "targetId": "x:3", + "type": "@xstate.event", + }, + { + "event": { + "actorId": "0.(machine).loading", + "output": 42, + "type": "xstate.done.actor.0.(machine).loading", + }, + "sourceId": "x:3", + "targetId": "x:2", + "type": "@xstate.event", + }, + { + "event": { + "type": "toParent", + }, + "sourceId": "x:2", + "targetId": "x:1", + "type": "@xstate.event", + }, + { + "actorId": "x:1", + "event": { + "type": "toParent", + }, + "snapshot": { + "value": "waiting", + }, + "status": "active", + "type": "@xstate.snapshot", + }, + { + "event": { + "actorId": "child", + "output": undefined, + "type": "xstate.done.actor.child", + }, + "sourceId": "x:2", + "targetId": "x:1", + "type": "@xstate.event", + }, + { + "actorId": "x:1", + "event": { + "actorId": "child", + "output": undefined, + "type": "xstate.done.actor.child", + }, + "snapshot": { + "value": "success", + }, + "status": "active", + "type": "@xstate.snapshot", + }, + { + "actorId": "x:2", + "event": { + "actorId": "0.(machine).loading", + "output": 42, + "type": "xstate.done.actor.0.(machine).loading", + }, + "snapshot": { + "value": "loaded", + }, + "status": "done", + "type": "@xstate.snapshot", + }, + { + "actorId": "x:3", + "event": { + "data": 42, + "type": "xstate.promise.resolve", + }, + "snapshot": { + "children": {}, + "context": undefined, + "error": undefined, + "input": undefined, + "output": 42, + "status": "done", + }, + "status": "done", + "type": "@xstate.snapshot", + }, +] +`); }); it('can inspect microsteps from always events', async () => {