xstate 5.18.0 → 5.18.1

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 (32) 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.map +1 -1
  6. package/actors/dist/xstate-actors.cjs.js +1 -1
  7. package/actors/dist/xstate-actors.development.cjs.js +1 -1
  8. package/actors/dist/xstate-actors.development.esm.js +1 -1
  9. package/actors/dist/xstate-actors.esm.js +1 -1
  10. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  11. package/dist/declarations/src/State.d.ts +1 -1
  12. package/dist/declarations/src/createActor.d.ts +3 -4
  13. package/dist/declarations/src/index.d.ts +5 -5
  14. package/dist/{log-f7dcaa97.cjs.js → log-098d2ed5.cjs.js} +1 -1
  15. package/dist/{log-54d038f7.esm.js → log-a2c94240.esm.js} +1 -1
  16. package/dist/{log-4f8360d3.development.esm.js → log-c92a07bc.development.esm.js} +1 -1
  17. package/dist/{log-40d606d3.development.cjs.js → log-d26be77d.development.cjs.js} +1 -1
  18. package/dist/{State-34039d2a.development.esm.js → raise-206d3d29.development.esm.js} +206 -206
  19. package/dist/{State-cdbc7940.esm.js → raise-c0e3c984.esm.js} +191 -191
  20. package/dist/xstate.cjs.js +102 -102
  21. package/dist/xstate.development.cjs.js +102 -102
  22. package/dist/xstate.development.esm.js +98 -98
  23. package/dist/xstate.esm.js +98 -98
  24. package/dist/xstate.umd.min.js +1 -1
  25. package/dist/xstate.umd.min.js.map +1 -1
  26. package/guards/dist/xstate-guards.cjs.js +1 -1
  27. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  28. package/guards/dist/xstate-guards.development.esm.js +1 -1
  29. package/guards/dist/xstate-guards.esm.js +1 -1
  30. package/package.json +1 -1
  31. package/dist/{State-a2464a1e.development.cjs.js → raise-830a98f7.development.cjs.js} +205 -205
  32. package/dist/{State-30c95050.cjs.js → raise-cde45f56.cjs.js} +190 -190
@@ -1120,6 +1120,136 @@ function cancel(sendId) {
1120
1120
  return cancel;
1121
1121
  }
1122
1122
 
1123
+ function resolveSpawn(actorScope, snapshot, actionArgs, _actionParams, {
1124
+ id,
1125
+ systemId,
1126
+ src,
1127
+ input,
1128
+ syncSnapshot
1129
+ }) {
1130
+ const logic = typeof src === 'string' ? resolveReferencedActor(snapshot.machine, src) : src;
1131
+ const resolvedId = typeof id === 'function' ? id(actionArgs) : id;
1132
+ let actorRef;
1133
+ if (logic) {
1134
+ actorRef = createActor(logic, {
1135
+ id: resolvedId,
1136
+ src,
1137
+ parent: actorScope.self,
1138
+ syncSnapshot,
1139
+ systemId,
1140
+ input: typeof input === 'function' ? input({
1141
+ context: snapshot.context,
1142
+ event: actionArgs.event,
1143
+ self: actorScope.self
1144
+ }) : input
1145
+ });
1146
+ }
1147
+ return [cloneMachineSnapshot(snapshot, {
1148
+ children: {
1149
+ ...snapshot.children,
1150
+ [resolvedId]: actorRef
1151
+ }
1152
+ }), {
1153
+ id,
1154
+ actorRef
1155
+ }];
1156
+ }
1157
+ function executeSpawn(actorScope, {
1158
+ id,
1159
+ actorRef
1160
+ }) {
1161
+ if (!actorRef) {
1162
+ return;
1163
+ }
1164
+ actorScope.defer(() => {
1165
+ if (actorRef._processingStatus === ProcessingStatus.Stopped) {
1166
+ return;
1167
+ }
1168
+ actorRef.start();
1169
+ });
1170
+ }
1171
+ function spawnChild(...[src, {
1172
+ id,
1173
+ systemId,
1174
+ input,
1175
+ syncSnapshot = false
1176
+ } = {}]) {
1177
+ function spawnChild(args, params) {
1178
+ }
1179
+ spawnChild.type = 'snapshot.spawnChild';
1180
+ spawnChild.id = id;
1181
+ spawnChild.systemId = systemId;
1182
+ spawnChild.src = src;
1183
+ spawnChild.input = input;
1184
+ spawnChild.syncSnapshot = syncSnapshot;
1185
+ spawnChild.resolve = resolveSpawn;
1186
+ spawnChild.execute = executeSpawn;
1187
+ return spawnChild;
1188
+ }
1189
+
1190
+ function resolveStop(_, snapshot, args, actionParams, {
1191
+ actorRef
1192
+ }) {
1193
+ const actorRefOrString = typeof actorRef === 'function' ? actorRef(args, actionParams) : actorRef;
1194
+ const resolvedActorRef = typeof actorRefOrString === 'string' ? snapshot.children[actorRefOrString] : actorRefOrString;
1195
+ let children = snapshot.children;
1196
+ if (resolvedActorRef) {
1197
+ children = {
1198
+ ...children
1199
+ };
1200
+ delete children[resolvedActorRef.id];
1201
+ }
1202
+ return [cloneMachineSnapshot(snapshot, {
1203
+ children
1204
+ }), resolvedActorRef];
1205
+ }
1206
+ function executeStop(actorScope, actorRef) {
1207
+ if (!actorRef) {
1208
+ return;
1209
+ }
1210
+
1211
+ // we need to eagerly unregister it here so a new actor with the same systemId can be registered immediately
1212
+ // since we defer actual stopping of the actor but we don't defer actor creations (and we can't do that)
1213
+ // this could throw on `systemId` collision, for example, when dealing with reentering transitions
1214
+ actorScope.system._unregister(actorRef);
1215
+
1216
+ // this allows us to prevent an actor from being started if it gets stopped within the same macrostep
1217
+ // this can happen, for example, when the invoking state is being exited immediately by an always transition
1218
+ if (actorRef._processingStatus !== ProcessingStatus.Running) {
1219
+ actorScope.stopChild(actorRef);
1220
+ return;
1221
+ }
1222
+ // stopping a child enqueues a stop event in the child actor's mailbox
1223
+ // we need for all of the already enqueued events to be processed before we stop the child
1224
+ // the parent itself might want to send some events to a child (for example from exit actions on the invoking state)
1225
+ // and we don't want to ignore those events
1226
+ actorScope.defer(() => {
1227
+ actorScope.stopChild(actorRef);
1228
+ });
1229
+ }
1230
+ /**
1231
+ * Stops a child actor.
1232
+ *
1233
+ * @param actorRef The actor to stop.
1234
+ */
1235
+ function stopChild(actorRef) {
1236
+ function stop(args, params) {
1237
+ }
1238
+ stop.type = 'xstate.stopChild';
1239
+ stop.actorRef = actorRef;
1240
+ stop.resolve = resolveStop;
1241
+ stop.execute = executeStop;
1242
+ return stop;
1243
+ }
1244
+
1245
+ /**
1246
+ * Stops a child actor.
1247
+ *
1248
+ * @deprecated Use `stopChild(...)` instead
1249
+ * @alias
1250
+ */
1251
+ const stop = stopChild;
1252
+
1123
1253
  function checkStateIn(snapshot, _, {
1124
1254
  stateValue
1125
1255
  }) {
@@ -1308,196 +1438,6 @@ function evaluateGuard(guard, context, event, snapshot) {
1308
1438
  );
1309
1439
  }
1310
1440
 
1311
- function resolveRaise(_, snapshot, args, actionParams, {
1312
- event: eventOrExpr,
1313
- id,
1314
- delay
1315
- }, {
1316
- internalQueue
1317
- }) {
1318
- const delaysMap = snapshot.machine.implementations.delays;
1319
- if (typeof eventOrExpr === 'string') {
1320
- throw new Error(`Only event objects may be used with raise; use raise({ type: "${eventOrExpr}" }) instead`);
1321
- }
1322
- const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
1323
- let resolvedDelay;
1324
- if (typeof delay === 'string') {
1325
- const configDelay = delaysMap && delaysMap[delay];
1326
- resolvedDelay = typeof configDelay === 'function' ? configDelay(args, actionParams) : configDelay;
1327
- } else {
1328
- resolvedDelay = typeof delay === 'function' ? delay(args, actionParams) : delay;
1329
- }
1330
- if (typeof resolvedDelay !== 'number') {
1331
- internalQueue.push(resolvedEvent);
1332
- }
1333
- return [snapshot, {
1334
- event: resolvedEvent,
1335
- id,
1336
- delay: resolvedDelay
1337
- }];
1338
- }
1339
- function executeRaise(actorScope, params) {
1340
- const {
1341
- event,
1342
- delay,
1343
- id
1344
- } = params;
1345
- if (typeof delay === 'number') {
1346
- actorScope.defer(() => {
1347
- const self = actorScope.self;
1348
- actorScope.system.scheduler.schedule(self, self, event, delay, id);
1349
- });
1350
- return;
1351
- }
1352
- }
1353
- /**
1354
- * Raises an event. This places the event in the internal event queue, so that
1355
- * the event is immediately consumed by the machine in the current step.
1356
- *
1357
- * @param eventType The event to raise.
1358
- */
1359
- function raise(eventOrExpr, options) {
1360
- function raise(args, params) {
1361
- }
1362
- raise.type = 'xstate.raise';
1363
- raise.event = eventOrExpr;
1364
- raise.id = options?.id;
1365
- raise.delay = options?.delay;
1366
- raise.resolve = resolveRaise;
1367
- raise.execute = executeRaise;
1368
- return raise;
1369
- }
1370
-
1371
- function resolveSpawn(actorScope, snapshot, actionArgs, _actionParams, {
1372
- id,
1373
- systemId,
1374
- src,
1375
- input,
1376
- syncSnapshot
1377
- }) {
1378
- const logic = typeof src === 'string' ? resolveReferencedActor(snapshot.machine, src) : src;
1379
- const resolvedId = typeof id === 'function' ? id(actionArgs) : id;
1380
- let actorRef;
1381
- if (logic) {
1382
- actorRef = createActor(logic, {
1383
- id: resolvedId,
1384
- src,
1385
- parent: actorScope.self,
1386
- syncSnapshot,
1387
- systemId,
1388
- input: typeof input === 'function' ? input({
1389
- context: snapshot.context,
1390
- event: actionArgs.event,
1391
- self: actorScope.self
1392
- }) : input
1393
- });
1394
- }
1395
- return [cloneMachineSnapshot(snapshot, {
1396
- children: {
1397
- ...snapshot.children,
1398
- [resolvedId]: actorRef
1399
- }
1400
- }), {
1401
- id,
1402
- actorRef
1403
- }];
1404
- }
1405
- function executeSpawn(actorScope, {
1406
- id,
1407
- actorRef
1408
- }) {
1409
- if (!actorRef) {
1410
- return;
1411
- }
1412
- actorScope.defer(() => {
1413
- if (actorRef._processingStatus === ProcessingStatus.Stopped) {
1414
- return;
1415
- }
1416
- actorRef.start();
1417
- });
1418
- }
1419
- function spawnChild(...[src, {
1420
- id,
1421
- systemId,
1422
- input,
1423
- syncSnapshot = false
1424
- } = {}]) {
1425
- function spawnChild(args, params) {
1426
- }
1427
- spawnChild.type = 'snapshot.spawnChild';
1428
- spawnChild.id = id;
1429
- spawnChild.systemId = systemId;
1430
- spawnChild.src = src;
1431
- spawnChild.input = input;
1432
- spawnChild.syncSnapshot = syncSnapshot;
1433
- spawnChild.resolve = resolveSpawn;
1434
- spawnChild.execute = executeSpawn;
1435
- return spawnChild;
1436
- }
1437
-
1438
- function resolveStop(_, snapshot, args, actionParams, {
1439
- actorRef
1440
- }) {
1441
- const actorRefOrString = typeof actorRef === 'function' ? actorRef(args, actionParams) : actorRef;
1442
- const resolvedActorRef = typeof actorRefOrString === 'string' ? snapshot.children[actorRefOrString] : actorRefOrString;
1443
- let children = snapshot.children;
1444
- if (resolvedActorRef) {
1445
- children = {
1446
- ...children
1447
- };
1448
- delete children[resolvedActorRef.id];
1449
- }
1450
- return [cloneMachineSnapshot(snapshot, {
1451
- children
1452
- }), resolvedActorRef];
1453
- }
1454
- function executeStop(actorScope, actorRef) {
1455
- if (!actorRef) {
1456
- return;
1457
- }
1458
-
1459
- // we need to eagerly unregister it here so a new actor with the same systemId can be registered immediately
1460
- // since we defer actual stopping of the actor but we don't defer actor creations (and we can't do that)
1461
- // this could throw on `systemId` collision, for example, when dealing with reentering transitions
1462
- actorScope.system._unregister(actorRef);
1463
-
1464
- // this allows us to prevent an actor from being started if it gets stopped within the same macrostep
1465
- // this can happen, for example, when the invoking state is being exited immediately by an always transition
1466
- if (actorRef._processingStatus !== ProcessingStatus.Running) {
1467
- actorScope.stopChild(actorRef);
1468
- return;
1469
- }
1470
- // stopping a child enqueues a stop event in the child actor's mailbox
1471
- // we need for all of the already enqueued events to be processed before we stop the child
1472
- // the parent itself might want to send some events to a child (for example from exit actions on the invoking state)
1473
- // and we don't want to ignore those events
1474
- actorScope.defer(() => {
1475
- actorScope.stopChild(actorRef);
1476
- });
1477
- }
1478
- /**
1479
- * Stops a child actor.
1480
- *
1481
- * @param actorRef The actor to stop.
1482
- */
1483
- function stopChild(actorRef) {
1484
- function stop(args, params) {
1485
- }
1486
- stop.type = 'xstate.stopChild';
1487
- stop.actorRef = actorRef;
1488
- stop.resolve = resolveStop;
1489
- stop.execute = executeStop;
1490
- return stop;
1491
- }
1492
-
1493
- /**
1494
- * Stops a child actor.
1495
- *
1496
- * @deprecated Use `stopChild(...)` instead
1497
- * @alias
1498
- */
1499
- const stop = stopChild;
1500
-
1501
1441
  const isAtomicStateNode = stateNode => stateNode.type === 'atomic' || stateNode.type === 'final';
1502
1442
  function getChildren(stateNode) {
1503
1443
  return Object.values(stateNode.states).filter(sn => sn.type !== 'history');
@@ -2602,4 +2542,64 @@ function persistContext(contextPart) {
2602
2542
  return copy ?? contextPart;
2603
2543
  }
2604
2544
 
2605
- export { $$ACTOR_TYPE as $, createActor as A, isMachineSnapshot as B, Actor as C, interpret as D, and as E, not as F, or as G, stateIn as H, getAllOwnEventDescriptors as I, matchesState as J, pathToStateValue as K, toObserver as L, cancel as M, NULL_EVENT as N, raise as O, spawnChild as P, stop as Q, stopChild as R, STATE_DELIMITER as S, ProcessingStatus as T, cloneMachineSnapshot as U, XSTATE_ERROR as V, createErrorActorEvent as W, XSTATE_STOP as X, toTransitionConfigArray as a, formatTransition as b, createInvokeId as c, formatInitialTransition as d, evaluateGuard as e, formatTransitions as f, getDelayedTransitions as g, getCandidates as h, getAllStateNodes as i, getStateNodes as j, createMachineSnapshot as k, isInFinalState as l, mapValues as m, macrostep as n, transitionNode as o, resolveActionsAndContext as p, createInitEvent as q, resolveStateValue as r, microstep as s, toArray as t, getInitialStateNodes as u, toStatePath as v, isStateId as w, getStateNodeByPath as x, getPersistedSnapshot as y, resolveReferencedActor as z };
2545
+ function resolveRaise(_, snapshot, args, actionParams, {
2546
+ event: eventOrExpr,
2547
+ id,
2548
+ delay
2549
+ }, {
2550
+ internalQueue
2551
+ }) {
2552
+ const delaysMap = snapshot.machine.implementations.delays;
2553
+ if (typeof eventOrExpr === 'string') {
2554
+ throw new Error(`Only event objects may be used with raise; use raise({ type: "${eventOrExpr}" }) instead`);
2555
+ }
2556
+ const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
2557
+ let resolvedDelay;
2558
+ if (typeof delay === 'string') {
2559
+ const configDelay = delaysMap && delaysMap[delay];
2560
+ resolvedDelay = typeof configDelay === 'function' ? configDelay(args, actionParams) : configDelay;
2561
+ } else {
2562
+ resolvedDelay = typeof delay === 'function' ? delay(args, actionParams) : delay;
2563
+ }
2564
+ if (typeof resolvedDelay !== 'number') {
2565
+ internalQueue.push(resolvedEvent);
2566
+ }
2567
+ return [snapshot, {
2568
+ event: resolvedEvent,
2569
+ id,
2570
+ delay: resolvedDelay
2571
+ }];
2572
+ }
2573
+ function executeRaise(actorScope, params) {
2574
+ const {
2575
+ event,
2576
+ delay,
2577
+ id
2578
+ } = params;
2579
+ if (typeof delay === 'number') {
2580
+ actorScope.defer(() => {
2581
+ const self = actorScope.self;
2582
+ actorScope.system.scheduler.schedule(self, self, event, delay, id);
2583
+ });
2584
+ return;
2585
+ }
2586
+ }
2587
+ /**
2588
+ * Raises an event. This places the event in the internal event queue, so that
2589
+ * the event is immediately consumed by the machine in the current step.
2590
+ *
2591
+ * @param eventType The event to raise.
2592
+ */
2593
+ function raise(eventOrExpr, options) {
2594
+ function raise(args, params) {
2595
+ }
2596
+ raise.type = 'xstate.raise';
2597
+ raise.event = eventOrExpr;
2598
+ raise.id = options?.id;
2599
+ raise.delay = options?.delay;
2600
+ raise.resolve = resolveRaise;
2601
+ raise.execute = executeRaise;
2602
+ return raise;
2603
+ }
2604
+
2605
+ export { $$ACTOR_TYPE as $, createActor as A, Actor as B, interpret as C, and as D, not as E, or as F, stateIn as G, isMachineSnapshot as H, getAllOwnEventDescriptors as I, matchesState as J, pathToStateValue as K, toObserver as L, cancel as M, NULL_EVENT as N, raise as O, spawnChild as P, stop as Q, stopChild as R, STATE_DELIMITER as S, ProcessingStatus as T, cloneMachineSnapshot as U, XSTATE_ERROR as V, createErrorActorEvent as W, XSTATE_STOP as X, toTransitionConfigArray as a, formatTransition as b, createInvokeId as c, formatInitialTransition as d, evaluateGuard as e, formatTransitions as f, getDelayedTransitions as g, getCandidates as h, getAllStateNodes as i, getStateNodes as j, createMachineSnapshot as k, isInFinalState as l, mapValues as m, macrostep as n, transitionNode as o, resolveActionsAndContext as p, createInitEvent as q, resolveStateValue as r, microstep as s, toArray as t, getInitialStateNodes as u, toStatePath as v, isStateId as w, getStateNodeByPath as x, getPersistedSnapshot as y, resolveReferencedActor as z };
@@ -2,74 +2,39 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var guards_dist_xstateGuards = require('./State-30c95050.cjs.js');
6
- var log = require('./log-f7dcaa97.cjs.js');
7
5
  var actors_dist_xstateActors = require('../actors/dist/xstate-actors.cjs.js');
6
+ var guards_dist_xstateGuards = require('./raise-cde45f56.cjs.js');
7
+ var log = require('./log-098d2ed5.cjs.js');
8
8
  require('../dev/dist/xstate-dev.cjs.js');
9
9
 
10
- class SimulatedClock {
11
- constructor() {
12
- this.timeouts = new Map();
13
- this._now = 0;
14
- this._id = 0;
15
- this._flushing = false;
16
- this._flushingInvalidated = false;
17
- }
18
- now() {
19
- return this._now;
20
- }
21
- getId() {
22
- return this._id++;
23
- }
24
- setTimeout(fn, timeout) {
25
- this._flushingInvalidated = this._flushing;
26
- const id = this.getId();
27
- this.timeouts.set(id, {
28
- start: this.now(),
29
- timeout,
30
- fn
31
- });
32
- return id;
33
- }
34
- clearTimeout(id) {
35
- this._flushingInvalidated = this._flushing;
36
- this.timeouts.delete(id);
37
- }
38
- set(time) {
39
- if (this._now > time) {
40
- throw new Error('Unable to travel back in time');
41
- }
42
- this._now = time;
43
- this.flushTimeouts();
44
- }
45
- flushTimeouts() {
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]) => {
52
- const endA = timeoutA.start + timeoutA.timeout;
53
- const endB = timeoutB.start + timeoutB.timeout;
54
- return endB > endA ? -1 : 1;
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
- }
63
- if (this.now() - timeout.start >= timeout.timeout) {
64
- this.timeouts.delete(id);
65
- timeout.fn.call(null);
66
- }
67
- }
68
- this._flushing = false;
69
- }
70
- increment(ms) {
71
- this._now += ms;
72
- this.flushTimeouts();
10
+ /**
11
+ * Asserts that the given event object is of the specified type or types. Throws
12
+ * an error if the event object is not of the specified types.
13
+ *
14
+ * @example
15
+ *
16
+ * ```ts
17
+ * // ...
18
+ * entry: ({ event }) => {
19
+ * assertEvent(event, 'doNothing');
20
+ * // event is { type: 'doNothing' }
21
+ * },
22
+ * // ...
23
+ * exit: ({ event }) => {
24
+ * assertEvent(event, 'greet');
25
+ * // event is { type: 'greet'; message: string }
26
+ *
27
+ * assertEvent(event, ['greet', 'notify']);
28
+ * // event is { type: 'greet'; message: string }
29
+ * // or { type: 'notify'; message: string; level: 'info' | 'error' }
30
+ * },
31
+ * ```
32
+ */
33
+ function assertEvent(event, type) {
34
+ const types = guards_dist_xstateGuards.toArray(type);
35
+ if (!types.includes(event.type)) {
36
+ const typesText = types.length === 1 ? `type "${types[0]}"` : `one of types "${types.join('", "')}"`;
37
+ throw new Error(`Expected event ${JSON.stringify(event)} to have ${typesText}`);
73
38
  }
74
39
  }
75
40
 
@@ -598,37 +563,6 @@ class StateMachine {
598
563
  }
599
564
  }
600
565
 
601
- /**
602
- * Asserts that the given event object is of the specified type or types. Throws
603
- * an error if the event object is not of the specified types.
604
- *
605
- * @example
606
- *
607
- * ```ts
608
- * // ...
609
- * entry: ({ event }) => {
610
- * assertEvent(event, 'doNothing');
611
- * // event is { type: 'doNothing' }
612
- * },
613
- * // ...
614
- * exit: ({ event }) => {
615
- * assertEvent(event, 'greet');
616
- * // event is { type: 'greet'; message: string }
617
- *
618
- * assertEvent(event, ['greet', 'notify']);
619
- * // event is { type: 'greet'; message: string }
620
- * // or { type: 'notify'; message: string; level: 'info' | 'error' }
621
- * },
622
- * ```
623
- */
624
- function assertEvent(event, type) {
625
- const types = guards_dist_xstateGuards.toArray(type);
626
- if (!types.includes(event.type)) {
627
- const typesText = types.length === 1 ? `type "${types[0]}"` : `one of types "${types.join('", "')}"`;
628
- throw new Error(`Expected event ${JSON.stringify(event)} to have ${typesText}`);
629
- }
630
- }
631
-
632
566
  // this is not 100% accurate since we can't make parallel regions required in the result
633
567
  // `TTestValue` doesn't encode this information anyhow for us to be able to do that
634
568
  // this is fine for most practical use cases anyway though
@@ -758,6 +692,72 @@ function setup({
758
692
  };
759
693
  }
760
694
 
695
+ class SimulatedClock {
696
+ constructor() {
697
+ this.timeouts = new Map();
698
+ this._now = 0;
699
+ this._id = 0;
700
+ this._flushing = false;
701
+ this._flushingInvalidated = false;
702
+ }
703
+ now() {
704
+ return this._now;
705
+ }
706
+ getId() {
707
+ return this._id++;
708
+ }
709
+ setTimeout(fn, timeout) {
710
+ this._flushingInvalidated = this._flushing;
711
+ const id = this.getId();
712
+ this.timeouts.set(id, {
713
+ start: this.now(),
714
+ timeout,
715
+ fn
716
+ });
717
+ return id;
718
+ }
719
+ clearTimeout(id) {
720
+ this._flushingInvalidated = this._flushing;
721
+ this.timeouts.delete(id);
722
+ }
723
+ set(time) {
724
+ if (this._now > time) {
725
+ throw new Error('Unable to travel back in time');
726
+ }
727
+ this._now = time;
728
+ this.flushTimeouts();
729
+ }
730
+ flushTimeouts() {
731
+ if (this._flushing) {
732
+ this._flushingInvalidated = true;
733
+ return;
734
+ }
735
+ this._flushing = true;
736
+ const sorted = [...this.timeouts].sort(([_idA, timeoutA], [_idB, timeoutB]) => {
737
+ const endA = timeoutA.start + timeoutA.timeout;
738
+ const endB = timeoutB.start + timeoutB.timeout;
739
+ return endB > endA ? -1 : 1;
740
+ });
741
+ for (const [id, timeout] of sorted) {
742
+ if (this._flushingInvalidated) {
743
+ this._flushingInvalidated = false;
744
+ this._flushing = false;
745
+ this.flushTimeouts();
746
+ return;
747
+ }
748
+ if (this.now() - timeout.start >= timeout.timeout) {
749
+ this.timeouts.delete(id);
750
+ timeout.fn.call(null);
751
+ }
752
+ }
753
+ this._flushing = false;
754
+ }
755
+ increment(ms) {
756
+ this._now += ms;
757
+ this.flushTimeouts();
758
+ }
759
+ }
760
+
761
761
  /**
762
762
  * Returns a promise that resolves to the `output` of the actor when it is done.
763
763
  *
@@ -889,6 +889,12 @@ function waitFor(actorRef, predicate, options) {
889
889
  });
890
890
  }
891
891
 
892
+ exports.createEmptyActor = actors_dist_xstateActors.createEmptyActor;
893
+ exports.fromCallback = actors_dist_xstateActors.fromCallback;
894
+ exports.fromEventObservable = actors_dist_xstateActors.fromEventObservable;
895
+ exports.fromObservable = actors_dist_xstateActors.fromObservable;
896
+ exports.fromPromise = actors_dist_xstateActors.fromPromise;
897
+ exports.fromTransition = actors_dist_xstateActors.fromTransition;
892
898
  exports.Actor = guards_dist_xstateGuards.Actor;
893
899
  exports.__unsafe_getAllOwnEventDescriptors = guards_dist_xstateGuards.getAllOwnEventDescriptors;
894
900
  exports.and = guards_dist_xstateGuards.and;
@@ -915,12 +921,6 @@ exports.forwardTo = log.forwardTo;
915
921
  exports.log = log.log;
916
922
  exports.sendParent = log.sendParent;
917
923
  exports.sendTo = log.sendTo;
918
- exports.createEmptyActor = actors_dist_xstateActors.createEmptyActor;
919
- exports.fromCallback = actors_dist_xstateActors.fromCallback;
920
- exports.fromEventObservable = actors_dist_xstateActors.fromEventObservable;
921
- exports.fromObservable = actors_dist_xstateActors.fromObservable;
922
- exports.fromPromise = actors_dist_xstateActors.fromPromise;
923
- exports.fromTransition = actors_dist_xstateActors.fromTransition;
924
924
  exports.SimulatedClock = SimulatedClock;
925
925
  exports.StateMachine = StateMachine;
926
926
  exports.StateNode = StateNode;