xstate 5.0.1 → 5.1.0

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 (43) hide show
  1. package/actions/dist/xstate-actions.cjs.js +2 -2
  2. package/actions/dist/xstate-actions.development.cjs.js +2 -2
  3. package/actions/dist/xstate-actions.development.esm.js +2 -2
  4. package/actions/dist/xstate-actions.esm.js +2 -2
  5. package/actions/dist/xstate-actions.umd.min.js +1 -1
  6. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  7. package/actors/dist/xstate-actors.cjs.js +1 -2
  8. package/actors/dist/xstate-actors.development.cjs.js +1 -2
  9. package/actors/dist/xstate-actors.development.esm.js +1 -2
  10. package/actors/dist/xstate-actors.esm.js +1 -2
  11. package/actors/dist/xstate-actors.umd.min.js +1 -1
  12. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  13. package/dist/declarations/src/SimulatedClock.d.ts +3 -1
  14. package/dist/declarations/src/actions/send.d.ts +1 -1
  15. package/dist/declarations/src/actors/callback.d.ts +3 -2
  16. package/dist/declarations/src/actors/observable.d.ts +2 -1
  17. package/dist/declarations/src/actors/promise.d.ts +3 -15
  18. package/dist/declarations/src/actors/transition.d.ts +3 -2
  19. package/dist/declarations/src/index.d.ts +1 -1
  20. package/dist/declarations/src/interpreter.d.ts +4 -8
  21. package/dist/declarations/src/setup.d.ts +6 -4
  22. package/dist/declarations/src/system.d.ts +32 -2
  23. package/dist/declarations/src/types.d.ts +20 -24
  24. package/dist/{log-f196f85f.development.esm.js → log-2cbae8e5.development.esm.js} +12 -8
  25. package/dist/{log-22e678c5.esm.js → log-6e1b5540.esm.js} +12 -8
  26. package/dist/{log-641cd926.development.cjs.js → log-aa639ffe.development.cjs.js} +12 -8
  27. package/dist/{log-5e226275.cjs.js → log-dca6710c.cjs.js} +12 -8
  28. package/dist/{raise-62704519.development.cjs.js → raise-23324e7c.development.cjs.js} +86 -38
  29. package/dist/{raise-8bc422d1.esm.js → raise-525f78b5.esm.js} +86 -38
  30. package/dist/{raise-34e25c2c.cjs.js → raise-9f420f96.cjs.js} +86 -38
  31. package/dist/{raise-89c581c4.development.esm.js → raise-dd0e6cd2.development.esm.js} +86 -38
  32. package/dist/xstate.cjs.js +23 -5
  33. package/dist/xstate.development.cjs.js +23 -5
  34. package/dist/xstate.development.esm.js +25 -7
  35. package/dist/xstate.esm.js +25 -7
  36. package/dist/xstate.umd.min.js +1 -1
  37. package/dist/xstate.umd.min.js.map +1 -1
  38. package/guards/dist/xstate-guards.cjs.js +1 -1
  39. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  40. package/guards/dist/xstate-guards.development.esm.js +1 -1
  41. package/guards/dist/xstate-guards.esm.js +1 -1
  42. package/package.json +1 -1
  43. package/dist/declarations/src/constants.d.ts +0 -8
@@ -126,13 +126,56 @@ function reportUnhandledError(err) {
126
126
 
127
127
  const symbolObservable = (() => typeof Symbol === 'function' && Symbol.observable || '@@observable')();
128
128
 
129
+ function createScheduledEventId(actorRef, id) {
130
+ return `${actorRef.sessionId}.${id}`;
131
+ }
129
132
  let idCounter = 0;
130
- function createSystem(rootActor) {
133
+ function createSystem(rootActor, options) {
131
134
  const children = new Map();
132
135
  const keyedActors = new Map();
133
136
  const reverseKeyedActors = new WeakMap();
134
137
  const observers = new Set();
138
+ const timerMap = {};
139
+ const clock = options.clock;
140
+ const scheduler = {
141
+ schedule: (source, target, event, delay, id = Math.random().toString(36).slice(2)) => {
142
+ const scheduledEvent = {
143
+ source,
144
+ target,
145
+ event,
146
+ delay,
147
+ id,
148
+ startedAt: Date.now()
149
+ };
150
+ const scheduledEventId = createScheduledEventId(source, id);
151
+ system._snapshot._scheduledEvents[scheduledEventId] = scheduledEvent;
152
+ const timeout = clock.setTimeout(() => {
153
+ delete timerMap[scheduledEventId];
154
+ delete system._snapshot._scheduledEvents[scheduledEventId];
155
+ system._relay(source, target, event);
156
+ }, delay);
157
+ timerMap[scheduledEventId] = timeout;
158
+ },
159
+ cancel: (source, id) => {
160
+ const scheduledEventId = createScheduledEventId(source, id);
161
+ const timeout = timerMap[scheduledEventId];
162
+ delete timerMap[scheduledEventId];
163
+ delete system._snapshot._scheduledEvents[scheduledEventId];
164
+ clock.clearTimeout(timeout);
165
+ },
166
+ cancelAll: actorRef => {
167
+ for (const scheduledEventId in system._snapshot._scheduledEvents) {
168
+ const scheduledEvent = system._snapshot._scheduledEvents[scheduledEventId];
169
+ if (scheduledEvent.source === actorRef) {
170
+ scheduler.cancel(actorRef, scheduledEvent.id);
171
+ }
172
+ }
173
+ }
174
+ };
135
175
  const system = {
176
+ _snapshot: {
177
+ _scheduledEvents: (options?.snapshot && options.snapshot.scheduler) ?? {}
178
+ },
136
179
  _bookId: () => `x:${idCounter++}`,
137
180
  _register: (sessionId, actorRef) => {
138
181
  children.set(sessionId, actorRef);
@@ -175,6 +218,28 @@ function createSystem(rootActor) {
175
218
  event
176
219
  });
177
220
  target._send(event);
221
+ },
222
+ scheduler,
223
+ getSnapshot: () => {
224
+ return {
225
+ _scheduledEvents: {
226
+ ...system._snapshot._scheduledEvents
227
+ }
228
+ };
229
+ },
230
+ start: () => {
231
+ const scheduledEvets = system._snapshot._scheduledEvents;
232
+ system._snapshot._scheduledEvents = {};
233
+ for (const scheduledId in scheduledEvets) {
234
+ const {
235
+ source,
236
+ target,
237
+ event,
238
+ delay,
239
+ id
240
+ } = scheduledEvets[scheduledId];
241
+ scheduler.schedule(source, target, event, delay, id);
242
+ }
178
243
  }
179
244
  };
180
245
  return system;
@@ -360,7 +425,6 @@ class Actor {
360
425
  */
361
426
  this.id = void 0;
362
427
  this.mailbox = new Mailbox(this._process.bind(this));
363
- this.delayedEventsMap = {};
364
428
  this.observers = new Set();
365
429
  this.logger = void 0;
366
430
  /** @internal */
@@ -397,7 +461,9 @@ class Actor {
397
461
  systemId,
398
462
  inspect
399
463
  } = resolvedOptions;
400
- this.system = parent?.system ?? createSystem(this);
464
+ this.system = parent ? parent.system : createSystem(this, {
465
+ clock
466
+ });
401
467
  if (inspect && !parent) {
402
468
  // Always inspect at the system-level
403
469
  this.system.inspect(toObserver(inspect));
@@ -638,6 +704,9 @@ class Actor {
638
704
  this._error(this._snapshot.error);
639
705
  return this;
640
706
  }
707
+ if (!this._parent) {
708
+ this.system.start();
709
+ }
641
710
  if (this.logic.start) {
642
711
  try {
643
712
  this.logic.start(this._snapshot, this._actorScope);
@@ -766,9 +835,7 @@ class Actor {
766
835
  }
767
836
 
768
837
  // Cancel all delayed events
769
- for (const key of Object.keys(this.delayedEventsMap)) {
770
- this.clock.clearTimeout(this.delayedEventsMap[key]);
771
- }
838
+ this.system.scheduler.cancelAll(this);
772
839
 
773
840
  // TODO: mailbox.reset
774
841
  this.mailbox.clear();
@@ -800,35 +867,6 @@ class Actor {
800
867
  send(event) {
801
868
  this.system._relay(undefined, this, event);
802
869
  }
803
-
804
- /**
805
- * TODO: figure out a way to do this within the machine
806
- * @internal
807
- */
808
- delaySend(params) {
809
- const {
810
- event,
811
- id,
812
- delay
813
- } = params;
814
- const timerId = this.clock.setTimeout(() => {
815
- this.system._relay(this, params.to ?? this, event);
816
- }, delay);
817
-
818
- // TODO: consider the rehydration story here
819
- if (id) {
820
- this.delayedEventsMap[id] = timerId;
821
- }
822
- }
823
-
824
- /**
825
- * TODO: figure out a way to do this within the machine
826
- * @internal
827
- */
828
- cancel(sendId) {
829
- this.clock.clearTimeout(this.delayedEventsMap[sendId]);
830
- delete this.delayedEventsMap[sendId];
831
- }
832
870
  attachDevTools() {
833
871
  const {
834
872
  devTools
@@ -940,7 +978,9 @@ function resolveCancel(_, snapshot, actionArgs, actionParams, {
940
978
  return [snapshot, resolvedSendId];
941
979
  }
942
980
  function executeCancel(actorScope, resolvedSendId) {
943
- actorScope.self.cancel(resolvedSendId);
981
+ actorScope.defer(() => {
982
+ actorScope.system.scheduler.cancel(actorScope.self, resolvedSendId);
983
+ });
944
984
  }
945
985
  /**
946
986
  * Cancels an in-flight `send(...)` action. A canceled sent action will not
@@ -2291,8 +2331,16 @@ function resolveRaise(_, snapshot, args, actionParams, {
2291
2331
  }];
2292
2332
  }
2293
2333
  function executeRaise(actorScope, params) {
2294
- if (typeof params.delay === 'number') {
2295
- actorScope.self.delaySend(params);
2334
+ const {
2335
+ event,
2336
+ delay,
2337
+ id
2338
+ } = params;
2339
+ if (typeof delay === 'number') {
2340
+ actorScope.defer(() => {
2341
+ const self = actorScope.self;
2342
+ actorScope.system.scheduler.schedule(self, self, event, delay, id);
2343
+ });
2296
2344
  return;
2297
2345
  }
2298
2346
  }
@@ -128,13 +128,56 @@ function reportUnhandledError(err) {
128
128
 
129
129
  const symbolObservable = (() => typeof Symbol === 'function' && Symbol.observable || '@@observable')();
130
130
 
131
+ function createScheduledEventId(actorRef, id) {
132
+ return `${actorRef.sessionId}.${id}`;
133
+ }
131
134
  let idCounter = 0;
132
- function createSystem(rootActor) {
135
+ function createSystem(rootActor, options) {
133
136
  const children = new Map();
134
137
  const keyedActors = new Map();
135
138
  const reverseKeyedActors = new WeakMap();
136
139
  const observers = new Set();
140
+ const timerMap = {};
141
+ const clock = options.clock;
142
+ const scheduler = {
143
+ schedule: (source, target, event, delay, id = Math.random().toString(36).slice(2)) => {
144
+ const scheduledEvent = {
145
+ source,
146
+ target,
147
+ event,
148
+ delay,
149
+ id,
150
+ startedAt: Date.now()
151
+ };
152
+ const scheduledEventId = createScheduledEventId(source, id);
153
+ system._snapshot._scheduledEvents[scheduledEventId] = scheduledEvent;
154
+ const timeout = clock.setTimeout(() => {
155
+ delete timerMap[scheduledEventId];
156
+ delete system._snapshot._scheduledEvents[scheduledEventId];
157
+ system._relay(source, target, event);
158
+ }, delay);
159
+ timerMap[scheduledEventId] = timeout;
160
+ },
161
+ cancel: (source, id) => {
162
+ const scheduledEventId = createScheduledEventId(source, id);
163
+ const timeout = timerMap[scheduledEventId];
164
+ delete timerMap[scheduledEventId];
165
+ delete system._snapshot._scheduledEvents[scheduledEventId];
166
+ clock.clearTimeout(timeout);
167
+ },
168
+ cancelAll: actorRef => {
169
+ for (const scheduledEventId in system._snapshot._scheduledEvents) {
170
+ const scheduledEvent = system._snapshot._scheduledEvents[scheduledEventId];
171
+ if (scheduledEvent.source === actorRef) {
172
+ scheduler.cancel(actorRef, scheduledEvent.id);
173
+ }
174
+ }
175
+ }
176
+ };
137
177
  const system = {
178
+ _snapshot: {
179
+ _scheduledEvents: (options?.snapshot && options.snapshot.scheduler) ?? {}
180
+ },
138
181
  _bookId: () => `x:${idCounter++}`,
139
182
  _register: (sessionId, actorRef) => {
140
183
  children.set(sessionId, actorRef);
@@ -177,6 +220,28 @@ function createSystem(rootActor) {
177
220
  event
178
221
  });
179
222
  target._send(event);
223
+ },
224
+ scheduler,
225
+ getSnapshot: () => {
226
+ return {
227
+ _scheduledEvents: {
228
+ ...system._snapshot._scheduledEvents
229
+ }
230
+ };
231
+ },
232
+ start: () => {
233
+ const scheduledEvets = system._snapshot._scheduledEvents;
234
+ system._snapshot._scheduledEvents = {};
235
+ for (const scheduledId in scheduledEvets) {
236
+ const {
237
+ source,
238
+ target,
239
+ event,
240
+ delay,
241
+ id
242
+ } = scheduledEvets[scheduledId];
243
+ scheduler.schedule(source, target, event, delay, id);
244
+ }
180
245
  }
181
246
  };
182
247
  return system;
@@ -362,7 +427,6 @@ class Actor {
362
427
  */
363
428
  this.id = void 0;
364
429
  this.mailbox = new Mailbox(this._process.bind(this));
365
- this.delayedEventsMap = {};
366
430
  this.observers = new Set();
367
431
  this.logger = void 0;
368
432
  /** @internal */
@@ -399,7 +463,9 @@ class Actor {
399
463
  systemId,
400
464
  inspect
401
465
  } = resolvedOptions;
402
- this.system = parent?.system ?? createSystem(this);
466
+ this.system = parent ? parent.system : createSystem(this, {
467
+ clock
468
+ });
403
469
  if (inspect && !parent) {
404
470
  // Always inspect at the system-level
405
471
  this.system.inspect(toObserver(inspect));
@@ -640,6 +706,9 @@ class Actor {
640
706
  this._error(this._snapshot.error);
641
707
  return this;
642
708
  }
709
+ if (!this._parent) {
710
+ this.system.start();
711
+ }
643
712
  if (this.logic.start) {
644
713
  try {
645
714
  this.logic.start(this._snapshot, this._actorScope);
@@ -768,9 +837,7 @@ class Actor {
768
837
  }
769
838
 
770
839
  // Cancel all delayed events
771
- for (const key of Object.keys(this.delayedEventsMap)) {
772
- this.clock.clearTimeout(this.delayedEventsMap[key]);
773
- }
840
+ this.system.scheduler.cancelAll(this);
774
841
 
775
842
  // TODO: mailbox.reset
776
843
  this.mailbox.clear();
@@ -802,35 +869,6 @@ class Actor {
802
869
  send(event) {
803
870
  this.system._relay(undefined, this, event);
804
871
  }
805
-
806
- /**
807
- * TODO: figure out a way to do this within the machine
808
- * @internal
809
- */
810
- delaySend(params) {
811
- const {
812
- event,
813
- id,
814
- delay
815
- } = params;
816
- const timerId = this.clock.setTimeout(() => {
817
- this.system._relay(this, params.to ?? this, event);
818
- }, delay);
819
-
820
- // TODO: consider the rehydration story here
821
- if (id) {
822
- this.delayedEventsMap[id] = timerId;
823
- }
824
- }
825
-
826
- /**
827
- * TODO: figure out a way to do this within the machine
828
- * @internal
829
- */
830
- cancel(sendId) {
831
- this.clock.clearTimeout(this.delayedEventsMap[sendId]);
832
- delete this.delayedEventsMap[sendId];
833
- }
834
872
  attachDevTools() {
835
873
  const {
836
874
  devTools
@@ -942,7 +980,9 @@ function resolveCancel(_, snapshot, actionArgs, actionParams, {
942
980
  return [snapshot, resolvedSendId];
943
981
  }
944
982
  function executeCancel(actorScope, resolvedSendId) {
945
- actorScope.self.cancel(resolvedSendId);
983
+ actorScope.defer(() => {
984
+ actorScope.system.scheduler.cancel(actorScope.self, resolvedSendId);
985
+ });
946
986
  }
947
987
  /**
948
988
  * Cancels an in-flight `send(...)` action. A canceled sent action will not
@@ -2293,8 +2333,16 @@ function resolveRaise(_, snapshot, args, actionParams, {
2293
2333
  }];
2294
2334
  }
2295
2335
  function executeRaise(actorScope, params) {
2296
- if (typeof params.delay === 'number') {
2297
- actorScope.self.delaySend(params);
2336
+ const {
2337
+ event,
2338
+ delay,
2339
+ id
2340
+ } = params;
2341
+ if (typeof delay === 'number') {
2342
+ actorScope.defer(() => {
2343
+ const self = actorScope.self;
2344
+ actorScope.system.scheduler.schedule(self, self, event, delay, id);
2345
+ });
2298
2346
  return;
2299
2347
  }
2300
2348
  }
@@ -126,13 +126,56 @@ function reportUnhandledError(err) {
126
126
 
127
127
  const symbolObservable = (() => typeof Symbol === 'function' && Symbol.observable || '@@observable')();
128
128
 
129
+ function createScheduledEventId(actorRef, id) {
130
+ return `${actorRef.sessionId}.${id}`;
131
+ }
129
132
  let idCounter = 0;
130
- function createSystem(rootActor) {
133
+ function createSystem(rootActor, options) {
131
134
  const children = new Map();
132
135
  const keyedActors = new Map();
133
136
  const reverseKeyedActors = new WeakMap();
134
137
  const observers = new Set();
138
+ const timerMap = {};
139
+ const clock = options.clock;
140
+ const scheduler = {
141
+ schedule: (source, target, event, delay, id = Math.random().toString(36).slice(2)) => {
142
+ const scheduledEvent = {
143
+ source,
144
+ target,
145
+ event,
146
+ delay,
147
+ id,
148
+ startedAt: Date.now()
149
+ };
150
+ const scheduledEventId = createScheduledEventId(source, id);
151
+ system._snapshot._scheduledEvents[scheduledEventId] = scheduledEvent;
152
+ const timeout = clock.setTimeout(() => {
153
+ delete timerMap[scheduledEventId];
154
+ delete system._snapshot._scheduledEvents[scheduledEventId];
155
+ system._relay(source, target, event);
156
+ }, delay);
157
+ timerMap[scheduledEventId] = timeout;
158
+ },
159
+ cancel: (source, id) => {
160
+ const scheduledEventId = createScheduledEventId(source, id);
161
+ const timeout = timerMap[scheduledEventId];
162
+ delete timerMap[scheduledEventId];
163
+ delete system._snapshot._scheduledEvents[scheduledEventId];
164
+ clock.clearTimeout(timeout);
165
+ },
166
+ cancelAll: actorRef => {
167
+ for (const scheduledEventId in system._snapshot._scheduledEvents) {
168
+ const scheduledEvent = system._snapshot._scheduledEvents[scheduledEventId];
169
+ if (scheduledEvent.source === actorRef) {
170
+ scheduler.cancel(actorRef, scheduledEvent.id);
171
+ }
172
+ }
173
+ }
174
+ };
135
175
  const system = {
176
+ _snapshot: {
177
+ _scheduledEvents: (options?.snapshot && options.snapshot.scheduler) ?? {}
178
+ },
136
179
  _bookId: () => `x:${idCounter++}`,
137
180
  _register: (sessionId, actorRef) => {
138
181
  children.set(sessionId, actorRef);
@@ -175,6 +218,28 @@ function createSystem(rootActor) {
175
218
  event
176
219
  });
177
220
  target._send(event);
221
+ },
222
+ scheduler,
223
+ getSnapshot: () => {
224
+ return {
225
+ _scheduledEvents: {
226
+ ...system._snapshot._scheduledEvents
227
+ }
228
+ };
229
+ },
230
+ start: () => {
231
+ const scheduledEvets = system._snapshot._scheduledEvents;
232
+ system._snapshot._scheduledEvents = {};
233
+ for (const scheduledId in scheduledEvets) {
234
+ const {
235
+ source,
236
+ target,
237
+ event,
238
+ delay,
239
+ id
240
+ } = scheduledEvets[scheduledId];
241
+ scheduler.schedule(source, target, event, delay, id);
242
+ }
178
243
  }
179
244
  };
180
245
  return system;
@@ -363,7 +428,6 @@ class Actor {
363
428
  */
364
429
  this.id = void 0;
365
430
  this.mailbox = new Mailbox(this._process.bind(this));
366
- this.delayedEventsMap = {};
367
431
  this.observers = new Set();
368
432
  this.logger = void 0;
369
433
  /** @internal */
@@ -400,7 +464,9 @@ class Actor {
400
464
  systemId,
401
465
  inspect
402
466
  } = resolvedOptions;
403
- this.system = parent?.system ?? createSystem(this);
467
+ this.system = parent ? parent.system : createSystem(this, {
468
+ clock
469
+ });
404
470
  if (inspect && !parent) {
405
471
  // Always inspect at the system-level
406
472
  this.system.inspect(toObserver(inspect));
@@ -641,6 +707,9 @@ class Actor {
641
707
  this._error(this._snapshot.error);
642
708
  return this;
643
709
  }
710
+ if (!this._parent) {
711
+ this.system.start();
712
+ }
644
713
  if (this.logic.start) {
645
714
  try {
646
715
  this.logic.start(this._snapshot, this._actorScope);
@@ -769,9 +838,7 @@ class Actor {
769
838
  }
770
839
 
771
840
  // Cancel all delayed events
772
- for (const key of Object.keys(this.delayedEventsMap)) {
773
- this.clock.clearTimeout(this.delayedEventsMap[key]);
774
- }
841
+ this.system.scheduler.cancelAll(this);
775
842
 
776
843
  // TODO: mailbox.reset
777
844
  this.mailbox.clear();
@@ -811,35 +878,6 @@ class Actor {
811
878
  }
812
879
  this.system._relay(undefined, this, event);
813
880
  }
814
-
815
- /**
816
- * TODO: figure out a way to do this within the machine
817
- * @internal
818
- */
819
- delaySend(params) {
820
- const {
821
- event,
822
- id,
823
- delay
824
- } = params;
825
- const timerId = this.clock.setTimeout(() => {
826
- this.system._relay(this, params.to ?? this, event);
827
- }, delay);
828
-
829
- // TODO: consider the rehydration story here
830
- if (id) {
831
- this.delayedEventsMap[id] = timerId;
832
- }
833
- }
834
-
835
- /**
836
- * TODO: figure out a way to do this within the machine
837
- * @internal
838
- */
839
- cancel(sendId) {
840
- this.clock.clearTimeout(this.delayedEventsMap[sendId]);
841
- delete this.delayedEventsMap[sendId];
842
- }
843
881
  attachDevTools() {
844
882
  const {
845
883
  devTools
@@ -951,7 +989,9 @@ function resolveCancel(_, snapshot, actionArgs, actionParams, {
951
989
  return [snapshot, resolvedSendId];
952
990
  }
953
991
  function executeCancel(actorScope, resolvedSendId) {
954
- actorScope.self.cancel(resolvedSendId);
992
+ actorScope.defer(() => {
993
+ actorScope.system.scheduler.cancel(actorScope.self, resolvedSendId);
994
+ });
955
995
  }
956
996
  /**
957
997
  * Cancels an in-flight `send(...)` action. A canceled sent action will not
@@ -2342,8 +2382,16 @@ function resolveRaise(_, snapshot, args, actionParams, {
2342
2382
  }];
2343
2383
  }
2344
2384
  function executeRaise(actorScope, params) {
2345
- if (typeof params.delay === 'number') {
2346
- actorScope.self.delaySend(params);
2385
+ const {
2386
+ event,
2387
+ delay,
2388
+ id
2389
+ } = params;
2390
+ if (typeof delay === 'number') {
2391
+ actorScope.defer(() => {
2392
+ const self = actorScope.self;
2393
+ actorScope.system.scheduler.schedule(self, self, event, delay, id);
2394
+ });
2347
2395
  return;
2348
2396
  }
2349
2397
  }
@@ -3,8 +3,8 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var actors_dist_xstateActors = require('../actors/dist/xstate-actors.cjs.js');
6
- var guards_dist_xstateGuards = require('./raise-34e25c2c.cjs.js');
7
- var log = require('./log-5e226275.cjs.js');
6
+ var guards_dist_xstateGuards = require('./raise-9f420f96.cjs.js');
7
+ var log = require('./log-dca6710c.cjs.js');
8
8
  require('../dev/dist/xstate-dev.cjs.js');
9
9
 
10
10
  class SimulatedClock {
@@ -12,6 +12,8 @@ class SimulatedClock {
12
12
  this.timeouts = new Map();
13
13
  this._now = 0;
14
14
  this._id = 0;
15
+ this._flushing = false;
16
+ this._flushingInvalidated = false;
15
17
  }
16
18
  now() {
17
19
  return this._now;
@@ -20,6 +22,7 @@ class SimulatedClock {
20
22
  return this._id++;
21
23
  }
22
24
  setTimeout(fn, timeout) {
25
+ this._flushingInvalidated = this._flushing;
23
26
  const id = this.getId();
24
27
  this.timeouts.set(id, {
25
28
  start: this.now(),
@@ -29,6 +32,7 @@ class SimulatedClock {
29
32
  return id;
30
33
  }
31
34
  clearTimeout(id) {
35
+ this._flushingInvalidated = this._flushing;
32
36
  this.timeouts.delete(id);
33
37
  }
34
38
  set(time) {
@@ -39,16 +43,29 @@ class SimulatedClock {
39
43
  this.flushTimeouts();
40
44
  }
41
45
  flushTimeouts() {
42
- [...this.timeouts].sort(([_idA, timeoutA], [_idB, timeoutB]) => {
46
+ if (this._flushing) {
47
+ this._flushingInvalidated = true;
48
+ return;
49
+ }
50
+ this._flushing = true;
51
+ const sorted = [...this.timeouts].sort(([_idA, timeoutA], [_idB, timeoutB]) => {
43
52
  const endA = timeoutA.start + timeoutA.timeout;
44
53
  const endB = timeoutB.start + timeoutB.timeout;
45
54
  return endB > endA ? -1 : 1;
46
- }).forEach(([id, timeout]) => {
55
+ });
56
+ for (const [id, timeout] of sorted) {
57
+ if (this._flushingInvalidated) {
58
+ this._flushingInvalidated = false;
59
+ this._flushing = false;
60
+ this.flushTimeouts();
61
+ return;
62
+ }
47
63
  if (this.now() - timeout.start >= timeout.timeout) {
48
64
  this.timeouts.delete(id);
49
65
  timeout.fn.call(null);
50
66
  }
51
- });
67
+ }
68
+ this._flushing = false;
52
69
  }
53
70
  increment(ms) {
54
71
  this._now += ms;
@@ -390,6 +407,7 @@ class StateMachine {
390
407
  this.version = this.config.version;
391
408
  this.transition = this.transition.bind(this);
392
409
  this.getInitialSnapshot = this.getInitialSnapshot.bind(this);
410
+ this.getPersistedSnapshot = this.getPersistedSnapshot.bind(this);
393
411
  this.restoreSnapshot = this.restoreSnapshot.bind(this);
394
412
  this.start = this.start.bind(this);
395
413
  this.root = new StateNode(config, {