xstate 6.0.0-alpha.34 → 6.0.0-alpha.35

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 (28) hide show
  1. package/dist/{StateMachine-b827f6a8.development.cjs.js → StateMachine-41dc03be.development.cjs.js} +12 -4
  2. package/dist/{StateMachine-b72b23cc.development.esm.js → StateMachine-8dd1e4a0.development.esm.js} +12 -4
  3. package/dist/{StateMachine-269b22a3.esm.js → StateMachine-9d68f525.esm.js} +12 -4
  4. package/dist/{StateMachine-204661ea.cjs.js → StateMachine-c6fc9d29.cjs.js} +12 -4
  5. package/dist/declarations/src/createActor.d.ts +5 -0
  6. package/dist/{index-692b05ac.development.cjs.js → index-0acba787.development.cjs.js} +95 -71
  7. package/dist/{index-16832816.esm.js → index-9146dea1.esm.js} +95 -71
  8. package/dist/{index-b440472d.cjs.js → index-9683e24d.cjs.js} +95 -71
  9. package/dist/{index-ea9227d3.development.esm.js → index-9d364b4a.development.esm.js} +95 -71
  10. package/dist/xstate-actors.cjs.js +1 -1
  11. package/dist/xstate-actors.development.cjs.js +1 -1
  12. package/dist/xstate-actors.development.esm.js +1 -1
  13. package/dist/xstate-actors.esm.js +1 -1
  14. package/dist/xstate-actors.umd.min.js +1 -1
  15. package/dist/xstate-actors.umd.min.js.map +1 -1
  16. package/dist/xstate-graph.cjs.js +2 -2
  17. package/dist/xstate-graph.development.cjs.js +2 -2
  18. package/dist/xstate-graph.development.esm.js +2 -2
  19. package/dist/xstate-graph.esm.js +2 -2
  20. package/dist/xstate-graph.umd.min.js +1 -1
  21. package/dist/xstate-graph.umd.min.js.map +1 -1
  22. package/dist/xstate.cjs.js +2 -2
  23. package/dist/xstate.development.cjs.js +2 -2
  24. package/dist/xstate.development.esm.js +4 -4
  25. package/dist/xstate.esm.js +4 -4
  26. package/dist/xstate.umd.min.js +1 -1
  27. package/dist/xstate.umd.min.js.map +1 -1
  28. package/package.json +1 -1
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var dist_xstateActors = require('./index-692b05ac.development.cjs.js');
3
+ var dist_xstateActors = require('./index-0acba787.development.cjs.js');
4
4
 
5
5
  function createSpawner(actorScope, actors, spawnedChildren) {
6
6
  return (src, options) => {
@@ -631,10 +631,13 @@ class StateMachine {
631
631
  _collectEffects(microsteps) {
632
632
  return microsteps.flatMap(([, actions]) => actions);
633
633
  }
634
- _attachPureActorRef(snapshot, actorScope) {
634
+ _attachPureActorRef(snapshot, actorScope, skipInitializingActor = false) {
635
635
  if (dist_xstateActors.isInertActorScope(actorScope)) {
636
636
  return snapshot;
637
637
  }
638
+ if (skipInitializingActor && actorScope.self._actorScope === actorScope && actorScope.self._snapshot === undefined) {
639
+ return snapshot;
640
+ }
638
641
  dist_xstateActors.setSnapshotActorRef(snapshot, actorScope.self, actorScope.system);
639
642
  return snapshot;
640
643
  }
@@ -816,7 +819,7 @@ class StateMachine {
816
819
  if (usesInertScope) {
817
820
  dist_xstateActors.setInertActorScopeSnapshot(resolvedActorScope, macroState, false);
818
821
  }
819
- const returnedSnapshot = usesInertScope ? dist_xstateActors.attachSnapshotActorRef(resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope);
822
+ const returnedSnapshot = usesInertScope ? dist_xstateActors.attachSnapshotActorRef(resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope, true);
820
823
  const effects = this._collectEffects(microsteps);
821
824
  if (this.validator) {
822
825
  dist_xstateActors.assertValid(this.validator, {
@@ -851,7 +854,12 @@ class StateMachine {
851
854
  if (!snapshot?.children) {
852
855
  return;
853
856
  }
854
- for (const child of Object.values(snapshot.children)) {
857
+ const children = snapshot.children;
858
+ for (const childId in children) {
859
+ if (!Object.hasOwn(children, childId)) {
860
+ continue;
861
+ }
862
+ const child = children[childId];
855
863
  if (child._rehydrated && child.getSnapshot?.().status === 'active') {
856
864
  child.start();
857
865
  }
@@ -1,4 +1,4 @@
1
- import { U as STATE_DELIMITER, V as mapValues, t as toArray, W as createInvokeId, Y as getDelayedTransitions, Z as getEventDescriptorKey, _ as getCandidates, $ as evaluateCandidate, a0 as formatTransition, a1 as NULL_EVENT, a2 as toTransitionConfigArray, a3 as createInvokeTimeoutEventId, a4 as matchesActorSession, a5 as formatRouteTransitions, a6 as resolveStateValue, a7 as getAllStateNodes, B as getStateNodes, a8 as createMachineSnapshot, a9 as isInFinalState, k as createInertActorScope, aa as assertValid, s as setInertActorScopeSnapshot, l as attachSnapshotActorRef, n as macrostep, ab as isInertActorScope, ac as setSnapshotActorRef, ad as cloneMachineSnapshot, ae as transitionNode, e as XSTATE_TIMER, m as matchesEventDescriptor, af as getTransitionResult, ag as hasEffect, ah as createErrorPlatformEvent, ai as withActorSelf, r as resolveActionsWithContext, o as createInitEvent, q as createSpawnEffect, u as initialMicrostep, aj as toStatePath, ak as isStateId, al as getStateNodeByPath, am as getPersistedSnapshot, an as resolveReferencedActor, ao as createInvokeTimeoutEvent, ap as $$ACTOR_TYPE, j as createActor } from './index-ea9227d3.development.esm.js';
1
+ import { U as STATE_DELIMITER, V as mapValues, t as toArray, W as createInvokeId, Y as getDelayedTransitions, Z as getEventDescriptorKey, _ as getCandidates, $ as evaluateCandidate, a0 as formatTransition, a1 as NULL_EVENT, a2 as toTransitionConfigArray, a3 as createInvokeTimeoutEventId, a4 as matchesActorSession, a5 as formatRouteTransitions, a6 as resolveStateValue, a7 as getAllStateNodes, B as getStateNodes, a8 as createMachineSnapshot, a9 as isInFinalState, k as createInertActorScope, aa as assertValid, s as setInertActorScopeSnapshot, l as attachSnapshotActorRef, n as macrostep, ab as isInertActorScope, ac as setSnapshotActorRef, ad as cloneMachineSnapshot, ae as transitionNode, e as XSTATE_TIMER, m as matchesEventDescriptor, af as getTransitionResult, ag as hasEffect, ah as createErrorPlatformEvent, ai as withActorSelf, r as resolveActionsWithContext, o as createInitEvent, q as createSpawnEffect, u as initialMicrostep, aj as toStatePath, ak as isStateId, al as getStateNodeByPath, am as getPersistedSnapshot, an as resolveReferencedActor, ao as createInvokeTimeoutEvent, ap as $$ACTOR_TYPE, j as createActor } from './index-9d364b4a.development.esm.js';
2
2
 
3
3
  function createSpawner(actorScope, actors, spawnedChildren) {
4
4
  return (src, options) => {
@@ -629,10 +629,13 @@ class StateMachine {
629
629
  _collectEffects(microsteps) {
630
630
  return microsteps.flatMap(([, actions]) => actions);
631
631
  }
632
- _attachPureActorRef(snapshot, actorScope) {
632
+ _attachPureActorRef(snapshot, actorScope, skipInitializingActor = false) {
633
633
  if (isInertActorScope(actorScope)) {
634
634
  return snapshot;
635
635
  }
636
+ if (skipInitializingActor && actorScope.self._actorScope === actorScope && actorScope.self._snapshot === undefined) {
637
+ return snapshot;
638
+ }
636
639
  setSnapshotActorRef(snapshot, actorScope.self, actorScope.system);
637
640
  return snapshot;
638
641
  }
@@ -814,7 +817,7 @@ class StateMachine {
814
817
  if (usesInertScope) {
815
818
  setInertActorScopeSnapshot(resolvedActorScope, macroState, false);
816
819
  }
817
- const returnedSnapshot = usesInertScope ? attachSnapshotActorRef(resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope);
820
+ const returnedSnapshot = usesInertScope ? attachSnapshotActorRef(resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope, true);
818
821
  const effects = this._collectEffects(microsteps);
819
822
  if (this.validator) {
820
823
  assertValid(this.validator, {
@@ -849,7 +852,12 @@ class StateMachine {
849
852
  if (!snapshot?.children) {
850
853
  return;
851
854
  }
852
- for (const child of Object.values(snapshot.children)) {
855
+ const children = snapshot.children;
856
+ for (const childId in children) {
857
+ if (!Object.hasOwn(children, childId)) {
858
+ continue;
859
+ }
860
+ const child = children[childId];
853
861
  if (child._rehydrated && child.getSnapshot?.().status === 'active') {
854
862
  child.start();
855
863
  }
@@ -1,4 +1,4 @@
1
- import { U as STATE_DELIMITER, V as mapValues, t as toArray, W as createInvokeId, Y as getDelayedTransitions, Z as getEventDescriptorKey, _ as getCandidates, $ as evaluateCandidate, a0 as formatTransition, a1 as NULL_EVENT, a2 as toTransitionConfigArray, a3 as createInvokeTimeoutEventId, a4 as matchesActorSession, a5 as formatRouteTransitions, a6 as resolveStateValue, a7 as getAllStateNodes, B as getStateNodes, a8 as createMachineSnapshot, a9 as isInFinalState, k as createInertActorScope, aa as assertValid, s as setInertActorScopeSnapshot, l as attachSnapshotActorRef, n as macrostep, ab as isInertActorScope, ac as setSnapshotActorRef, ad as cloneMachineSnapshot, ae as transitionNode, e as XSTATE_TIMER, m as matchesEventDescriptor, af as getTransitionResult, ag as hasEffect, ah as createErrorPlatformEvent, ai as withActorSelf, r as resolveActionsWithContext, o as createInitEvent, q as createSpawnEffect, u as initialMicrostep, aj as toStatePath, ak as isStateId, al as getStateNodeByPath, am as getPersistedSnapshot, an as resolveReferencedActor, ao as createInvokeTimeoutEvent, ap as $$ACTOR_TYPE, j as createActor } from './index-16832816.esm.js';
1
+ import { U as STATE_DELIMITER, V as mapValues, t as toArray, W as createInvokeId, Y as getDelayedTransitions, Z as getEventDescriptorKey, _ as getCandidates, $ as evaluateCandidate, a0 as formatTransition, a1 as NULL_EVENT, a2 as toTransitionConfigArray, a3 as createInvokeTimeoutEventId, a4 as matchesActorSession, a5 as formatRouteTransitions, a6 as resolveStateValue, a7 as getAllStateNodes, B as getStateNodes, a8 as createMachineSnapshot, a9 as isInFinalState, k as createInertActorScope, aa as assertValid, s as setInertActorScopeSnapshot, l as attachSnapshotActorRef, n as macrostep, ab as isInertActorScope, ac as setSnapshotActorRef, ad as cloneMachineSnapshot, ae as transitionNode, e as XSTATE_TIMER, m as matchesEventDescriptor, af as getTransitionResult, ag as hasEffect, ah as createErrorPlatformEvent, ai as withActorSelf, r as resolveActionsWithContext, o as createInitEvent, q as createSpawnEffect, u as initialMicrostep, aj as toStatePath, ak as isStateId, al as getStateNodeByPath, am as getPersistedSnapshot, an as resolveReferencedActor, ao as createInvokeTimeoutEvent, ap as $$ACTOR_TYPE, j as createActor } from './index-9146dea1.esm.js';
2
2
 
3
3
  function createSpawner(actorScope, actors, spawnedChildren) {
4
4
  return (src, options) => {
@@ -600,10 +600,13 @@ class StateMachine {
600
600
  _collectEffects(microsteps) {
601
601
  return microsteps.flatMap(([, actions]) => actions);
602
602
  }
603
- _attachPureActorRef(snapshot, actorScope) {
603
+ _attachPureActorRef(snapshot, actorScope, skipInitializingActor = false) {
604
604
  if (isInertActorScope(actorScope)) {
605
605
  return snapshot;
606
606
  }
607
+ if (skipInitializingActor && actorScope.self._actorScope === actorScope && actorScope.self._snapshot === undefined) {
608
+ return snapshot;
609
+ }
607
610
  setSnapshotActorRef(snapshot, actorScope.self, actorScope.system);
608
611
  return snapshot;
609
612
  }
@@ -785,7 +788,7 @@ class StateMachine {
785
788
  if (usesInertScope) {
786
789
  setInertActorScopeSnapshot(resolvedActorScope, macroState, false);
787
790
  }
788
- const returnedSnapshot = usesInertScope ? attachSnapshotActorRef(resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope);
791
+ const returnedSnapshot = usesInertScope ? attachSnapshotActorRef(resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope, true);
789
792
  const effects = this._collectEffects(microsteps);
790
793
  if (this.validator) {
791
794
  assertValid(this.validator, {
@@ -820,7 +823,12 @@ class StateMachine {
820
823
  if (!snapshot?.children) {
821
824
  return;
822
825
  }
823
- for (const child of Object.values(snapshot.children)) {
826
+ const children = snapshot.children;
827
+ for (const childId in children) {
828
+ if (!Object.hasOwn(children, childId)) {
829
+ continue;
830
+ }
831
+ const child = children[childId];
824
832
  if (child._rehydrated && child.getSnapshot?.().status === 'active') {
825
833
  child.start();
826
834
  }
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var dist_xstateActors = require('./index-b440472d.cjs.js');
3
+ var dist_xstateActors = require('./index-9683e24d.cjs.js');
4
4
 
5
5
  function createSpawner(actorScope, actors, spawnedChildren) {
6
6
  return (src, options) => {
@@ -602,10 +602,13 @@ class StateMachine {
602
602
  _collectEffects(microsteps) {
603
603
  return microsteps.flatMap(([, actions]) => actions);
604
604
  }
605
- _attachPureActorRef(snapshot, actorScope) {
605
+ _attachPureActorRef(snapshot, actorScope, skipInitializingActor = false) {
606
606
  if (dist_xstateActors.isInertActorScope(actorScope)) {
607
607
  return snapshot;
608
608
  }
609
+ if (skipInitializingActor && actorScope.self._actorScope === actorScope && actorScope.self._snapshot === undefined) {
610
+ return snapshot;
611
+ }
609
612
  dist_xstateActors.setSnapshotActorRef(snapshot, actorScope.self, actorScope.system);
610
613
  return snapshot;
611
614
  }
@@ -787,7 +790,7 @@ class StateMachine {
787
790
  if (usesInertScope) {
788
791
  dist_xstateActors.setInertActorScopeSnapshot(resolvedActorScope, macroState, false);
789
792
  }
790
- const returnedSnapshot = usesInertScope ? dist_xstateActors.attachSnapshotActorRef(resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope);
793
+ const returnedSnapshot = usesInertScope ? dist_xstateActors.attachSnapshotActorRef(resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope, true);
791
794
  const effects = this._collectEffects(microsteps);
792
795
  if (this.validator) {
793
796
  dist_xstateActors.assertValid(this.validator, {
@@ -822,7 +825,12 @@ class StateMachine {
822
825
  if (!snapshot?.children) {
823
826
  return;
824
827
  }
825
- for (const child of Object.values(snapshot.children)) {
828
+ const children = snapshot.children;
829
+ for (const childId in children) {
830
+ if (!Object.hasOwn(children, childId)) {
831
+ continue;
832
+ }
833
+ const child = children[childId];
826
834
  if (child._rehydrated && child.getSnapshot?.().status === 'active') {
827
835
  child.start();
828
836
  }
@@ -61,6 +61,11 @@ export declare class Actor<TLogic extends AnyActorLogic> implements ActorInstanc
61
61
  constructor(logic: TLogic, options?: ActorOptions<TLogic>);
62
62
  private _deferred;
63
63
  private _restored;
64
+ private get self();
65
+ private get defer();
66
+ private get stopChild();
67
+ private get emit();
68
+ private get actionExecutor();
64
69
  /** Associates each live snapshot with this actor for later pure transitions. */
65
70
  private _setSnapshot;
66
71
  private _setErrorSnapshot;
@@ -1225,6 +1225,18 @@ function copySnapshotActorRef(source, target) {
1225
1225
  return false;
1226
1226
  }
1227
1227
 
1228
+ /** Refreshes the only topology change made by an otherwise idle root start. */
1229
+ function refreshSnapshotActorRefRoot(snapshot, actor, system) {
1230
+ const ref = snapshotActorRefs.get(snapshot);
1231
+ const sourceVersion = getSystemSnapshotVersion(system);
1232
+ if (ref?.actor !== actor || ref.systemState.sourceSystem !== system || ref.systemState.sourceVersion + 1 !== sourceVersion || system._getRootActor?.() !== actor || system._peekChildren?.()) {
1233
+ return false;
1234
+ }
1235
+ ref.systemState.root = actor;
1236
+ ref.systemState.sourceVersion = sourceVersion;
1237
+ return true;
1238
+ }
1239
+
1228
1240
  /**
1229
1241
  * Associates a transition snapshot with an actor and an immutable system view.
1230
1242
  * The internal association is excluded from enumeration and persistence.
@@ -3633,36 +3645,41 @@ function matchesEventDescriptor(eventType, descriptor) {
3633
3645
  return true;
3634
3646
  }
3635
3647
 
3636
- let nextFallbackSystemId = 0;
3648
+ let systemIdPrefix;
3649
+ let nextSystemId = 0;
3637
3650
 
3638
3651
  /** @internal */
3639
3652
  const transitionEffectSignal = new Error('Transition effect');
3640
3653
  /** @internal */
3641
3654
  const transitionEffectTargets = [];
3642
- function createSystemId() {
3655
+ function createSystemIdPrefix() {
3643
3656
  let crypto;
3644
3657
  try {
3645
3658
  crypto = globalThis.crypto;
3646
3659
  } catch {
3647
3660
  // Use the process-local fallback below.
3648
3661
  }
3649
- if (crypto?.randomUUID) {
3662
+ if (crypto?.getRandomValues) {
3650
3663
  try {
3651
- return crypto.randomUUID();
3664
+ const values = new Uint32Array(4);
3665
+ crypto.getRandomValues(values);
3666
+ return Array.from(values, value => value.toString(36).padStart(7, '0')).join('');
3652
3667
  } catch {
3653
- // Try getRandomValues next.
3668
+ // Try randomUUID next.
3654
3669
  }
3655
3670
  }
3656
- if (crypto?.getRandomValues) {
3671
+ if (crypto?.randomUUID) {
3657
3672
  try {
3658
- const values = new Uint32Array(4);
3659
- crypto.getRandomValues(values);
3660
- return Array.from(values, value => value.toString(36)).join('-');
3673
+ return crypto.randomUUID().replaceAll('-', '');
3661
3674
  } catch {
3662
3675
  // Use the process-local fallback below.
3663
3676
  }
3664
3677
  }
3665
- return `xstate-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}-${nextFallbackSystemId++}`;
3678
+ return `xstate-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
3679
+ }
3680
+ function createSystemId() {
3681
+ systemIdPrefix ??= createSystemIdPrefix();
3682
+ return `${systemIdPrefix}:${(nextSystemId++).toString(36)}`;
3666
3683
  }
3667
3684
 
3668
3685
  /** @internal */
@@ -3696,9 +3713,14 @@ function bookSessionId(system) {
3696
3713
  * system provides the local in-memory implementation.
3697
3714
  */
3698
3715
 
3716
+ const emptyScheduledTimers = Object.freeze({});
3699
3717
  function createScheduledTimerId(actor, id) {
3700
3718
  return `${actor.sessionId}.${id}`;
3701
3719
  }
3720
+
3721
+ // These optional lazy fields intentionally have no emitted initializers.
3722
+ // oxlint-disable-next-line typescript/no-unsafe-declaration-merging
3723
+
3702
3724
  class RuntimeSystem {
3703
3725
  get children() {
3704
3726
  const children = this._children ??= new Map();
@@ -3735,9 +3757,6 @@ class RuntimeSystem {
3735
3757
  }
3736
3758
  constructor(_rootActor, options) {
3737
3759
  this._rootActor = _rootActor;
3738
- this._children = void 0;
3739
- this._keyedActors = void 0;
3740
- this._reverseKeyedActors = void 0;
3741
3760
  this._identity = {
3742
3761
  systemId: createSystemId(),
3743
3762
  nextSessionId: 0
@@ -3748,14 +3767,12 @@ class RuntimeSystem {
3748
3767
  this._clock = void 0;
3749
3768
  this._logger = void 0;
3750
3769
  this.createActorRef = void 0;
3751
- this._inspectionObservers = void 0;
3752
- this._timerMap = void 0;
3753
3770
  const restoredSnapshot = typeof options.snapshot === 'object' && options.snapshot !== null ? options.snapshot : undefined;
3754
3771
  this._clock = options.clock;
3755
3772
  this._logger = options.logger;
3756
3773
  this.createActorRef = options.createActorRef;
3757
3774
  this._snapshot = {
3758
- _scheduledTimers: restoredSnapshot?.scheduler ?? {},
3775
+ _scheduledTimers: restoredSnapshot?.scheduler ?? emptyScheduledTimers,
3759
3776
  _nextActorId: restoredSnapshot?._nextActorId ?? 0
3760
3777
  };
3761
3778
  }
@@ -3796,6 +3813,9 @@ class RuntimeSystem {
3796
3813
  dueAt: scheduledAt + delay
3797
3814
  };
3798
3815
  const scheduledTimerId = createScheduledTimerId(source, id);
3816
+ if (this._snapshot._scheduledTimers === emptyScheduledTimers) {
3817
+ this._snapshot._scheduledTimers = {};
3818
+ }
3799
3819
  this._snapshot._scheduledTimers[scheduledTimerId] = scheduledTimer;
3800
3820
  markSystemSnapshotDirty(this);
3801
3821
  const timeout = this._clock.setTimeout(() => {
@@ -3817,7 +3837,9 @@ class RuntimeSystem {
3817
3837
  if (this._timerMap) {
3818
3838
  delete this._timerMap[scheduledTimerId];
3819
3839
  }
3820
- delete this._snapshot._scheduledTimers[scheduledTimerId];
3840
+ if (this._snapshot._scheduledTimers !== emptyScheduledTimers) {
3841
+ delete this._snapshot._scheduledTimers[scheduledTimerId];
3842
+ }
3821
3843
  markSystemSnapshotDirty(this);
3822
3844
  if (timeout !== undefined) {
3823
3845
  this._clock.clearTimeout(timeout);
@@ -3955,8 +3977,12 @@ class RuntimeSystem {
3955
3977
  }
3956
3978
  start() {
3957
3979
  const scheduledTimers = this._snapshot._scheduledTimers;
3958
- this._snapshot._scheduledTimers = {};
3980
+ let resetScheduledTimers = true;
3959
3981
  for (const scheduledId in scheduledTimers) {
3982
+ if (resetScheduledTimers) {
3983
+ this._snapshot._scheduledTimers = {};
3984
+ resetScheduledTimers = false;
3985
+ }
3960
3986
  const {
3961
3987
  source,
3962
3988
  dueAt,
@@ -3986,7 +4012,7 @@ let ProcessingStatus = /*#__PURE__*/function (ProcessingStatus) {
3986
4012
  ProcessingStatus[ProcessingStatus["Stopped"] = 2] = "Stopped";
3987
4013
  return ProcessingStatus;
3988
4014
  }({});
3989
- const defaultOptions = {
4015
+ const defaultOptions = Object.freeze({
3990
4016
  clock: {
3991
4017
  setTimeout: (fn, ms) => {
3992
4018
  return setTimeout(fn, ms);
@@ -3996,7 +4022,7 @@ const defaultOptions = {
3996
4022
  }
3997
4023
  },
3998
4024
  logger: console.log.bind(console)
3999
- };
4025
+ });
4000
4026
  function safeCall(fn, arg) {
4001
4027
  try {
4002
4028
  fn?.(arg);
@@ -4015,42 +4041,6 @@ function executeExecutableEffects(effects, actorScope) {
4015
4041
  function createActorRef(logic, options) {
4016
4042
  return new Actor(logic, options);
4017
4043
  }
4018
- class RuntimeActorScope {
4019
- constructor(actor) {
4020
- this.actor = actor;
4021
- this._defer = void 0;
4022
- this._stopChild = void 0;
4023
- this._emit = void 0;
4024
- this._actionExecutor = void 0;
4025
- }
4026
- get self() {
4027
- return this.actor;
4028
- }
4029
- get id() {
4030
- return this.actor.id;
4031
- }
4032
- get sessionId() {
4033
- return this.actor.sessionId;
4034
- }
4035
- get logger() {
4036
- return this.actor._getLogger();
4037
- }
4038
- get system() {
4039
- return this.actor.system;
4040
- }
4041
- get defer() {
4042
- return this._defer ??= fn => this.actor._defer(fn);
4043
- }
4044
- get stopChild() {
4045
- return this._stopChild ??= child => this.actor._stopChild(child);
4046
- }
4047
- get emit() {
4048
- return this._emit ??= event => this.actor.system.emitEvent(this.actor, event);
4049
- }
4050
- get actionExecutor() {
4051
- return this._actionExecutor ??= action => this.actor._executeAction(action);
4052
- }
4053
- }
4054
4044
 
4055
4045
  /**
4056
4046
  * An Actor is a running process that can receive events, send events and change
@@ -4133,10 +4123,10 @@ class Actor {
4133
4123
  // array of functions to defer
4134
4124
  this._deferred = void 0;
4135
4125
  this._restored = false;
4136
- const resolvedOptions = {
4126
+ const resolvedOptions = options ? {
4137
4127
  ...defaultOptions,
4138
4128
  ...options
4139
- };
4129
+ } : defaultOptions;
4140
4130
  const {
4141
4131
  clock,
4142
4132
  logger,
@@ -4168,7 +4158,7 @@ class Actor {
4168
4158
  this.options = resolvedOptions;
4169
4159
  this.src = resolvedOptions.src ?? logic;
4170
4160
  this.ref = this;
4171
- this._actorScope = new RuntimeActorScope(this);
4161
+ this._actorScope = this;
4172
4162
  if (registryKey) {
4173
4163
  this.registryKey = registryKey;
4174
4164
  this.system._set(registryKey, this);
@@ -4188,7 +4178,7 @@ class Actor {
4188
4178
  } else {
4189
4179
  const [snapshot, effects] = finalizeTransitionResult(this._actorScope, undefined, this.logic.initialTransition(this.options?.input, this._actorScope));
4190
4180
  this._setSnapshot(snapshot);
4191
- this._initialEffects = effects;
4181
+ this._initialEffects = effects.length ? effects : undefined;
4192
4182
  }
4193
4183
  } catch (err) {
4194
4184
  // if we get here then it means that we assign a value to this._snapshot that is not of the correct type
@@ -4227,6 +4217,37 @@ class Actor {
4227
4217
  _getLogger() {
4228
4218
  return this.logger;
4229
4219
  }
4220
+ get self() {
4221
+ return this;
4222
+ }
4223
+ get defer() {
4224
+ const defer = fn => this._defer(fn);
4225
+ Object.defineProperty(this, 'defer', {
4226
+ value: defer
4227
+ });
4228
+ return defer;
4229
+ }
4230
+ get stopChild() {
4231
+ const stopChild = child => this._stopChild(child);
4232
+ Object.defineProperty(this, 'stopChild', {
4233
+ value: stopChild
4234
+ });
4235
+ return stopChild;
4236
+ }
4237
+ get emit() {
4238
+ const emit = event => this.system.emitEvent(this, event);
4239
+ Object.defineProperty(this, 'emit', {
4240
+ value: emit
4241
+ });
4242
+ return emit;
4243
+ }
4244
+ get actionExecutor() {
4245
+ const actionExecutor = action => this._executeAction(action);
4246
+ Object.defineProperty(this, 'actionExecutor', {
4247
+ value: actionExecutor
4248
+ });
4249
+ return actionExecutor;
4250
+ }
4230
4251
 
4231
4252
  /** @internal */
4232
4253
  _isRunning() {
@@ -4372,7 +4393,7 @@ class Actor {
4372
4393
  this._collectedSent = undefined;
4373
4394
  }
4374
4395
  _flushInitialEffects() {
4375
- if (!this._initialEffects) {
4396
+ if (!this._initialEffects?.length) {
4376
4397
  return true;
4377
4398
  }
4378
4399
  this._forceDeferredActions = true;
@@ -4542,10 +4563,6 @@ class Actor {
4542
4563
  this.system._set(this.registryKey, this);
4543
4564
  }
4544
4565
  this._processingStatus = ProcessingStatus.Running;
4545
-
4546
- // TODO: this isn't correct when rehydrating
4547
- const initEvent = createInitEvent(this.options.input);
4548
- // remember source of init as parent for unified transition event
4549
4566
  this._lastSourceRef = this._parent;
4550
4567
  const status = this._snapshot.status;
4551
4568
  switch (status) {
@@ -4563,7 +4580,7 @@ class Actor {
4563
4580
  if (!this._flushInitialEffects()) {
4564
4581
  return this;
4565
4582
  }
4566
- this.update(this._snapshot, initEvent);
4583
+ this.update(this._snapshot, createInitEvent(this.options.input));
4567
4584
  // TODO: rethink cleanup of observers, mailbox, etc
4568
4585
  return this;
4569
4586
  case 'error':
@@ -4587,11 +4604,18 @@ class Actor {
4587
4604
  return this;
4588
4605
  }
4589
4606
  }
4590
-
4591
- // TODO: this notifies all subscribers but usually this is redundant
4592
- // there is no real change happening here
4593
- // we need to rethink if this needs to be refactored
4594
- this.update(this._snapshot, initEvent);
4607
+ if (!this._restored && !this._deferred?.length && !this.observers?.size && !(this.system._hasInspectionObservers?.() ?? true)) {
4608
+ // Starting changes the registered system view associated with the
4609
+ // snapshot, even when there is nothing to publish or execute.
4610
+ if (!refreshSnapshotActorRefRoot(this._snapshot, this, this.system)) {
4611
+ this._setSnapshot(this._snapshot);
4612
+ }
4613
+ this._collectedMicrosteps = undefined;
4614
+ this._collectedActions = undefined;
4615
+ this._collectedSent = undefined;
4616
+ } else {
4617
+ this.update(this._snapshot, createInitEvent(this.options.input));
4618
+ }
4595
4619
  if (this._restored) {
4596
4620
  const timers = this._snapshot.timers ?? {};
4597
4621
  for (const timer of Object.values(timers)) {