xstate 5.0.0-beta.37 → 5.0.0-beta.39

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 (62) hide show
  1. package/actions/dist/xstate-actions.cjs.js +4 -3
  2. package/actions/dist/xstate-actions.cjs.mjs +1 -0
  3. package/actions/dist/xstate-actions.development.cjs.js +4 -3
  4. package/actions/dist/xstate-actions.development.cjs.mjs +1 -0
  5. package/actions/dist/xstate-actions.development.esm.js +3 -3
  6. package/actions/dist/xstate-actions.esm.js +3 -3
  7. package/actions/dist/xstate-actions.umd.min.js +1 -1
  8. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  9. package/actors/dist/xstate-actors.cjs.js +60 -3
  10. package/actors/dist/xstate-actors.development.cjs.js +60 -3
  11. package/actors/dist/xstate-actors.development.esm.js +60 -3
  12. package/actors/dist/xstate-actors.esm.js +60 -3
  13. package/actors/dist/xstate-actors.umd.min.js +1 -1
  14. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  15. package/dist/declarations/src/State.d.ts +2 -2
  16. package/dist/declarations/src/StateMachine.d.ts +7 -7
  17. package/dist/declarations/src/actions/assign.d.ts +4 -4
  18. package/dist/declarations/src/actions/cancel.d.ts +4 -4
  19. package/dist/declarations/src/actions/choose.d.ts +3 -3
  20. package/dist/declarations/src/actions/log.d.ts +4 -4
  21. package/dist/declarations/src/actions/pure.d.ts +4 -4
  22. package/dist/declarations/src/actions/raise.d.ts +3 -3
  23. package/dist/declarations/src/actions/send.d.ts +7 -7
  24. package/dist/declarations/src/actions/spawn.d.ts +34 -0
  25. package/dist/declarations/src/actions/stop.d.ts +4 -4
  26. package/dist/declarations/src/actions.d.ts +1 -0
  27. package/dist/declarations/src/actors/callback.d.ts +76 -2
  28. package/dist/declarations/src/actors/observable.d.ts +2 -3
  29. package/dist/declarations/src/actors/promise.d.ts +0 -1
  30. package/dist/declarations/src/actors/transition.d.ts +3 -3
  31. package/dist/declarations/src/guards.d.ts +9 -10
  32. package/dist/declarations/src/interpreter.d.ts +71 -10
  33. package/dist/declarations/src/spawn.d.ts +3 -4
  34. package/dist/declarations/src/stateUtils.d.ts +4 -9
  35. package/dist/declarations/src/types.d.ts +51 -62
  36. package/dist/{interpreter-e58ca48d.development.cjs.js → interpreter-03a5c3f5.development.cjs.js} +91 -12
  37. package/dist/{interpreter-8def682e.esm.js → interpreter-1e8c1c0c.esm.js} +91 -12
  38. package/dist/{interpreter-97aff8d2.cjs.js → interpreter-5dfcd203.cjs.js} +91 -12
  39. package/dist/{interpreter-1c52b23c.development.esm.js → interpreter-70cd9217.development.esm.js} +91 -12
  40. package/dist/{raise-1fd59c65.development.cjs.js → raise-17cb3d9d.development.cjs.js} +145 -152
  41. package/dist/{raise-800296d7.cjs.js → raise-291d2181.cjs.js} +145 -152
  42. package/dist/{raise-21c417c1.esm.js → raise-62de3670.esm.js} +145 -153
  43. package/dist/{raise-e342a840.development.esm.js → raise-e044f460.development.esm.js} +145 -153
  44. package/dist/{send-92854675.esm.js → send-1249d4ac.esm.js} +45 -44
  45. package/dist/{send-b309ef4e.development.cjs.js → send-33433787.development.cjs.js} +45 -44
  46. package/dist/{send-4cc29786.cjs.js → send-af152aca.cjs.js} +45 -44
  47. package/dist/{send-83ccc98b.development.esm.js → send-f1a2a827.development.esm.js} +45 -44
  48. package/dist/xstate.cjs.js +23 -20
  49. package/dist/xstate.cjs.mjs +1 -0
  50. package/dist/xstate.development.cjs.js +23 -20
  51. package/dist/xstate.development.cjs.mjs +1 -0
  52. package/dist/xstate.development.esm.js +25 -23
  53. package/dist/xstate.esm.js +25 -23
  54. package/dist/xstate.umd.min.js +1 -1
  55. package/dist/xstate.umd.min.js.map +1 -1
  56. package/guards/dist/xstate-guards.cjs.js +2 -2
  57. package/guards/dist/xstate-guards.development.cjs.js +2 -2
  58. package/guards/dist/xstate-guards.development.esm.js +2 -2
  59. package/guards/dist/xstate-guards.esm.js +2 -2
  60. package/guards/dist/xstate-guards.umd.min.js +1 -1
  61. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  62. package/package.json +1 -1
@@ -356,6 +356,10 @@ const defaultOptions = {
356
356
  logger: console.log.bind(console),
357
357
  devTools: false
358
358
  };
359
+
360
+ /**
361
+ * An Actor is a running process that can receive events, send events and change its behavior based on the events it receives, which can cause effects outside of the actor. When you run a state machine, it becomes an actor.
362
+ */
359
363
  class Actor {
360
364
  /**
361
365
  * The current internal state of the actor.
@@ -381,6 +385,10 @@ class Actor {
381
385
  * The globally unique process ID for this invocation.
382
386
  */
383
387
 
388
+ /**
389
+ * The system to which this actor belongs.
390
+ */
391
+
384
392
  /**
385
393
  * Creates a new actor instance for the given logic with the provided options, if any.
386
394
  *
@@ -400,7 +408,7 @@ class Actor {
400
408
  this.status = ActorStatus.NotStarted;
401
409
  this._parent = void 0;
402
410
  this.ref = void 0;
403
- this._actorContext = void 0;
411
+ this._actorScope = void 0;
404
412
  this._systemId = void 0;
405
413
  this.sessionId = void 0;
406
414
  this.system = void 0;
@@ -424,10 +432,6 @@ class Actor {
424
432
  // Always inspect at the system-level
425
433
  this.system.inspect(toObserver(inspect));
426
434
  }
427
- if (systemId) {
428
- this._systemId = systemId;
429
- this.system._set(systemId, this);
430
- }
431
435
  this.sessionId = this.system._bookId();
432
436
  this.id = id ?? this.sessionId;
433
437
  this.logger = logger;
@@ -436,7 +440,7 @@ class Actor {
436
440
  this.options = resolvedOptions;
437
441
  this.src = resolvedOptions.src;
438
442
  this.ref = this;
439
- this._actorContext = {
443
+ this._actorScope = {
440
444
  self: this,
441
445
  id: this.id,
442
446
  sessionId: this.sessionId,
@@ -461,9 +465,13 @@ class Actor {
461
465
  actorRef: this
462
466
  });
463
467
  this._initState();
468
+ if (systemId && this._state.status === 'active') {
469
+ this._systemId = systemId;
470
+ this.system._set(systemId, this);
471
+ }
464
472
  }
465
473
  _initState() {
466
- this._state = this.options.state ? this.logic.restoreState ? this.logic.restoreState(this.options.state, this._actorContext) : this.options.state : this.logic.getInitialState(this._actorContext, this.options?.input);
474
+ this._state = this.options.state ? this.logic.restoreState ? this.logic.restoreState(this.options.state, this._actorScope) : this.options.state : this.logic.getInitialState(this._actorScope, this.options?.input);
467
475
  }
468
476
 
469
477
  // array of functions to defer
@@ -509,6 +517,56 @@ class Actor {
509
517
  snapshot
510
518
  });
511
519
  }
520
+
521
+ /**
522
+ * Subscribe an observer to an actor’s snapshot values.
523
+ *
524
+ * @remarks
525
+ * The observer will receive the actor’s snapshot value when it is emitted. The observer can be:
526
+ * - A plain function that receives the latest snapshot, or
527
+ * - An observer object whose `.next(snapshot)` method receives the latest snapshot
528
+ *
529
+ * @example
530
+ * ```ts
531
+ * // Observer as a plain function
532
+ * const subscription = actor.subscribe((snapshot) => {
533
+ * console.log(snapshot);
534
+ * });
535
+ * ```
536
+ *
537
+ * @example
538
+ * ```ts
539
+ * // Observer as an object
540
+ * const subscription = actor.subscribe({
541
+ * next(snapshot) {
542
+ * console.log(snapshot);
543
+ * },
544
+ * error(err) {
545
+ * // ...
546
+ * },
547
+ * complete() {
548
+ * // ...
549
+ * },
550
+ * });
551
+ * ```
552
+ *
553
+ * The return value of `actor.subscribe(observer)` is a subscription object that has an `.unsubscribe()` method. You can call `subscription.unsubscribe()` to unsubscribe the observer:
554
+ *
555
+ * @example
556
+ * ```ts
557
+ * const subscription = actor.subscribe((snapshot) => {
558
+ * // ...
559
+ * });
560
+ *
561
+ * // Unsubscribe the observer
562
+ * subscription.unsubscribe();
563
+ * ```
564
+ *
565
+ * When the actor is stopped, all of its observers will automatically be unsubscribed.
566
+ *
567
+ * @param observer - Either a plain function that receives the latest snapshot, or an observer object whose `.next(snapshot)` method receives the latest snapshot
568
+ */
569
+
512
570
  subscribe(nextListenerOrObserver, errorListener, completeListener) {
513
571
  const observer = toObserver(nextListenerOrObserver, errorListener, completeListener);
514
572
  if (this.status !== ActorStatus.Stopped) {
@@ -560,7 +618,7 @@ class Actor {
560
618
  }
561
619
  if (this.logic.start) {
562
620
  try {
563
- this.logic.start(this._state, this._actorContext);
621
+ this.logic.start(this._state, this._actorScope);
564
622
  } catch (err) {
565
623
  this._stopProcedure();
566
624
  this._error(err);
@@ -584,7 +642,7 @@ class Actor {
584
642
  let nextState;
585
643
  let caughtError;
586
644
  try {
587
- nextState = this.logic.transition(this._state, event, this._actorContext);
645
+ nextState = this.logic.transition(this._state, event, this._actorScope);
588
646
  } catch (err) {
589
647
  // we wrap it in a box so we can rethrow it later even if falsy value gets caught here
590
648
  caughtError = {
@@ -704,7 +762,10 @@ class Actor {
704
762
  this.system._relay(undefined, this, event);
705
763
  }
706
764
 
707
- // TODO: make private (and figure out a way to do this within the machine)
765
+ /**
766
+ * TODO: figure out a way to do this within the machine
767
+ * @internal
768
+ */
708
769
  delaySend(params) {
709
770
  const {
710
771
  event,
@@ -721,7 +782,10 @@ class Actor {
721
782
  }
722
783
  }
723
784
 
724
- // TODO: make private (and figure out a way to do this within the machine)
785
+ /**
786
+ * TODO: figure out a way to do this within the machine
787
+ * @internal
788
+ */
725
789
  cancel(sendId) {
726
790
  this.clock.clearTimeout(this.delayedEventsMap[sendId]);
727
791
  delete this.delayedEventsMap[sendId];
@@ -742,11 +806,26 @@ class Actor {
742
806
  };
743
807
  }
744
808
  getPersistedState() {
745
- return this.logic.getPersistedState?.(this._state);
809
+ return this.logic.getPersistedState(this._state);
746
810
  }
747
811
  [symbolObservable]() {
748
812
  return this;
749
813
  }
814
+
815
+ /**
816
+ * Read an actor’s snapshot synchronously.
817
+ *
818
+ * @remarks
819
+ * The snapshot represent an actor's last emitted value.
820
+ *
821
+ * When an actor receives an event, its internal state may change.
822
+ * An actor may emit a snapshot when a state transition occurs.
823
+ *
824
+ * Note that some actors, such as callback actors generated with `fromCallback`, will not emit snapshots.
825
+ *
826
+ * @see {@link Actor.subscribe} to subscribe to an actor’s snapshot values.
827
+ * @see {@link Actor.getPersistedState} to persist the internal state of an actor (which is more than just a snapshot).
828
+ */
750
829
  getSnapshot() {
751
830
  return this._state;
752
831
  }
@@ -357,6 +357,10 @@ const defaultOptions = {
357
357
  logger: console.log.bind(console),
358
358
  devTools: false
359
359
  };
360
+
361
+ /**
362
+ * An Actor is a running process that can receive events, send events and change its behavior based on the events it receives, which can cause effects outside of the actor. When you run a state machine, it becomes an actor.
363
+ */
360
364
  class Actor {
361
365
  /**
362
366
  * The current internal state of the actor.
@@ -382,6 +386,10 @@ class Actor {
382
386
  * The globally unique process ID for this invocation.
383
387
  */
384
388
 
389
+ /**
390
+ * The system to which this actor belongs.
391
+ */
392
+
385
393
  /**
386
394
  * Creates a new actor instance for the given logic with the provided options, if any.
387
395
  *
@@ -401,7 +409,7 @@ class Actor {
401
409
  this.status = ActorStatus.NotStarted;
402
410
  this._parent = void 0;
403
411
  this.ref = void 0;
404
- this._actorContext = void 0;
412
+ this._actorScope = void 0;
405
413
  this._systemId = void 0;
406
414
  this.sessionId = void 0;
407
415
  this.system = void 0;
@@ -425,10 +433,6 @@ class Actor {
425
433
  // Always inspect at the system-level
426
434
  this.system.inspect(toObserver(inspect));
427
435
  }
428
- if (systemId) {
429
- this._systemId = systemId;
430
- this.system._set(systemId, this);
431
- }
432
436
  this.sessionId = this.system._bookId();
433
437
  this.id = id ?? this.sessionId;
434
438
  this.logger = logger;
@@ -437,7 +441,7 @@ class Actor {
437
441
  this.options = resolvedOptions;
438
442
  this.src = resolvedOptions.src;
439
443
  this.ref = this;
440
- this._actorContext = {
444
+ this._actorScope = {
441
445
  self: this,
442
446
  id: this.id,
443
447
  sessionId: this.sessionId,
@@ -462,9 +466,13 @@ class Actor {
462
466
  actorRef: this
463
467
  });
464
468
  this._initState();
469
+ if (systemId && this._state.status === 'active') {
470
+ this._systemId = systemId;
471
+ this.system._set(systemId, this);
472
+ }
465
473
  }
466
474
  _initState() {
467
- this._state = this.options.state ? this.logic.restoreState ? this.logic.restoreState(this.options.state, this._actorContext) : this.options.state : this.logic.getInitialState(this._actorContext, this.options?.input);
475
+ this._state = this.options.state ? this.logic.restoreState ? this.logic.restoreState(this.options.state, this._actorScope) : this.options.state : this.logic.getInitialState(this._actorScope, this.options?.input);
468
476
  }
469
477
 
470
478
  // array of functions to defer
@@ -510,6 +518,56 @@ class Actor {
510
518
  snapshot
511
519
  });
512
520
  }
521
+
522
+ /**
523
+ * Subscribe an observer to an actor’s snapshot values.
524
+ *
525
+ * @remarks
526
+ * The observer will receive the actor’s snapshot value when it is emitted. The observer can be:
527
+ * - A plain function that receives the latest snapshot, or
528
+ * - An observer object whose `.next(snapshot)` method receives the latest snapshot
529
+ *
530
+ * @example
531
+ * ```ts
532
+ * // Observer as a plain function
533
+ * const subscription = actor.subscribe((snapshot) => {
534
+ * console.log(snapshot);
535
+ * });
536
+ * ```
537
+ *
538
+ * @example
539
+ * ```ts
540
+ * // Observer as an object
541
+ * const subscription = actor.subscribe({
542
+ * next(snapshot) {
543
+ * console.log(snapshot);
544
+ * },
545
+ * error(err) {
546
+ * // ...
547
+ * },
548
+ * complete() {
549
+ * // ...
550
+ * },
551
+ * });
552
+ * ```
553
+ *
554
+ * The return value of `actor.subscribe(observer)` is a subscription object that has an `.unsubscribe()` method. You can call `subscription.unsubscribe()` to unsubscribe the observer:
555
+ *
556
+ * @example
557
+ * ```ts
558
+ * const subscription = actor.subscribe((snapshot) => {
559
+ * // ...
560
+ * });
561
+ *
562
+ * // Unsubscribe the observer
563
+ * subscription.unsubscribe();
564
+ * ```
565
+ *
566
+ * When the actor is stopped, all of its observers will automatically be unsubscribed.
567
+ *
568
+ * @param observer - Either a plain function that receives the latest snapshot, or an observer object whose `.next(snapshot)` method receives the latest snapshot
569
+ */
570
+
513
571
  subscribe(nextListenerOrObserver, errorListener, completeListener) {
514
572
  const observer = toObserver(nextListenerOrObserver, errorListener, completeListener);
515
573
  if (this.status !== ActorStatus.Stopped) {
@@ -561,7 +619,7 @@ class Actor {
561
619
  }
562
620
  if (this.logic.start) {
563
621
  try {
564
- this.logic.start(this._state, this._actorContext);
622
+ this.logic.start(this._state, this._actorScope);
565
623
  } catch (err) {
566
624
  this._stopProcedure();
567
625
  this._error(err);
@@ -585,7 +643,7 @@ class Actor {
585
643
  let nextState;
586
644
  let caughtError;
587
645
  try {
588
- nextState = this.logic.transition(this._state, event, this._actorContext);
646
+ nextState = this.logic.transition(this._state, event, this._actorScope);
589
647
  } catch (err) {
590
648
  // we wrap it in a box so we can rethrow it later even if falsy value gets caught here
591
649
  caughtError = {
@@ -713,7 +771,10 @@ class Actor {
713
771
  this.system._relay(undefined, this, event);
714
772
  }
715
773
 
716
- // TODO: make private (and figure out a way to do this within the machine)
774
+ /**
775
+ * TODO: figure out a way to do this within the machine
776
+ * @internal
777
+ */
717
778
  delaySend(params) {
718
779
  const {
719
780
  event,
@@ -730,7 +791,10 @@ class Actor {
730
791
  }
731
792
  }
732
793
 
733
- // TODO: make private (and figure out a way to do this within the machine)
794
+ /**
795
+ * TODO: figure out a way to do this within the machine
796
+ * @internal
797
+ */
734
798
  cancel(sendId) {
735
799
  this.clock.clearTimeout(this.delayedEventsMap[sendId]);
736
800
  delete this.delayedEventsMap[sendId];
@@ -751,11 +815,26 @@ class Actor {
751
815
  };
752
816
  }
753
817
  getPersistedState() {
754
- return this.logic.getPersistedState?.(this._state);
818
+ return this.logic.getPersistedState(this._state);
755
819
  }
756
820
  [symbolObservable]() {
757
821
  return this;
758
822
  }
823
+
824
+ /**
825
+ * Read an actor’s snapshot synchronously.
826
+ *
827
+ * @remarks
828
+ * The snapshot represent an actor's last emitted value.
829
+ *
830
+ * When an actor receives an event, its internal state may change.
831
+ * An actor may emit a snapshot when a state transition occurs.
832
+ *
833
+ * Note that some actors, such as callback actors generated with `fromCallback`, will not emit snapshots.
834
+ *
835
+ * @see {@link Actor.subscribe} to subscribe to an actor’s snapshot values.
836
+ * @see {@link Actor.getPersistedState} to persist the internal state of an actor (which is more than just a snapshot).
837
+ */
759
838
  getSnapshot() {
760
839
  return this._state;
761
840
  }