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
@@ -1222,6 +1222,18 @@ function copySnapshotActorRef(source, target) {
1222
1222
  return false;
1223
1223
  }
1224
1224
 
1225
+ /** Refreshes the only topology change made by an otherwise idle root start. */
1226
+ function refreshSnapshotActorRefRoot(snapshot, actor, system) {
1227
+ const ref = snapshotActorRefs.get(snapshot);
1228
+ const sourceVersion = getSystemSnapshotVersion(system);
1229
+ if (ref?.actor !== actor || ref.systemState.sourceSystem !== system || ref.systemState.sourceVersion + 1 !== sourceVersion || system._getRootActor?.() !== actor || system._peekChildren?.()) {
1230
+ return false;
1231
+ }
1232
+ ref.systemState.root = actor;
1233
+ ref.systemState.sourceVersion = sourceVersion;
1234
+ return true;
1235
+ }
1236
+
1225
1237
  /**
1226
1238
  * Associates a transition snapshot with an actor and an immutable system view.
1227
1239
  * The internal association is excluded from enumeration and persistence.
@@ -3605,36 +3617,41 @@ function matchesEventDescriptor(eventType, descriptor) {
3605
3617
  return true;
3606
3618
  }
3607
3619
 
3608
- let nextFallbackSystemId = 0;
3620
+ let systemIdPrefix;
3621
+ let nextSystemId = 0;
3609
3622
 
3610
3623
  /** @internal */
3611
3624
  const transitionEffectSignal = new Error('Transition effect');
3612
3625
  /** @internal */
3613
3626
  const transitionEffectTargets = [];
3614
- function createSystemId() {
3627
+ function createSystemIdPrefix() {
3615
3628
  let crypto;
3616
3629
  try {
3617
3630
  crypto = globalThis.crypto;
3618
3631
  } catch {
3619
3632
  // Use the process-local fallback below.
3620
3633
  }
3621
- if (crypto?.randomUUID) {
3634
+ if (crypto?.getRandomValues) {
3622
3635
  try {
3623
- return crypto.randomUUID();
3636
+ const values = new Uint32Array(4);
3637
+ crypto.getRandomValues(values);
3638
+ return Array.from(values, value => value.toString(36).padStart(7, '0')).join('');
3624
3639
  } catch {
3625
- // Try getRandomValues next.
3640
+ // Try randomUUID next.
3626
3641
  }
3627
3642
  }
3628
- if (crypto?.getRandomValues) {
3643
+ if (crypto?.randomUUID) {
3629
3644
  try {
3630
- const values = new Uint32Array(4);
3631
- crypto.getRandomValues(values);
3632
- return Array.from(values, value => value.toString(36)).join('-');
3645
+ return crypto.randomUUID().replaceAll('-', '');
3633
3646
  } catch {
3634
3647
  // Use the process-local fallback below.
3635
3648
  }
3636
3649
  }
3637
- return `xstate-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}-${nextFallbackSystemId++}`;
3650
+ return `xstate-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
3651
+ }
3652
+ function createSystemId() {
3653
+ systemIdPrefix ??= createSystemIdPrefix();
3654
+ return `${systemIdPrefix}:${(nextSystemId++).toString(36)}`;
3638
3655
  }
3639
3656
 
3640
3657
  /** @internal */
@@ -3668,9 +3685,14 @@ function bookSessionId(system) {
3668
3685
  * system provides the local in-memory implementation.
3669
3686
  */
3670
3687
 
3688
+ const emptyScheduledTimers = Object.freeze({});
3671
3689
  function createScheduledTimerId(actor, id) {
3672
3690
  return `${actor.sessionId}.${id}`;
3673
3691
  }
3692
+
3693
+ // These optional lazy fields intentionally have no emitted initializers.
3694
+ // oxlint-disable-next-line typescript/no-unsafe-declaration-merging
3695
+
3674
3696
  class RuntimeSystem {
3675
3697
  get children() {
3676
3698
  const children = this._children ??= new Map();
@@ -3707,9 +3729,6 @@ class RuntimeSystem {
3707
3729
  }
3708
3730
  constructor(_rootActor, options) {
3709
3731
  this._rootActor = _rootActor;
3710
- this._children = void 0;
3711
- this._keyedActors = void 0;
3712
- this._reverseKeyedActors = void 0;
3713
3732
  this._identity = {
3714
3733
  systemId: createSystemId(),
3715
3734
  nextSessionId: 0
@@ -3720,14 +3739,12 @@ class RuntimeSystem {
3720
3739
  this._clock = void 0;
3721
3740
  this._logger = void 0;
3722
3741
  this.createActorRef = void 0;
3723
- this._inspectionObservers = void 0;
3724
- this._timerMap = void 0;
3725
3742
  const restoredSnapshot = typeof options.snapshot === 'object' && options.snapshot !== null ? options.snapshot : undefined;
3726
3743
  this._clock = options.clock;
3727
3744
  this._logger = options.logger;
3728
3745
  this.createActorRef = options.createActorRef;
3729
3746
  this._snapshot = {
3730
- _scheduledTimers: restoredSnapshot?.scheduler ?? {},
3747
+ _scheduledTimers: restoredSnapshot?.scheduler ?? emptyScheduledTimers,
3731
3748
  _nextActorId: restoredSnapshot?._nextActorId ?? 0
3732
3749
  };
3733
3750
  }
@@ -3768,6 +3785,9 @@ class RuntimeSystem {
3768
3785
  dueAt: scheduledAt + delay
3769
3786
  };
3770
3787
  const scheduledTimerId = createScheduledTimerId(source, id);
3788
+ if (this._snapshot._scheduledTimers === emptyScheduledTimers) {
3789
+ this._snapshot._scheduledTimers = {};
3790
+ }
3771
3791
  this._snapshot._scheduledTimers[scheduledTimerId] = scheduledTimer;
3772
3792
  markSystemSnapshotDirty(this);
3773
3793
  const timeout = this._clock.setTimeout(() => {
@@ -3789,7 +3809,9 @@ class RuntimeSystem {
3789
3809
  if (this._timerMap) {
3790
3810
  delete this._timerMap[scheduledTimerId];
3791
3811
  }
3792
- delete this._snapshot._scheduledTimers[scheduledTimerId];
3812
+ if (this._snapshot._scheduledTimers !== emptyScheduledTimers) {
3813
+ delete this._snapshot._scheduledTimers[scheduledTimerId];
3814
+ }
3793
3815
  markSystemSnapshotDirty(this);
3794
3816
  if (timeout !== undefined) {
3795
3817
  this._clock.clearTimeout(timeout);
@@ -3927,8 +3949,12 @@ class RuntimeSystem {
3927
3949
  }
3928
3950
  start() {
3929
3951
  const scheduledTimers = this._snapshot._scheduledTimers;
3930
- this._snapshot._scheduledTimers = {};
3952
+ let resetScheduledTimers = true;
3931
3953
  for (const scheduledId in scheduledTimers) {
3954
+ if (resetScheduledTimers) {
3955
+ this._snapshot._scheduledTimers = {};
3956
+ resetScheduledTimers = false;
3957
+ }
3932
3958
  const {
3933
3959
  source,
3934
3960
  dueAt,
@@ -3958,7 +3984,7 @@ let ProcessingStatus = /*#__PURE__*/function (ProcessingStatus) {
3958
3984
  ProcessingStatus[ProcessingStatus["Stopped"] = 2] = "Stopped";
3959
3985
  return ProcessingStatus;
3960
3986
  }({});
3961
- const defaultOptions = {
3987
+ const defaultOptions = Object.freeze({
3962
3988
  clock: {
3963
3989
  setTimeout: (fn, ms) => {
3964
3990
  return setTimeout(fn, ms);
@@ -3968,7 +3994,7 @@ const defaultOptions = {
3968
3994
  }
3969
3995
  },
3970
3996
  logger: console.log.bind(console)
3971
- };
3997
+ });
3972
3998
  function safeCall(fn, arg) {
3973
3999
  try {
3974
4000
  fn?.(arg);
@@ -3987,42 +4013,6 @@ function executeExecutableEffects(effects, actorScope) {
3987
4013
  function createActorRef(logic, options) {
3988
4014
  return new Actor(logic, options);
3989
4015
  }
3990
- class RuntimeActorScope {
3991
- constructor(actor) {
3992
- this.actor = actor;
3993
- this._defer = void 0;
3994
- this._stopChild = void 0;
3995
- this._emit = void 0;
3996
- this._actionExecutor = void 0;
3997
- }
3998
- get self() {
3999
- return this.actor;
4000
- }
4001
- get id() {
4002
- return this.actor.id;
4003
- }
4004
- get sessionId() {
4005
- return this.actor.sessionId;
4006
- }
4007
- get logger() {
4008
- return this.actor._getLogger();
4009
- }
4010
- get system() {
4011
- return this.actor.system;
4012
- }
4013
- get defer() {
4014
- return this._defer ??= fn => this.actor._defer(fn);
4015
- }
4016
- get stopChild() {
4017
- return this._stopChild ??= child => this.actor._stopChild(child);
4018
- }
4019
- get emit() {
4020
- return this._emit ??= event => this.actor.system.emitEvent(this.actor, event);
4021
- }
4022
- get actionExecutor() {
4023
- return this._actionExecutor ??= action => this.actor._executeAction(action);
4024
- }
4025
- }
4026
4016
 
4027
4017
  /**
4028
4018
  * An Actor is a running process that can receive events, send events and change
@@ -4105,10 +4095,10 @@ class Actor {
4105
4095
  // array of functions to defer
4106
4096
  this._deferred = void 0;
4107
4097
  this._restored = false;
4108
- const resolvedOptions = {
4098
+ const resolvedOptions = options ? {
4109
4099
  ...defaultOptions,
4110
4100
  ...options
4111
- };
4101
+ } : defaultOptions;
4112
4102
  const {
4113
4103
  clock,
4114
4104
  logger,
@@ -4140,7 +4130,7 @@ class Actor {
4140
4130
  this.options = resolvedOptions;
4141
4131
  this.src = resolvedOptions.src ?? logic;
4142
4132
  this.ref = this;
4143
- this._actorScope = new RuntimeActorScope(this);
4133
+ this._actorScope = this;
4144
4134
  if (registryKey) {
4145
4135
  this.registryKey = registryKey;
4146
4136
  this.system._set(registryKey, this);
@@ -4160,7 +4150,7 @@ class Actor {
4160
4150
  } else {
4161
4151
  const [snapshot, effects] = finalizeTransitionResult(this._actorScope, undefined, this.logic.initialTransition(this.options?.input, this._actorScope));
4162
4152
  this._setSnapshot(snapshot);
4163
- this._initialEffects = effects;
4153
+ this._initialEffects = effects.length ? effects : undefined;
4164
4154
  }
4165
4155
  } catch (err) {
4166
4156
  // if we get here then it means that we assign a value to this._snapshot that is not of the correct type
@@ -4199,6 +4189,37 @@ class Actor {
4199
4189
  _getLogger() {
4200
4190
  return this.logger;
4201
4191
  }
4192
+ get self() {
4193
+ return this;
4194
+ }
4195
+ get defer() {
4196
+ const defer = fn => this._defer(fn);
4197
+ Object.defineProperty(this, 'defer', {
4198
+ value: defer
4199
+ });
4200
+ return defer;
4201
+ }
4202
+ get stopChild() {
4203
+ const stopChild = child => this._stopChild(child);
4204
+ Object.defineProperty(this, 'stopChild', {
4205
+ value: stopChild
4206
+ });
4207
+ return stopChild;
4208
+ }
4209
+ get emit() {
4210
+ const emit = event => this.system.emitEvent(this, event);
4211
+ Object.defineProperty(this, 'emit', {
4212
+ value: emit
4213
+ });
4214
+ return emit;
4215
+ }
4216
+ get actionExecutor() {
4217
+ const actionExecutor = action => this._executeAction(action);
4218
+ Object.defineProperty(this, 'actionExecutor', {
4219
+ value: actionExecutor
4220
+ });
4221
+ return actionExecutor;
4222
+ }
4202
4223
 
4203
4224
  /** @internal */
4204
4225
  _isRunning() {
@@ -4344,7 +4365,7 @@ class Actor {
4344
4365
  this._collectedSent = undefined;
4345
4366
  }
4346
4367
  _flushInitialEffects() {
4347
- if (!this._initialEffects) {
4368
+ if (!this._initialEffects?.length) {
4348
4369
  return true;
4349
4370
  }
4350
4371
  this._forceDeferredActions = true;
@@ -4514,10 +4535,6 @@ class Actor {
4514
4535
  this.system._set(this.registryKey, this);
4515
4536
  }
4516
4537
  this._processingStatus = ProcessingStatus.Running;
4517
-
4518
- // TODO: this isn't correct when rehydrating
4519
- const initEvent = createInitEvent(this.options.input);
4520
- // remember source of init as parent for unified transition event
4521
4538
  this._lastSourceRef = this._parent;
4522
4539
  const status = this._snapshot.status;
4523
4540
  switch (status) {
@@ -4535,7 +4552,7 @@ class Actor {
4535
4552
  if (!this._flushInitialEffects()) {
4536
4553
  return this;
4537
4554
  }
4538
- this.update(this._snapshot, initEvent);
4555
+ this.update(this._snapshot, createInitEvent(this.options.input));
4539
4556
  // TODO: rethink cleanup of observers, mailbox, etc
4540
4557
  return this;
4541
4558
  case 'error':
@@ -4559,11 +4576,18 @@ class Actor {
4559
4576
  return this;
4560
4577
  }
4561
4578
  }
4562
-
4563
- // TODO: this notifies all subscribers but usually this is redundant
4564
- // there is no real change happening here
4565
- // we need to rethink if this needs to be refactored
4566
- this.update(this._snapshot, initEvent);
4579
+ if (!this._restored && !this._deferred?.length && !this.observers?.size && !(this.system._hasInspectionObservers?.() ?? true)) {
4580
+ // Starting changes the registered system view associated with the
4581
+ // snapshot, even when there is nothing to publish or execute.
4582
+ if (!refreshSnapshotActorRefRoot(this._snapshot, this, this.system)) {
4583
+ this._setSnapshot(this._snapshot);
4584
+ }
4585
+ this._collectedMicrosteps = undefined;
4586
+ this._collectedActions = undefined;
4587
+ this._collectedSent = undefined;
4588
+ } else {
4589
+ this.update(this._snapshot, createInitEvent(this.options.input));
4590
+ }
4567
4591
  if (this._restored) {
4568
4592
  const timers = this._snapshot.timers ?? {};
4569
4593
  for (const timer of Object.values(timers)) {
@@ -1224,6 +1224,18 @@ function copySnapshotActorRef(source, target) {
1224
1224
  return false;
1225
1225
  }
1226
1226
 
1227
+ /** Refreshes the only topology change made by an otherwise idle root start. */
1228
+ function refreshSnapshotActorRefRoot(snapshot, actor, system) {
1229
+ const ref = snapshotActorRefs.get(snapshot);
1230
+ const sourceVersion = getSystemSnapshotVersion(system);
1231
+ if (ref?.actor !== actor || ref.systemState.sourceSystem !== system || ref.systemState.sourceVersion + 1 !== sourceVersion || system._getRootActor?.() !== actor || system._peekChildren?.()) {
1232
+ return false;
1233
+ }
1234
+ ref.systemState.root = actor;
1235
+ ref.systemState.sourceVersion = sourceVersion;
1236
+ return true;
1237
+ }
1238
+
1227
1239
  /**
1228
1240
  * Associates a transition snapshot with an actor and an immutable system view.
1229
1241
  * The internal association is excluded from enumeration and persistence.
@@ -3607,36 +3619,41 @@ function matchesEventDescriptor(eventType, descriptor) {
3607
3619
  return true;
3608
3620
  }
3609
3621
 
3610
- let nextFallbackSystemId = 0;
3622
+ let systemIdPrefix;
3623
+ let nextSystemId = 0;
3611
3624
 
3612
3625
  /** @internal */
3613
3626
  const transitionEffectSignal = new Error('Transition effect');
3614
3627
  /** @internal */
3615
3628
  const transitionEffectTargets = [];
3616
- function createSystemId() {
3629
+ function createSystemIdPrefix() {
3617
3630
  let crypto;
3618
3631
  try {
3619
3632
  crypto = globalThis.crypto;
3620
3633
  } catch {
3621
3634
  // Use the process-local fallback below.
3622
3635
  }
3623
- if (crypto?.randomUUID) {
3636
+ if (crypto?.getRandomValues) {
3624
3637
  try {
3625
- return crypto.randomUUID();
3638
+ const values = new Uint32Array(4);
3639
+ crypto.getRandomValues(values);
3640
+ return Array.from(values, value => value.toString(36).padStart(7, '0')).join('');
3626
3641
  } catch {
3627
- // Try getRandomValues next.
3642
+ // Try randomUUID next.
3628
3643
  }
3629
3644
  }
3630
- if (crypto?.getRandomValues) {
3645
+ if (crypto?.randomUUID) {
3631
3646
  try {
3632
- const values = new Uint32Array(4);
3633
- crypto.getRandomValues(values);
3634
- return Array.from(values, value => value.toString(36)).join('-');
3647
+ return crypto.randomUUID().replaceAll('-', '');
3635
3648
  } catch {
3636
3649
  // Use the process-local fallback below.
3637
3650
  }
3638
3651
  }
3639
- return `xstate-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}-${nextFallbackSystemId++}`;
3652
+ return `xstate-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
3653
+ }
3654
+ function createSystemId() {
3655
+ systemIdPrefix ??= createSystemIdPrefix();
3656
+ return `${systemIdPrefix}:${(nextSystemId++).toString(36)}`;
3640
3657
  }
3641
3658
 
3642
3659
  /** @internal */
@@ -3670,9 +3687,14 @@ function bookSessionId(system) {
3670
3687
  * system provides the local in-memory implementation.
3671
3688
  */
3672
3689
 
3690
+ const emptyScheduledTimers = Object.freeze({});
3673
3691
  function createScheduledTimerId(actor, id) {
3674
3692
  return `${actor.sessionId}.${id}`;
3675
3693
  }
3694
+
3695
+ // These optional lazy fields intentionally have no emitted initializers.
3696
+ // oxlint-disable-next-line typescript/no-unsafe-declaration-merging
3697
+
3676
3698
  class RuntimeSystem {
3677
3699
  get children() {
3678
3700
  const children = this._children ??= new Map();
@@ -3709,9 +3731,6 @@ class RuntimeSystem {
3709
3731
  }
3710
3732
  constructor(_rootActor, options) {
3711
3733
  this._rootActor = _rootActor;
3712
- this._children = void 0;
3713
- this._keyedActors = void 0;
3714
- this._reverseKeyedActors = void 0;
3715
3734
  this._identity = {
3716
3735
  systemId: createSystemId(),
3717
3736
  nextSessionId: 0
@@ -3722,14 +3741,12 @@ class RuntimeSystem {
3722
3741
  this._clock = void 0;
3723
3742
  this._logger = void 0;
3724
3743
  this.createActorRef = void 0;
3725
- this._inspectionObservers = void 0;
3726
- this._timerMap = void 0;
3727
3744
  const restoredSnapshot = typeof options.snapshot === 'object' && options.snapshot !== null ? options.snapshot : undefined;
3728
3745
  this._clock = options.clock;
3729
3746
  this._logger = options.logger;
3730
3747
  this.createActorRef = options.createActorRef;
3731
3748
  this._snapshot = {
3732
- _scheduledTimers: restoredSnapshot?.scheduler ?? {},
3749
+ _scheduledTimers: restoredSnapshot?.scheduler ?? emptyScheduledTimers,
3733
3750
  _nextActorId: restoredSnapshot?._nextActorId ?? 0
3734
3751
  };
3735
3752
  }
@@ -3770,6 +3787,9 @@ class RuntimeSystem {
3770
3787
  dueAt: scheduledAt + delay
3771
3788
  };
3772
3789
  const scheduledTimerId = createScheduledTimerId(source, id);
3790
+ if (this._snapshot._scheduledTimers === emptyScheduledTimers) {
3791
+ this._snapshot._scheduledTimers = {};
3792
+ }
3773
3793
  this._snapshot._scheduledTimers[scheduledTimerId] = scheduledTimer;
3774
3794
  markSystemSnapshotDirty(this);
3775
3795
  const timeout = this._clock.setTimeout(() => {
@@ -3791,7 +3811,9 @@ class RuntimeSystem {
3791
3811
  if (this._timerMap) {
3792
3812
  delete this._timerMap[scheduledTimerId];
3793
3813
  }
3794
- delete this._snapshot._scheduledTimers[scheduledTimerId];
3814
+ if (this._snapshot._scheduledTimers !== emptyScheduledTimers) {
3815
+ delete this._snapshot._scheduledTimers[scheduledTimerId];
3816
+ }
3795
3817
  markSystemSnapshotDirty(this);
3796
3818
  if (timeout !== undefined) {
3797
3819
  this._clock.clearTimeout(timeout);
@@ -3929,8 +3951,12 @@ class RuntimeSystem {
3929
3951
  }
3930
3952
  start() {
3931
3953
  const scheduledTimers = this._snapshot._scheduledTimers;
3932
- this._snapshot._scheduledTimers = {};
3954
+ let resetScheduledTimers = true;
3933
3955
  for (const scheduledId in scheduledTimers) {
3956
+ if (resetScheduledTimers) {
3957
+ this._snapshot._scheduledTimers = {};
3958
+ resetScheduledTimers = false;
3959
+ }
3934
3960
  const {
3935
3961
  source,
3936
3962
  dueAt,
@@ -3960,7 +3986,7 @@ let ProcessingStatus = /*#__PURE__*/function (ProcessingStatus) {
3960
3986
  ProcessingStatus[ProcessingStatus["Stopped"] = 2] = "Stopped";
3961
3987
  return ProcessingStatus;
3962
3988
  }({});
3963
- const defaultOptions = {
3989
+ const defaultOptions = Object.freeze({
3964
3990
  clock: {
3965
3991
  setTimeout: (fn, ms) => {
3966
3992
  return setTimeout(fn, ms);
@@ -3970,7 +3996,7 @@ const defaultOptions = {
3970
3996
  }
3971
3997
  },
3972
3998
  logger: console.log.bind(console)
3973
- };
3999
+ });
3974
4000
  function safeCall(fn, arg) {
3975
4001
  try {
3976
4002
  fn?.(arg);
@@ -3989,42 +4015,6 @@ function executeExecutableEffects(effects, actorScope) {
3989
4015
  function createActorRef(logic, options) {
3990
4016
  return new Actor(logic, options);
3991
4017
  }
3992
- class RuntimeActorScope {
3993
- constructor(actor) {
3994
- this.actor = actor;
3995
- this._defer = void 0;
3996
- this._stopChild = void 0;
3997
- this._emit = void 0;
3998
- this._actionExecutor = void 0;
3999
- }
4000
- get self() {
4001
- return this.actor;
4002
- }
4003
- get id() {
4004
- return this.actor.id;
4005
- }
4006
- get sessionId() {
4007
- return this.actor.sessionId;
4008
- }
4009
- get logger() {
4010
- return this.actor._getLogger();
4011
- }
4012
- get system() {
4013
- return this.actor.system;
4014
- }
4015
- get defer() {
4016
- return this._defer ??= fn => this.actor._defer(fn);
4017
- }
4018
- get stopChild() {
4019
- return this._stopChild ??= child => this.actor._stopChild(child);
4020
- }
4021
- get emit() {
4022
- return this._emit ??= event => this.actor.system.emitEvent(this.actor, event);
4023
- }
4024
- get actionExecutor() {
4025
- return this._actionExecutor ??= action => this.actor._executeAction(action);
4026
- }
4027
- }
4028
4018
 
4029
4019
  /**
4030
4020
  * An Actor is a running process that can receive events, send events and change
@@ -4107,10 +4097,10 @@ class Actor {
4107
4097
  // array of functions to defer
4108
4098
  this._deferred = void 0;
4109
4099
  this._restored = false;
4110
- const resolvedOptions = {
4100
+ const resolvedOptions = options ? {
4111
4101
  ...defaultOptions,
4112
4102
  ...options
4113
- };
4103
+ } : defaultOptions;
4114
4104
  const {
4115
4105
  clock,
4116
4106
  logger,
@@ -4142,7 +4132,7 @@ class Actor {
4142
4132
  this.options = resolvedOptions;
4143
4133
  this.src = resolvedOptions.src ?? logic;
4144
4134
  this.ref = this;
4145
- this._actorScope = new RuntimeActorScope(this);
4135
+ this._actorScope = this;
4146
4136
  if (registryKey) {
4147
4137
  this.registryKey = registryKey;
4148
4138
  this.system._set(registryKey, this);
@@ -4162,7 +4152,7 @@ class Actor {
4162
4152
  } else {
4163
4153
  const [snapshot, effects] = finalizeTransitionResult(this._actorScope, undefined, this.logic.initialTransition(this.options?.input, this._actorScope));
4164
4154
  this._setSnapshot(snapshot);
4165
- this._initialEffects = effects;
4155
+ this._initialEffects = effects.length ? effects : undefined;
4166
4156
  }
4167
4157
  } catch (err) {
4168
4158
  // if we get here then it means that we assign a value to this._snapshot that is not of the correct type
@@ -4201,6 +4191,37 @@ class Actor {
4201
4191
  _getLogger() {
4202
4192
  return this.logger;
4203
4193
  }
4194
+ get self() {
4195
+ return this;
4196
+ }
4197
+ get defer() {
4198
+ const defer = fn => this._defer(fn);
4199
+ Object.defineProperty(this, 'defer', {
4200
+ value: defer
4201
+ });
4202
+ return defer;
4203
+ }
4204
+ get stopChild() {
4205
+ const stopChild = child => this._stopChild(child);
4206
+ Object.defineProperty(this, 'stopChild', {
4207
+ value: stopChild
4208
+ });
4209
+ return stopChild;
4210
+ }
4211
+ get emit() {
4212
+ const emit = event => this.system.emitEvent(this, event);
4213
+ Object.defineProperty(this, 'emit', {
4214
+ value: emit
4215
+ });
4216
+ return emit;
4217
+ }
4218
+ get actionExecutor() {
4219
+ const actionExecutor = action => this._executeAction(action);
4220
+ Object.defineProperty(this, 'actionExecutor', {
4221
+ value: actionExecutor
4222
+ });
4223
+ return actionExecutor;
4224
+ }
4204
4225
 
4205
4226
  /** @internal */
4206
4227
  _isRunning() {
@@ -4346,7 +4367,7 @@ class Actor {
4346
4367
  this._collectedSent = undefined;
4347
4368
  }
4348
4369
  _flushInitialEffects() {
4349
- if (!this._initialEffects) {
4370
+ if (!this._initialEffects?.length) {
4350
4371
  return true;
4351
4372
  }
4352
4373
  this._forceDeferredActions = true;
@@ -4516,10 +4537,6 @@ class Actor {
4516
4537
  this.system._set(this.registryKey, this);
4517
4538
  }
4518
4539
  this._processingStatus = ProcessingStatus.Running;
4519
-
4520
- // TODO: this isn't correct when rehydrating
4521
- const initEvent = createInitEvent(this.options.input);
4522
- // remember source of init as parent for unified transition event
4523
4540
  this._lastSourceRef = this._parent;
4524
4541
  const status = this._snapshot.status;
4525
4542
  switch (status) {
@@ -4537,7 +4554,7 @@ class Actor {
4537
4554
  if (!this._flushInitialEffects()) {
4538
4555
  return this;
4539
4556
  }
4540
- this.update(this._snapshot, initEvent);
4557
+ this.update(this._snapshot, createInitEvent(this.options.input));
4541
4558
  // TODO: rethink cleanup of observers, mailbox, etc
4542
4559
  return this;
4543
4560
  case 'error':
@@ -4561,11 +4578,18 @@ class Actor {
4561
4578
  return this;
4562
4579
  }
4563
4580
  }
4564
-
4565
- // TODO: this notifies all subscribers but usually this is redundant
4566
- // there is no real change happening here
4567
- // we need to rethink if this needs to be refactored
4568
- this.update(this._snapshot, initEvent);
4581
+ if (!this._restored && !this._deferred?.length && !this.observers?.size && !(this.system._hasInspectionObservers?.() ?? true)) {
4582
+ // Starting changes the registered system view associated with the
4583
+ // snapshot, even when there is nothing to publish or execute.
4584
+ if (!refreshSnapshotActorRefRoot(this._snapshot, this, this.system)) {
4585
+ this._setSnapshot(this._snapshot);
4586
+ }
4587
+ this._collectedMicrosteps = undefined;
4588
+ this._collectedActions = undefined;
4589
+ this._collectedSent = undefined;
4590
+ } else {
4591
+ this.update(this._snapshot, createInitEvent(this.options.input));
4592
+ }
4569
4593
  if (this._restored) {
4570
4594
  const timers = this._snapshot.timers ?? {};
4571
4595
  for (const timer of Object.values(timers)) {