xstate 5.16.0 → 5.17.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 (44) 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 -1
  8. package/actors/dist/xstate-actors.development.cjs.js +1 -1
  9. package/actors/dist/xstate-actors.development.esm.js +1 -1
  10. package/actors/dist/xstate-actors.esm.js +1 -1
  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/{raise-a6298350.cjs.js → State-30c95050.cjs.js} +199 -191
  14. package/dist/{raise-7d030497.development.esm.js → State-34039d2a.development.esm.js} +215 -207
  15. package/dist/{raise-bad6a97b.development.cjs.js → State-a2464a1e.development.cjs.js} +214 -206
  16. package/dist/{raise-2cfe6b8f.esm.js → State-cdbc7940.esm.js} +200 -192
  17. package/dist/declarations/src/State.d.ts +8 -8
  18. package/dist/declarations/src/StateMachine.d.ts +10 -12
  19. package/dist/declarations/src/actions/stopChild.d.ts +1 -0
  20. package/dist/declarations/src/actors/callback.d.ts +3 -2
  21. package/dist/declarations/src/createActor.d.ts +6 -6
  22. package/dist/declarations/src/index.d.ts +11 -12
  23. package/dist/declarations/src/inspection.d.ts +3 -2
  24. package/dist/declarations/src/setup.d.ts +1 -1
  25. package/dist/declarations/src/stateUtils.d.ts +1 -13
  26. package/dist/declarations/src/system.d.ts +3 -2
  27. package/dist/declarations/src/types.d.ts +7 -1
  28. package/dist/declarations/src/utils.d.ts +1 -5
  29. package/dist/{log-f9587b82.cjs.js → log-19086852.cjs.js} +1 -1
  30. package/dist/{log-8320f5e6.esm.js → log-4a38a98a.esm.js} +1 -1
  31. package/dist/{log-17f4495d.development.esm.js → log-62f17756.development.esm.js} +1 -1
  32. package/dist/{log-31321d85.development.cjs.js → log-ec36113c.development.cjs.js} +1 -1
  33. package/dist/xstate.cjs.js +92 -92
  34. package/dist/xstate.development.cjs.js +95 -95
  35. package/dist/xstate.development.esm.js +91 -91
  36. package/dist/xstate.esm.js +88 -88
  37. package/dist/xstate.umd.min.js +1 -1
  38. package/dist/xstate.umd.min.js.map +1 -1
  39. package/guards/dist/xstate-guards.cjs.js +1 -1
  40. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  41. package/guards/dist/xstate-guards.development.esm.js +1 -1
  42. package/guards/dist/xstate-guards.esm.js +1 -1
  43. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  44. package/package.json +1 -3
@@ -326,7 +326,9 @@ function createSystem(rootActor, options) {
326
326
  const timeout = timerMap[scheduledEventId];
327
327
  delete timerMap[scheduledEventId];
328
328
  delete system._snapshot._scheduledEvents[scheduledEventId];
329
- clock.clearTimeout(timeout);
329
+ if (timeout !== undefined) {
330
+ clock.clearTimeout(timeout);
331
+ }
330
332
  },
331
333
  cancelAll: actorRef => {
332
334
  for (const scheduledEventId in system._snapshot._scheduledEvents) {
@@ -423,6 +425,7 @@ function createSystem(rootActor, options) {
423
425
  }
424
426
 
425
427
  const $$ACTOR_TYPE = 1;
428
+
426
429
  // those values are currently used by @xstate/react directly so it's important to keep the assigned values in sync
427
430
  let ProcessingStatus = /*#__PURE__*/function (ProcessingStatus) {
428
431
  ProcessingStatus[ProcessingStatus["NotStarted"] = 0] = "NotStarted";
@@ -1071,10 +1074,14 @@ function createActor(logic, ...[options]) {
1071
1074
  * options, if any.
1072
1075
  *
1073
1076
  * @deprecated Use `createActor` instead
1077
+ * @alias
1074
1078
  */
1075
1079
  const interpret = createActor;
1076
1080
 
1077
- /** @deprecated Use `Actor` instead. */
1081
+ /**
1082
+ * @deprecated Use `Actor` instead.
1083
+ * @alias
1084
+ */
1078
1085
 
1079
1086
  function resolveCancel(_, snapshot, actionArgs, actionParams, {
1080
1087
  sendId
@@ -1132,144 +1139,6 @@ function cancel(sendId) {
1132
1139
  return cancel;
1133
1140
  }
1134
1141
 
1135
- function resolveSpawn(actorScope, snapshot, actionArgs, _actionParams, {
1136
- id,
1137
- systemId,
1138
- src,
1139
- input,
1140
- syncSnapshot
1141
- }) {
1142
- const logic = typeof src === 'string' ? resolveReferencedActor(snapshot.machine, src) : src;
1143
- const resolvedId = typeof id === 'function' ? id(actionArgs) : id;
1144
- let actorRef;
1145
- if (logic) {
1146
- actorRef = createActor(logic, {
1147
- id: resolvedId,
1148
- src,
1149
- parent: actorScope.self,
1150
- syncSnapshot,
1151
- systemId,
1152
- input: typeof input === 'function' ? input({
1153
- context: snapshot.context,
1154
- event: actionArgs.event,
1155
- self: actorScope.self
1156
- }) : input
1157
- });
1158
- }
1159
- if (!actorRef) {
1160
- console.warn(`Actor type '${src}' not found in machine '${actorScope.id}'.`);
1161
- }
1162
- return [cloneMachineSnapshot(snapshot, {
1163
- children: {
1164
- ...snapshot.children,
1165
- [resolvedId]: actorRef
1166
- }
1167
- }), {
1168
- id,
1169
- actorRef
1170
- }];
1171
- }
1172
- function executeSpawn(actorScope, {
1173
- id,
1174
- actorRef
1175
- }) {
1176
- if (!actorRef) {
1177
- return;
1178
- }
1179
- actorScope.defer(() => {
1180
- if (actorRef._processingStatus === ProcessingStatus.Stopped) {
1181
- return;
1182
- }
1183
- actorRef.start();
1184
- });
1185
- }
1186
- function spawnChild(...[src, {
1187
- id,
1188
- systemId,
1189
- input,
1190
- syncSnapshot = false
1191
- } = {}]) {
1192
- function spawnChild(args, params) {
1193
- {
1194
- throw new Error(`This isn't supposed to be called`);
1195
- }
1196
- }
1197
- spawnChild.type = 'snapshot.spawnChild';
1198
- spawnChild.id = id;
1199
- spawnChild.systemId = systemId;
1200
- spawnChild.src = src;
1201
- spawnChild.input = input;
1202
- spawnChild.syncSnapshot = syncSnapshot;
1203
- spawnChild.resolve = resolveSpawn;
1204
- spawnChild.execute = executeSpawn;
1205
- return spawnChild;
1206
- }
1207
-
1208
- function resolveStop(_, snapshot, args, actionParams, {
1209
- actorRef
1210
- }) {
1211
- const actorRefOrString = typeof actorRef === 'function' ? actorRef(args, actionParams) : actorRef;
1212
- const resolvedActorRef = typeof actorRefOrString === 'string' ? snapshot.children[actorRefOrString] : actorRefOrString;
1213
- let children = snapshot.children;
1214
- if (resolvedActorRef) {
1215
- children = {
1216
- ...children
1217
- };
1218
- delete children[resolvedActorRef.id];
1219
- }
1220
- return [cloneMachineSnapshot(snapshot, {
1221
- children
1222
- }), resolvedActorRef];
1223
- }
1224
- function executeStop(actorScope, actorRef) {
1225
- if (!actorRef) {
1226
- return;
1227
- }
1228
-
1229
- // we need to eagerly unregister it here so a new actor with the same systemId can be registered immediately
1230
- // since we defer actual stopping of the actor but we don't defer actor creations (and we can't do that)
1231
- // this could throw on `systemId` collision, for example, when dealing with reentering transitions
1232
- actorScope.system._unregister(actorRef);
1233
-
1234
- // this allows us to prevent an actor from being started if it gets stopped within the same macrostep
1235
- // this can happen, for example, when the invoking state is being exited immediately by an always transition
1236
- if (actorRef._processingStatus !== ProcessingStatus.Running) {
1237
- actorScope.stopChild(actorRef);
1238
- return;
1239
- }
1240
- // stopping a child enqueues a stop event in the child actor's mailbox
1241
- // we need for all of the already enqueued events to be processed before we stop the child
1242
- // the parent itself might want to send some events to a child (for example from exit actions on the invoking state)
1243
- // and we don't want to ignore those events
1244
- actorScope.defer(() => {
1245
- actorScope.stopChild(actorRef);
1246
- });
1247
- }
1248
- /**
1249
- * Stops a child actor.
1250
- *
1251
- * @param actorRef The actor to stop.
1252
- */
1253
- function stopChild(actorRef) {
1254
- function stop(args, params) {
1255
- {
1256
- throw new Error(`This isn't supposed to be called`);
1257
- }
1258
- }
1259
- stop.type = 'xstate.stopChild';
1260
- stop.actorRef = actorRef;
1261
- stop.resolve = resolveStop;
1262
- stop.execute = executeStop;
1263
- return stop;
1264
- }
1265
-
1266
- /**
1267
- * Stops a child actor.
1268
- *
1269
- * @deprecated Use `stopChild(...)` instead
1270
- */
1271
- const stop = stopChild;
1272
-
1273
1142
  function checkStateIn(snapshot, _, {
1274
1143
  stateValue
1275
1144
  }) {
@@ -1466,6 +1335,211 @@ function evaluateGuard(guard, context, event, snapshot) {
1466
1335
  );
1467
1336
  }
1468
1337
 
1338
+ function resolveRaise(_, snapshot, args, actionParams, {
1339
+ event: eventOrExpr,
1340
+ id,
1341
+ delay
1342
+ }, {
1343
+ internalQueue
1344
+ }) {
1345
+ const delaysMap = snapshot.machine.implementations.delays;
1346
+ if (typeof eventOrExpr === 'string') {
1347
+ throw new Error(`Only event objects may be used with raise; use raise({ type: "${eventOrExpr}" }) instead`);
1348
+ }
1349
+ const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
1350
+ let resolvedDelay;
1351
+ if (typeof delay === 'string') {
1352
+ const configDelay = delaysMap && delaysMap[delay];
1353
+ resolvedDelay = typeof configDelay === 'function' ? configDelay(args, actionParams) : configDelay;
1354
+ } else {
1355
+ resolvedDelay = typeof delay === 'function' ? delay(args, actionParams) : delay;
1356
+ }
1357
+ if (typeof resolvedDelay !== 'number') {
1358
+ internalQueue.push(resolvedEvent);
1359
+ }
1360
+ return [snapshot, {
1361
+ event: resolvedEvent,
1362
+ id,
1363
+ delay: resolvedDelay
1364
+ }];
1365
+ }
1366
+ function executeRaise(actorScope, params) {
1367
+ const {
1368
+ event,
1369
+ delay,
1370
+ id
1371
+ } = params;
1372
+ if (typeof delay === 'number') {
1373
+ actorScope.defer(() => {
1374
+ const self = actorScope.self;
1375
+ actorScope.system.scheduler.schedule(self, self, event, delay, id);
1376
+ });
1377
+ return;
1378
+ }
1379
+ }
1380
+ /**
1381
+ * Raises an event. This places the event in the internal event queue, so that
1382
+ * the event is immediately consumed by the machine in the current step.
1383
+ *
1384
+ * @param eventType The event to raise.
1385
+ */
1386
+ function raise(eventOrExpr, options) {
1387
+ if (exports.executingCustomAction) {
1388
+ console.warn('Custom actions should not call `raise()` directly, as it is not imperative. See https://stately.ai/docs/actions#built-in-actions for more details.');
1389
+ }
1390
+ function raise(args, params) {
1391
+ {
1392
+ throw new Error(`This isn't supposed to be called`);
1393
+ }
1394
+ }
1395
+ raise.type = 'xstate.raise';
1396
+ raise.event = eventOrExpr;
1397
+ raise.id = options?.id;
1398
+ raise.delay = options?.delay;
1399
+ raise.resolve = resolveRaise;
1400
+ raise.execute = executeRaise;
1401
+ return raise;
1402
+ }
1403
+
1404
+ function resolveSpawn(actorScope, snapshot, actionArgs, _actionParams, {
1405
+ id,
1406
+ systemId,
1407
+ src,
1408
+ input,
1409
+ syncSnapshot
1410
+ }) {
1411
+ const logic = typeof src === 'string' ? resolveReferencedActor(snapshot.machine, src) : src;
1412
+ const resolvedId = typeof id === 'function' ? id(actionArgs) : id;
1413
+ let actorRef;
1414
+ if (logic) {
1415
+ actorRef = createActor(logic, {
1416
+ id: resolvedId,
1417
+ src,
1418
+ parent: actorScope.self,
1419
+ syncSnapshot,
1420
+ systemId,
1421
+ input: typeof input === 'function' ? input({
1422
+ context: snapshot.context,
1423
+ event: actionArgs.event,
1424
+ self: actorScope.self
1425
+ }) : input
1426
+ });
1427
+ }
1428
+ if (!actorRef) {
1429
+ console.warn(`Actor type '${src}' not found in machine '${actorScope.id}'.`);
1430
+ }
1431
+ return [cloneMachineSnapshot(snapshot, {
1432
+ children: {
1433
+ ...snapshot.children,
1434
+ [resolvedId]: actorRef
1435
+ }
1436
+ }), {
1437
+ id,
1438
+ actorRef
1439
+ }];
1440
+ }
1441
+ function executeSpawn(actorScope, {
1442
+ id,
1443
+ actorRef
1444
+ }) {
1445
+ if (!actorRef) {
1446
+ return;
1447
+ }
1448
+ actorScope.defer(() => {
1449
+ if (actorRef._processingStatus === ProcessingStatus.Stopped) {
1450
+ return;
1451
+ }
1452
+ actorRef.start();
1453
+ });
1454
+ }
1455
+ function spawnChild(...[src, {
1456
+ id,
1457
+ systemId,
1458
+ input,
1459
+ syncSnapshot = false
1460
+ } = {}]) {
1461
+ function spawnChild(args, params) {
1462
+ {
1463
+ throw new Error(`This isn't supposed to be called`);
1464
+ }
1465
+ }
1466
+ spawnChild.type = 'snapshot.spawnChild';
1467
+ spawnChild.id = id;
1468
+ spawnChild.systemId = systemId;
1469
+ spawnChild.src = src;
1470
+ spawnChild.input = input;
1471
+ spawnChild.syncSnapshot = syncSnapshot;
1472
+ spawnChild.resolve = resolveSpawn;
1473
+ spawnChild.execute = executeSpawn;
1474
+ return spawnChild;
1475
+ }
1476
+
1477
+ function resolveStop(_, snapshot, args, actionParams, {
1478
+ actorRef
1479
+ }) {
1480
+ const actorRefOrString = typeof actorRef === 'function' ? actorRef(args, actionParams) : actorRef;
1481
+ const resolvedActorRef = typeof actorRefOrString === 'string' ? snapshot.children[actorRefOrString] : actorRefOrString;
1482
+ let children = snapshot.children;
1483
+ if (resolvedActorRef) {
1484
+ children = {
1485
+ ...children
1486
+ };
1487
+ delete children[resolvedActorRef.id];
1488
+ }
1489
+ return [cloneMachineSnapshot(snapshot, {
1490
+ children
1491
+ }), resolvedActorRef];
1492
+ }
1493
+ function executeStop(actorScope, actorRef) {
1494
+ if (!actorRef) {
1495
+ return;
1496
+ }
1497
+
1498
+ // we need to eagerly unregister it here so a new actor with the same systemId can be registered immediately
1499
+ // since we defer actual stopping of the actor but we don't defer actor creations (and we can't do that)
1500
+ // this could throw on `systemId` collision, for example, when dealing with reentering transitions
1501
+ actorScope.system._unregister(actorRef);
1502
+
1503
+ // this allows us to prevent an actor from being started if it gets stopped within the same macrostep
1504
+ // this can happen, for example, when the invoking state is being exited immediately by an always transition
1505
+ if (actorRef._processingStatus !== ProcessingStatus.Running) {
1506
+ actorScope.stopChild(actorRef);
1507
+ return;
1508
+ }
1509
+ // stopping a child enqueues a stop event in the child actor's mailbox
1510
+ // we need for all of the already enqueued events to be processed before we stop the child
1511
+ // the parent itself might want to send some events to a child (for example from exit actions on the invoking state)
1512
+ // and we don't want to ignore those events
1513
+ actorScope.defer(() => {
1514
+ actorScope.stopChild(actorRef);
1515
+ });
1516
+ }
1517
+ /**
1518
+ * Stops a child actor.
1519
+ *
1520
+ * @param actorRef The actor to stop.
1521
+ */
1522
+ function stopChild(actorRef) {
1523
+ function stop(args, params) {
1524
+ {
1525
+ throw new Error(`This isn't supposed to be called`);
1526
+ }
1527
+ }
1528
+ stop.type = 'xstate.stopChild';
1529
+ stop.actorRef = actorRef;
1530
+ stop.resolve = resolveStop;
1531
+ stop.execute = executeStop;
1532
+ return stop;
1533
+ }
1534
+
1535
+ /**
1536
+ * Stops a child actor.
1537
+ *
1538
+ * @deprecated Use `stopChild(...)` instead
1539
+ * @alias
1540
+ */
1541
+ const stop = stopChild;
1542
+
1469
1543
  const isAtomicStateNode = stateNode => stateNode.type === 'atomic' || stateNode.type === 'final';
1470
1544
  function getChildren(stateNode) {
1471
1545
  return Object.values(stateNode.states).filter(sn => sn.type !== 'history');
@@ -2590,72 +2664,6 @@ function persistContext(contextPart) {
2590
2664
  return copy ?? contextPart;
2591
2665
  }
2592
2666
 
2593
- function resolveRaise(_, snapshot, args, actionParams, {
2594
- event: eventOrExpr,
2595
- id,
2596
- delay
2597
- }, {
2598
- internalQueue
2599
- }) {
2600
- const delaysMap = snapshot.machine.implementations.delays;
2601
- if (typeof eventOrExpr === 'string') {
2602
- throw new Error(`Only event objects may be used with raise; use raise({ type: "${eventOrExpr}" }) instead`);
2603
- }
2604
- const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
2605
- let resolvedDelay;
2606
- if (typeof delay === 'string') {
2607
- const configDelay = delaysMap && delaysMap[delay];
2608
- resolvedDelay = typeof configDelay === 'function' ? configDelay(args, actionParams) : configDelay;
2609
- } else {
2610
- resolvedDelay = typeof delay === 'function' ? delay(args, actionParams) : delay;
2611
- }
2612
- if (typeof resolvedDelay !== 'number') {
2613
- internalQueue.push(resolvedEvent);
2614
- }
2615
- return [snapshot, {
2616
- event: resolvedEvent,
2617
- id,
2618
- delay: resolvedDelay
2619
- }];
2620
- }
2621
- function executeRaise(actorScope, params) {
2622
- const {
2623
- event,
2624
- delay,
2625
- id
2626
- } = params;
2627
- if (typeof delay === 'number') {
2628
- actorScope.defer(() => {
2629
- const self = actorScope.self;
2630
- actorScope.system.scheduler.schedule(self, self, event, delay, id);
2631
- });
2632
- return;
2633
- }
2634
- }
2635
- /**
2636
- * Raises an event. This places the event in the internal event queue, so that
2637
- * the event is immediately consumed by the machine in the current step.
2638
- *
2639
- * @param eventType The event to raise.
2640
- */
2641
- function raise(eventOrExpr, options) {
2642
- if (exports.executingCustomAction) {
2643
- console.warn('Custom actions should not call `raise()` directly, as it is not imperative. See https://stately.ai/docs/actions#built-in-actions for more details.');
2644
- }
2645
- function raise(args, params) {
2646
- {
2647
- throw new Error(`This isn't supposed to be called`);
2648
- }
2649
- }
2650
- raise.type = 'xstate.raise';
2651
- raise.event = eventOrExpr;
2652
- raise.id = options?.id;
2653
- raise.delay = options?.delay;
2654
- raise.resolve = resolveRaise;
2655
- raise.execute = executeRaise;
2656
- return raise;
2657
- }
2658
-
2659
2667
  exports.$$ACTOR_TYPE = $$ACTOR_TYPE;
2660
2668
  exports.Actor = Actor;
2661
2669
  exports.NULL_EVENT = NULL_EVENT;