xstate 6.0.0-alpha.19 → 6.0.0-alpha.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/dist/{StateMachine-bb722e90.esm.js → StateMachine-42f1d26c.esm.js} +9 -4
  2. package/dist/{StateMachine-9e72e36d.development.cjs.js → StateMachine-6661eb0d.development.cjs.js} +9 -4
  3. package/dist/{StateMachine-f777a374.development.esm.js → StateMachine-ba3ccedf.development.esm.js} +9 -4
  4. package/dist/{StateMachine-20b7d63b.cjs.js → StateMachine-f2b70732.cjs.js} +9 -4
  5. package/dist/declarations/src/StateMachine.d.ts +1 -0
  6. package/dist/declarations/src/actions.d.ts +1 -0
  7. package/dist/declarations/src/transitionActions.d.ts +7 -1
  8. package/dist/declarations/src/types.d.ts +10 -5
  9. package/dist/{index-626fe0b0.cjs.js → index-42956779.cjs.js} +77 -8
  10. package/dist/{index-792d3638.development.cjs.js → index-49293864.development.cjs.js} +77 -8
  11. package/dist/{index-0842e9fd.esm.js → index-d88b0c55.esm.js} +77 -9
  12. package/dist/{index-b5a6f830.development.esm.js → index-dc3443b6.development.esm.js} +77 -9
  13. package/dist/xstate-actors.cjs.js +1 -1
  14. package/dist/xstate-actors.development.cjs.js +1 -1
  15. package/dist/xstate-actors.development.esm.js +1 -1
  16. package/dist/xstate-actors.esm.js +1 -1
  17. package/dist/xstate-actors.umd.min.js +1 -1
  18. package/dist/xstate-actors.umd.min.js.map +1 -1
  19. package/dist/xstate-graph.cjs.js +2 -2
  20. package/dist/xstate-graph.development.cjs.js +2 -2
  21. package/dist/xstate-graph.development.esm.js +2 -2
  22. package/dist/xstate-graph.esm.js +2 -2
  23. package/dist/xstate-graph.umd.min.js +1 -1
  24. package/dist/xstate-graph.umd.min.js.map +1 -1
  25. package/dist/xstate.cjs.js +9 -5
  26. package/dist/xstate.development.cjs.js +9 -5
  27. package/dist/xstate.development.esm.js +11 -7
  28. package/dist/xstate.esm.js +11 -7
  29. package/dist/xstate.umd.min.js +1 -1
  30. package/dist/xstate.umd.min.js.map +1 -1
  31. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- import { P as ProcessingStatus, G as resolveReferencedActor, d as createActor, S as STATE_DELIMITER, H as mapValues, t as toArray, I as createInvokeId, J as getDelayedTransitions, K as evaluateCandidate, L as formatTransition, N as NULL_EVENT, M as toTransitionConfigArray, O as createInvokeTimeoutEvent, Q as getCandidates, R as formatRouteTransitions, U as resolveStateValue, V as getAllStateNodes, k as getStateNodes, W as createMachineSnapshot, Y as isInFinalState, e as macrostep, Z as cloneMachineSnapshot, _ as transitionNode, m as matchesEventDescriptor, $ as getTransitionResult, a0 as hasEffect, a1 as createErrorPlatformEvent, r as resolveActionsWithContext, f as createInitEvent, g as initialMicrostep, a2 as toStatePath, a3 as isStateId, a4 as getStateNodeByPath, a5 as getPersistedSnapshot, a6 as $$ACTOR_TYPE } from './index-0842e9fd.esm.js';
1
+ import { P as ProcessingStatus, H as resolveReferencedActor, e as createActor, S as STATE_DELIMITER, I as mapValues, t as toArray, J as createInvokeId, K as getDelayedTransitions, L as evaluateCandidate, M as formatTransition, N as NULL_EVENT, O as toTransitionConfigArray, Q as createInvokeTimeoutEvent, R as getCandidates, U as formatRouteTransitions, V as resolveStateValue, W as getAllStateNodes, l as getStateNodes, Y as createMachineSnapshot, Z as isInFinalState, f as macrostep, d as deriveDeferredStarts, _ as cloneMachineSnapshot, $ as transitionNode, m as matchesEventDescriptor, a0 as getTransitionResult, a1 as hasEffect, a2 as createErrorPlatformEvent, r as resolveActionsWithContext, g as createInitEvent, h as initialMicrostep, a3 as toStatePath, a4 as isStateId, a5 as getStateNodeByPath, a6 as getPersistedSnapshot, a7 as $$ACTOR_TYPE } from './index-d88b0c55.esm.js';
2
2
 
3
3
  function createSpawner(actorScope, {
4
4
  machine,
@@ -549,7 +549,11 @@ class StateMachine {
549
549
  snapshot: nextSnapshot,
550
550
  microsteps
551
551
  } = macrostep(snapshot, event, actorScope, []);
552
- return [nextSnapshot, microsteps.flatMap(([, actions]) => actions)];
552
+ return [nextSnapshot, this._collectEffects(microsteps)];
553
+ }
554
+ _collectEffects(microsteps, initialActions = []) {
555
+ const effects = [...initialActions, ...microsteps.flatMap(([, actions]) => actions)];
556
+ return [...effects, ...deriveDeferredStarts(effects)];
553
557
  }
554
558
  _transitionFast(snapshot, event, actorScope) {
555
559
  if (snapshot.status !== 'active' || typeof snapshot.value !== 'string' || this.root.always?.length) {
@@ -732,7 +736,7 @@ class StateMachine {
732
736
  microsteps = errorMacrostep.microsteps;
733
737
  initialActions = [];
734
738
  }
735
- return [macroState, [...initialActions, ...microsteps.flatMap(([, actions]) => actions)]];
739
+ return [macroState, this._collectEffects(microsteps, initialActions)];
736
740
  }
737
741
  start(snapshot) {
738
742
  // Start rehydrated children that were active when persisted. Freshly
@@ -830,7 +834,8 @@ class StateMachine {
830
834
  children,
831
835
  _nodes: nodes,
832
836
  value: snapshotData.value,
833
- historyValue: revivedHistoryValue
837
+ historyValue: revivedHistoryValue,
838
+ _stateInputs: snapshotData.stateInputs ?? {}
834
839
  }, this);
835
840
  const seen = new WeakSet();
836
841
  function reviveContext(contextPart) {
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var dist_xstateActors = require('./index-792d3638.development.cjs.js');
3
+ var dist_xstateActors = require('./index-49293864.development.cjs.js');
4
4
 
5
5
  function createSpawner(actorScope, {
6
6
  machine,
@@ -580,7 +580,11 @@ class StateMachine {
580
580
  snapshot: nextSnapshot,
581
581
  microsteps
582
582
  } = dist_xstateActors.macrostep(snapshot, event, actorScope, []);
583
- return [nextSnapshot, microsteps.flatMap(([, actions]) => actions)];
583
+ return [nextSnapshot, this._collectEffects(microsteps)];
584
+ }
585
+ _collectEffects(microsteps, initialActions = []) {
586
+ const effects = [...initialActions, ...microsteps.flatMap(([, actions]) => actions)];
587
+ return [...effects, ...dist_xstateActors.deriveDeferredStarts(effects)];
584
588
  }
585
589
  _transitionFast(snapshot, event, actorScope) {
586
590
  if (snapshot.status !== 'active' || typeof snapshot.value !== 'string' || this.root.always?.length) {
@@ -763,7 +767,7 @@ class StateMachine {
763
767
  microsteps = errorMacrostep.microsteps;
764
768
  initialActions = [];
765
769
  }
766
- return [macroState, [...initialActions, ...microsteps.flatMap(([, actions]) => actions)]];
770
+ return [macroState, this._collectEffects(microsteps, initialActions)];
767
771
  }
768
772
  start(snapshot) {
769
773
  // Start rehydrated children that were active when persisted. Freshly
@@ -864,7 +868,8 @@ class StateMachine {
864
868
  children,
865
869
  _nodes: nodes,
866
870
  value: snapshotData.value,
867
- historyValue: revivedHistoryValue
871
+ historyValue: revivedHistoryValue,
872
+ _stateInputs: snapshotData.stateInputs ?? {}
868
873
  }, this);
869
874
  const seen = new WeakSet();
870
875
  function reviveContext(contextPart) {
@@ -1,4 +1,4 @@
1
- import { P as ProcessingStatus, G as resolveReferencedActor, d as createActor, S as STATE_DELIMITER, H as mapValues, t as toArray, I as createInvokeId, J as getDelayedTransitions, K as evaluateCandidate, L as formatTransition, N as NULL_EVENT, M as toTransitionConfigArray, O as createInvokeTimeoutEvent, Q as getCandidates, R as formatRouteTransitions, U as resolveStateValue, V as getAllStateNodes, k as getStateNodes, W as createMachineSnapshot, Y as isInFinalState, e as macrostep, Z as cloneMachineSnapshot, _ as transitionNode, m as matchesEventDescriptor, $ as getTransitionResult, a0 as hasEffect, a1 as createErrorPlatformEvent, r as resolveActionsWithContext, f as createInitEvent, g as initialMicrostep, a2 as toStatePath, a3 as isStateId, a4 as getStateNodeByPath, a5 as getPersistedSnapshot, a6 as $$ACTOR_TYPE } from './index-b5a6f830.development.esm.js';
1
+ import { P as ProcessingStatus, H as resolveReferencedActor, e as createActor, S as STATE_DELIMITER, I as mapValues, t as toArray, J as createInvokeId, K as getDelayedTransitions, L as evaluateCandidate, M as formatTransition, N as NULL_EVENT, O as toTransitionConfigArray, Q as createInvokeTimeoutEvent, R as getCandidates, U as formatRouteTransitions, V as resolveStateValue, W as getAllStateNodes, l as getStateNodes, Y as createMachineSnapshot, Z as isInFinalState, f as macrostep, d as deriveDeferredStarts, _ as cloneMachineSnapshot, $ as transitionNode, m as matchesEventDescriptor, a0 as getTransitionResult, a1 as hasEffect, a2 as createErrorPlatformEvent, r as resolveActionsWithContext, g as createInitEvent, h as initialMicrostep, a3 as toStatePath, a4 as isStateId, a5 as getStateNodeByPath, a6 as getPersistedSnapshot, a7 as $$ACTOR_TYPE } from './index-dc3443b6.development.esm.js';
2
2
 
3
3
  function createSpawner(actorScope, {
4
4
  machine,
@@ -578,7 +578,11 @@ class StateMachine {
578
578
  snapshot: nextSnapshot,
579
579
  microsteps
580
580
  } = macrostep(snapshot, event, actorScope, []);
581
- return [nextSnapshot, microsteps.flatMap(([, actions]) => actions)];
581
+ return [nextSnapshot, this._collectEffects(microsteps)];
582
+ }
583
+ _collectEffects(microsteps, initialActions = []) {
584
+ const effects = [...initialActions, ...microsteps.flatMap(([, actions]) => actions)];
585
+ return [...effects, ...deriveDeferredStarts(effects)];
582
586
  }
583
587
  _transitionFast(snapshot, event, actorScope) {
584
588
  if (snapshot.status !== 'active' || typeof snapshot.value !== 'string' || this.root.always?.length) {
@@ -761,7 +765,7 @@ class StateMachine {
761
765
  microsteps = errorMacrostep.microsteps;
762
766
  initialActions = [];
763
767
  }
764
- return [macroState, [...initialActions, ...microsteps.flatMap(([, actions]) => actions)]];
768
+ return [macroState, this._collectEffects(microsteps, initialActions)];
765
769
  }
766
770
  start(snapshot) {
767
771
  // Start rehydrated children that were active when persisted. Freshly
@@ -862,7 +866,8 @@ class StateMachine {
862
866
  children,
863
867
  _nodes: nodes,
864
868
  value: snapshotData.value,
865
- historyValue: revivedHistoryValue
869
+ historyValue: revivedHistoryValue,
870
+ _stateInputs: snapshotData.stateInputs ?? {}
866
871
  }, this);
867
872
  const seen = new WeakSet();
868
873
  function reviveContext(contextPart) {
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var dist_xstateActors = require('./index-626fe0b0.cjs.js');
3
+ var dist_xstateActors = require('./index-42956779.cjs.js');
4
4
 
5
5
  function createSpawner(actorScope, {
6
6
  machine,
@@ -551,7 +551,11 @@ class StateMachine {
551
551
  snapshot: nextSnapshot,
552
552
  microsteps
553
553
  } = dist_xstateActors.macrostep(snapshot, event, actorScope, []);
554
- return [nextSnapshot, microsteps.flatMap(([, actions]) => actions)];
554
+ return [nextSnapshot, this._collectEffects(microsteps)];
555
+ }
556
+ _collectEffects(microsteps, initialActions = []) {
557
+ const effects = [...initialActions, ...microsteps.flatMap(([, actions]) => actions)];
558
+ return [...effects, ...dist_xstateActors.deriveDeferredStarts(effects)];
555
559
  }
556
560
  _transitionFast(snapshot, event, actorScope) {
557
561
  if (snapshot.status !== 'active' || typeof snapshot.value !== 'string' || this.root.always?.length) {
@@ -734,7 +738,7 @@ class StateMachine {
734
738
  microsteps = errorMacrostep.microsteps;
735
739
  initialActions = [];
736
740
  }
737
- return [macroState, [...initialActions, ...microsteps.flatMap(([, actions]) => actions)]];
741
+ return [macroState, this._collectEffects(microsteps, initialActions)];
738
742
  }
739
743
  start(snapshot) {
740
744
  // Start rehydrated children that were active when persisted. Freshly
@@ -832,7 +836,8 @@ class StateMachine {
832
836
  children,
833
837
  _nodes: nodes,
834
838
  value: snapshotData.value,
835
- historyValue: revivedHistoryValue
839
+ historyValue: revivedHistoryValue,
840
+ _stateInputs: snapshotData.stateInputs ?? {}
836
841
  }, this);
837
842
  const seen = new WeakSet();
838
843
  function reviveContext(contextPart) {
@@ -63,6 +63,7 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
63
63
  * @param event The received event
64
64
  */
65
65
  transition(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, event: TEvent, actorScope: ActorScope<typeof snapshot, TEvent, AnyActorSystem, TEmitted>): ActorLogicTransitionResult<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, ExecutableActionObjectFromLogic<this>>;
66
+ private _collectEffects;
66
67
  private _transitionFast;
67
68
  /**
68
69
  * Determines the next state given the current `state` and `event`. Calculates
@@ -1,5 +1,6 @@
1
1
  import { AnyActor, AnyActorScope, EventObject } from "./types.js";
2
2
  export declare const builtInActions: {
3
+ "@xstate.spawn": (_actor: AnyActor) => void;
3
4
  "@xstate.start": (actor: AnyActor) => void;
4
5
  "@xstate.raise": (actorScope: AnyActorScope, event: EventObject, options: {
5
6
  id?: string;
@@ -1,6 +1,12 @@
1
- import type { AnyAction, AnyActorScope, AnyEventObject, AnyMachineSnapshot, EnqueueObject, EventObject, ExecutableActionObject, MachineContext, SpecialExecutableAction } from "./types.js";
1
+ import type { AnyAction, AnyActorScope, AnyEventObject, AnyMachineSnapshot, EnqueueObject, EventObject, ExecutableActionObject, MachineContext, SpecialExecutableAction, StartExecutableActionObject } from "./types.js";
2
2
  export declare function mergeContextPatch(context: MachineContext, patch: MachineContext): MachineContext;
3
3
  export declare function createTransitionEnqueue(actorScope: AnyActorScope, actions: any[], internalEvents: EventObject[], actorSubscriptions?: boolean, createActors?: boolean): EnqueueObject<any, any>;
4
+ /**
5
+ * Attached (listener/subscription) starts are ordered before child starts so
6
+ * that a listener/subscription captures events emitted synchronously as its
7
+ * target actor starts.
8
+ */
9
+ export declare function deriveDeferredStarts(effects: ReadonlyArray<ExecutableActionObject>): StartExecutableActionObject[];
4
10
  export declare function isBuiltInExecutableAction(action: ExecutableActionObject): action is SpecialExecutableAction;
5
11
  export declare function resolveActionsWithContext(currentSnapshot: AnyMachineSnapshot, event: AnyEventObject, actorScope: AnyActorScope, actions: AnyAction[]): [AnyMachineSnapshot, ExecutableActionObject[]];
6
12
  export declare function createEnqueueObject(props: Partial<EnqueueObject<any, any>>, action: <T extends (...args: any[]) => any>(fn: T, ...args: Parameters<T>) => void): EnqueueObject<any, any>;
@@ -1052,13 +1052,19 @@ export interface BaseExecutableActionObject {
1052
1052
  export interface CustomExecutableActionObject<TType extends string = string & {}> extends BaseExecutableActionObject {
1053
1053
  type: TType;
1054
1054
  }
1055
- export interface StartExecutableActionObject extends BaseExecutableActionObject {
1056
- type: '@xstate.start';
1055
+ export interface SpawnExecutableActionObject extends BaseExecutableActionObject {
1056
+ type: '@xstate.spawn';
1057
1057
  actor: AnyActor;
1058
1058
  id: string;
1059
1059
  logic: AnyActorLogic;
1060
1060
  src: string | AnyActorLogic;
1061
1061
  input: unknown;
1062
+ args: Parameters<(typeof builtInActions)['@xstate.spawn']>;
1063
+ }
1064
+ export interface StartExecutableActionObject extends BaseExecutableActionObject {
1065
+ type: '@xstate.start';
1066
+ actor: AnyActor;
1067
+ id: string;
1062
1068
  args: Parameters<(typeof builtInActions)['@xstate.start']>;
1063
1069
  }
1064
1070
  export interface RaiseExecutableActionObject extends BaseExecutableActionObject {
@@ -1087,6 +1093,7 @@ export interface StopExecutableActionObject extends BaseExecutableActionObject {
1087
1093
  args: Parameters<(typeof builtInActions)['@xstate.stop']>;
1088
1094
  }
1089
1095
  export type BuiltInExecutableActionObject = Values<{
1096
+ '@xstate.spawn': SpawnExecutableActionObject;
1090
1097
  '@xstate.start': StartExecutableActionObject;
1091
1098
  '@xstate.raise': RaiseExecutableActionObject;
1092
1099
  '@xstate.sendTo': SendToExecutableActionObject;
@@ -1150,9 +1157,7 @@ export type EnqueueObject<TEvent extends EventObject, TEmittedEvent extends Even
1150
1157
  * @param mappers - Object with done/error/snapshot mappers, or a single
1151
1158
  * snapshot mapper function
1152
1159
  */
1153
- subscribeTo: {
1154
- <TSnapshot extends Snapshot<unknown>, TOutput, TMappedEvent extends TEvent>(actor: AnyActor, mappers: SubscribeToMappers<TSnapshot, TOutput, TMappedEvent> | ((snapshot: TSnapshot) => TMappedEvent)): AnyActor;
1155
- };
1160
+ subscribeTo: <TActor extends AnyActor, TMappedEvent extends TEvent>(actor: TActor, mappers: SubscribeToMappers<SnapshotFrom<TActor>, OutputFrom<TActor>, TMappedEvent> | ((snapshot: SnapshotFrom<TActor>) => TMappedEvent)) => AnyActor;
1156
1161
  };
1157
1162
  export type Action<TContext extends MachineContext, TEvent extends EventObject, TEmittedEvent extends EventObject, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays'], TParams = Record<string, unknown> | undefined, _TCtx = [TContext] extends [never] ? any : TContext> = (_: {
1158
1163
  context: _TCtx;
@@ -53,6 +53,8 @@ const NULL_EVENT = '';
53
53
  const STATE_IDENTIFIER = '#';
54
54
  const WILDCARD = '*';
55
55
  const XSTATE_INIT = '@xstate.init';
56
+ const XSTATE_START = '@xstate.start';
57
+ const XSTATE_SPAWN = '@xstate.spawn';
56
58
  const XSTATE_STOP = '@xstate.stop';
57
59
 
58
60
  /**
@@ -159,6 +161,9 @@ function reportUnhandledError(err) {
159
161
  const symbolObservable = (() => typeof Symbol === 'function' && Symbol.observable || '@@observable')();
160
162
 
161
163
  const builtInActions = {
164
+ // No-op: creation/registration are eager and pure; the start is a separate
165
+ // deferred `@xstate.start` effect (see `deriveDeferredStarts`).
166
+ ['@xstate.spawn']: _actor => {},
162
167
  ['@xstate.start']: actor => {
163
168
  actor.start();
164
169
  },
@@ -386,8 +391,8 @@ function unregisterChild(actor) {
386
391
  _special: true
387
392
  });
388
393
  }
389
- function pushStartedChild(actions, actor, id) {
390
- pushBuiltInAction(actions, builtInActions['@xstate.start'], actor);
394
+ function pushSpawnedChild(actions, actor, id) {
395
+ pushBuiltInAction(actions, builtInActions['@xstate.spawn'], actor);
391
396
  actions.push(registerSpawnedChild(actor, id ?? actor.id));
392
397
  }
393
398
  function createTransitionEnqueue(actorScope, actions, internalEvents, actorSubscriptions = false, createActors = true) {
@@ -422,7 +427,7 @@ function createTransitionEnqueue(actorScope, actions, internalEvents, actorSubsc
422
427
  ...options,
423
428
  parent: actorScope.self
424
429
  });
425
- pushStartedChild(actions, actor, options?.id);
430
+ pushSpawnedChild(actions, actor, options?.id);
426
431
  return actor;
427
432
  },
428
433
  sendTo: (actor, event, options) => {
@@ -454,7 +459,7 @@ function createTransitionEnqueue(actorScope, actions, internalEvents, actorSubsc
454
459
  input,
455
460
  parent: actorScope.self
456
461
  });
457
- pushBuiltInAction(actions, builtInActions['@xstate.start'], listenerActor);
462
+ pushBuiltInAction(actions, builtInActions['@xstate.spawn'], listenerActor);
458
463
  return listenerActor;
459
464
  },
460
465
  subscribeTo: (actor, mappers) => {
@@ -469,7 +474,7 @@ function createTransitionEnqueue(actorScope, actions, internalEvents, actorSubsc
469
474
  input,
470
475
  parent: actorScope.self
471
476
  });
472
- pushBuiltInAction(actions, builtInActions['@xstate.start'], subscriptionActor);
477
+ pushBuiltInAction(actions, builtInActions['@xstate.spawn'], subscriptionActor);
473
478
  return subscriptionActor;
474
479
  }
475
480
  });
@@ -480,7 +485,7 @@ function createTransitionEnqueue(actorScope, actions, internalEvents, actorSubsc
480
485
  }
481
486
  function getBuiltInActionFields(action, args) {
482
487
  switch (action) {
483
- case builtInActions['@xstate.start']:
488
+ case builtInActions['@xstate.spawn']:
484
489
  {
485
490
  const [actor] = args;
486
491
  return {
@@ -528,8 +533,65 @@ function getBuiltInActionFields(action, args) {
528
533
  return undefined;
529
534
  }
530
535
  }
536
+ function createStartEffect(actor) {
537
+ const action = builtInActions['@xstate.start'];
538
+ const args = [actor];
539
+ return {
540
+ type: XSTATE_START,
541
+ params: {
542
+ action,
543
+ args
544
+ },
545
+ args,
546
+ exec: action.bind(null, actor),
547
+ actor,
548
+ id: actor.id
549
+ };
550
+ }
551
+
552
+ /**
553
+ * Attached (listener/subscription) starts are ordered before child starts so
554
+ * that a listener/subscription captures events emitted synchronously as its
555
+ * target actor starts.
556
+ */
557
+ function deriveDeferredStarts(effects) {
558
+ const attachedStarts = [];
559
+ const childStarts = [];
560
+ for (const effect of effects) {
561
+ if (!isBuiltInExecutableAction(effect) || effect.type !== XSTATE_SPAWN) {
562
+ continue;
563
+ }
564
+ const {
565
+ actor,
566
+ logic
567
+ } = effect;
568
+ const start = createStartEffect(actor);
569
+ if (logic === listenerLogic || logic === subscriptionLogic) {
570
+ attachedStarts.push(start);
571
+ } else {
572
+ childStarts.push(start);
573
+ }
574
+ }
575
+ return [...attachedStarts, ...childStarts];
576
+ }
531
577
  function isBuiltInExecutableAction(action) {
532
- return Object.prototype.hasOwnProperty.call(builtInActions, action.type);
578
+ if (!Object.prototype.hasOwnProperty.call(builtInActions, action.type)) {
579
+ return false;
580
+ }
581
+ switch (action.type) {
582
+ case '@xstate.spawn':
583
+ return 'actor' in action && 'logic' in action && 'src' in action && 'input' in action;
584
+ case '@xstate.start':
585
+ case '@xstate.stop':
586
+ return 'actor' in action;
587
+ case '@xstate.raise':
588
+ case '@xstate.sendTo':
589
+ return 'event' in action;
590
+ case '@xstate.cancel':
591
+ return 'id' in action;
592
+ default:
593
+ return false;
594
+ }
533
595
  }
534
596
  function resolveActionsWithContext(currentSnapshot, event, actorScope, actions) {
535
597
  let intermediateSnapshot = currentSnapshot;
@@ -1612,7 +1674,7 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
1612
1674
  syncSnapshot: !!invokeDef.onSnapshot
1613
1675
  });
1614
1676
  actions.push({
1615
- action: builtInActions['@xstate.start'],
1677
+ action: builtInActions['@xstate.spawn'],
1616
1678
  args: [actor]
1617
1679
  });
1618
1680
  if (invokeDef.id) {
@@ -2160,6 +2222,9 @@ function getPersistedSnapshot(snapshot, options) {
2160
2222
  children: childrenJson,
2161
2223
  historyValue: serializeHistoryValue(jsonValues.historyValue)
2162
2224
  };
2225
+ if (_stateInputs && Object.keys(_stateInputs).length > 0) {
2226
+ persisted.stateInputs = _stateInputs;
2227
+ }
2163
2228
  if (machine.version !== undefined) {
2164
2229
  persisted.version = machine.version;
2165
2230
  }
@@ -3104,6 +3169,9 @@ class Actor {
3104
3169
  // Do not restart the service if it is already started
3105
3170
  return this;
3106
3171
  }
3172
+ if (this._processingStatus === ProcessingStatus.Stopped) {
3173
+ return this;
3174
+ }
3107
3175
  if (this._syncSnapshot) {
3108
3176
  this.subscribe({
3109
3177
  next: snapshot => {
@@ -4420,6 +4488,7 @@ exports.createMachineSnapshot = createMachineSnapshot;
4420
4488
  exports.createObservableLogic = createObservableLogic;
4421
4489
  exports.createSubscriptionLogic = createSubscriptionLogic;
4422
4490
  exports.createTransitionEnqueue = createTransitionEnqueue;
4491
+ exports.deriveDeferredStarts = deriveDeferredStarts;
4423
4492
  exports.evaluateCandidate = evaluateCandidate;
4424
4493
  exports.formatRouteTransitions = formatRouteTransitions;
4425
4494
  exports.formatTransition = formatTransition;
@@ -53,6 +53,8 @@ const NULL_EVENT = '';
53
53
  const STATE_IDENTIFIER = '#';
54
54
  const WILDCARD = '*';
55
55
  const XSTATE_INIT = '@xstate.init';
56
+ const XSTATE_START = '@xstate.start';
57
+ const XSTATE_SPAWN = '@xstate.spawn';
56
58
  const XSTATE_STOP = '@xstate.stop';
57
59
 
58
60
  /**
@@ -159,6 +161,9 @@ function reportUnhandledError(err) {
159
161
  const symbolObservable = (() => typeof Symbol === 'function' && Symbol.observable || '@@observable')();
160
162
 
161
163
  const builtInActions = {
164
+ // No-op: creation/registration are eager and pure; the start is a separate
165
+ // deferred `@xstate.start` effect (see `deriveDeferredStarts`).
166
+ ['@xstate.spawn']: _actor => {},
162
167
  ['@xstate.start']: actor => {
163
168
  actor.start();
164
169
  },
@@ -387,8 +392,8 @@ function unregisterChild(actor) {
387
392
  _special: true
388
393
  });
389
394
  }
390
- function pushStartedChild(actions, actor, id) {
391
- pushBuiltInAction(actions, builtInActions['@xstate.start'], actor);
395
+ function pushSpawnedChild(actions, actor, id) {
396
+ pushBuiltInAction(actions, builtInActions['@xstate.spawn'], actor);
392
397
  actions.push(registerSpawnedChild(actor, id ?? actor.id));
393
398
  }
394
399
  function createTransitionEnqueue(actorScope, actions, internalEvents, actorSubscriptions = false, createActors = true) {
@@ -423,7 +428,7 @@ function createTransitionEnqueue(actorScope, actions, internalEvents, actorSubsc
423
428
  ...options,
424
429
  parent: actorScope.self
425
430
  });
426
- pushStartedChild(actions, actor, options?.id);
431
+ pushSpawnedChild(actions, actor, options?.id);
427
432
  return actor;
428
433
  },
429
434
  sendTo: (actor, event, options) => {
@@ -455,7 +460,7 @@ function createTransitionEnqueue(actorScope, actions, internalEvents, actorSubsc
455
460
  input,
456
461
  parent: actorScope.self
457
462
  });
458
- pushBuiltInAction(actions, builtInActions['@xstate.start'], listenerActor);
463
+ pushBuiltInAction(actions, builtInActions['@xstate.spawn'], listenerActor);
459
464
  return listenerActor;
460
465
  },
461
466
  subscribeTo: (actor, mappers) => {
@@ -470,7 +475,7 @@ function createTransitionEnqueue(actorScope, actions, internalEvents, actorSubsc
470
475
  input,
471
476
  parent: actorScope.self
472
477
  });
473
- pushBuiltInAction(actions, builtInActions['@xstate.start'], subscriptionActor);
478
+ pushBuiltInAction(actions, builtInActions['@xstate.spawn'], subscriptionActor);
474
479
  return subscriptionActor;
475
480
  }
476
481
  });
@@ -481,7 +486,7 @@ function createTransitionEnqueue(actorScope, actions, internalEvents, actorSubsc
481
486
  }
482
487
  function getBuiltInActionFields(action, args) {
483
488
  switch (action) {
484
- case builtInActions['@xstate.start']:
489
+ case builtInActions['@xstate.spawn']:
485
490
  {
486
491
  const [actor] = args;
487
492
  return {
@@ -529,8 +534,65 @@ function getBuiltInActionFields(action, args) {
529
534
  return undefined;
530
535
  }
531
536
  }
537
+ function createStartEffect(actor) {
538
+ const action = builtInActions['@xstate.start'];
539
+ const args = [actor];
540
+ return {
541
+ type: XSTATE_START,
542
+ params: {
543
+ action,
544
+ args
545
+ },
546
+ args,
547
+ exec: action.bind(null, actor),
548
+ actor,
549
+ id: actor.id
550
+ };
551
+ }
552
+
553
+ /**
554
+ * Attached (listener/subscription) starts are ordered before child starts so
555
+ * that a listener/subscription captures events emitted synchronously as its
556
+ * target actor starts.
557
+ */
558
+ function deriveDeferredStarts(effects) {
559
+ const attachedStarts = [];
560
+ const childStarts = [];
561
+ for (const effect of effects) {
562
+ if (!isBuiltInExecutableAction(effect) || effect.type !== XSTATE_SPAWN) {
563
+ continue;
564
+ }
565
+ const {
566
+ actor,
567
+ logic
568
+ } = effect;
569
+ const start = createStartEffect(actor);
570
+ if (logic === listenerLogic || logic === subscriptionLogic) {
571
+ attachedStarts.push(start);
572
+ } else {
573
+ childStarts.push(start);
574
+ }
575
+ }
576
+ return [...attachedStarts, ...childStarts];
577
+ }
532
578
  function isBuiltInExecutableAction(action) {
533
- return Object.prototype.hasOwnProperty.call(builtInActions, action.type);
579
+ if (!Object.prototype.hasOwnProperty.call(builtInActions, action.type)) {
580
+ return false;
581
+ }
582
+ switch (action.type) {
583
+ case '@xstate.spawn':
584
+ return 'actor' in action && 'logic' in action && 'src' in action && 'input' in action;
585
+ case '@xstate.start':
586
+ case '@xstate.stop':
587
+ return 'actor' in action;
588
+ case '@xstate.raise':
589
+ case '@xstate.sendTo':
590
+ return 'event' in action;
591
+ case '@xstate.cancel':
592
+ return 'id' in action;
593
+ default:
594
+ return false;
595
+ }
534
596
  }
535
597
  function resolveActionsWithContext(currentSnapshot, event, actorScope, actions) {
536
598
  let intermediateSnapshot = currentSnapshot;
@@ -1623,7 +1685,7 @@ function microstep(transitions, currentSnapshot, actorScope, event, isInitial, i
1623
1685
  syncSnapshot: !!invokeDef.onSnapshot
1624
1686
  });
1625
1687
  actions.push({
1626
- action: builtInActions['@xstate.start'],
1688
+ action: builtInActions['@xstate.spawn'],
1627
1689
  args: [actor]
1628
1690
  });
1629
1691
  if (invokeDef.id) {
@@ -2177,6 +2239,9 @@ function getPersistedSnapshot(snapshot, options) {
2177
2239
  children: childrenJson,
2178
2240
  historyValue: serializeHistoryValue(jsonValues.historyValue)
2179
2241
  };
2242
+ if (_stateInputs && Object.keys(_stateInputs).length > 0) {
2243
+ persisted.stateInputs = _stateInputs;
2244
+ }
2180
2245
  if (machine.version !== undefined) {
2181
2246
  persisted.version = machine.version;
2182
2247
  }
@@ -3130,6 +3195,9 @@ class Actor {
3130
3195
  // Do not restart the service if it is already started
3131
3196
  return this;
3132
3197
  }
3198
+ if (this._processingStatus === ProcessingStatus.Stopped) {
3199
+ return this;
3200
+ }
3133
3201
  if (this._syncSnapshot) {
3134
3202
  this.subscribe({
3135
3203
  next: snapshot => {
@@ -4459,6 +4527,7 @@ exports.createMachineSnapshot = createMachineSnapshot;
4459
4527
  exports.createObservableLogic = createObservableLogic;
4460
4528
  exports.createSubscriptionLogic = createSubscriptionLogic;
4461
4529
  exports.createTransitionEnqueue = createTransitionEnqueue;
4530
+ exports.deriveDeferredStarts = deriveDeferredStarts;
4462
4531
  exports.evaluateCandidate = evaluateCandidate;
4463
4532
  exports.formatRouteTransitions = formatRouteTransitions;
4464
4533
  exports.formatTransition = formatTransition;