xstate 5.0.0-beta.38 → 5.0.0-beta.40

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 +3 -3
  2. package/actions/dist/xstate-actions.development.cjs.js +3 -3
  3. package/actions/dist/xstate-actions.development.esm.js +3 -3
  4. package/actions/dist/xstate-actions.esm.js +3 -3
  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 +60 -3
  8. package/actors/dist/xstate-actors.development.cjs.js +60 -3
  9. package/actors/dist/xstate-actors.development.esm.js +60 -3
  10. package/actors/dist/xstate-actors.esm.js +60 -3
  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/StateMachine.d.ts +5 -5
  14. package/dist/declarations/src/actors/callback.d.ts +74 -0
  15. package/dist/declarations/src/actors/transition.d.ts +2 -2
  16. package/dist/declarations/src/interpreter.d.ts +70 -9
  17. package/dist/declarations/src/spawn.d.ts +2 -2
  18. package/dist/declarations/src/stateUtils.d.ts +4 -4
  19. package/dist/declarations/src/system.d.ts +7 -3
  20. package/dist/declarations/src/types.d.ts +7 -7
  21. package/dist/{interpreter-4005eb36.development.esm.js → interpreter-410d7ca9.development.esm.js} +92 -13
  22. package/dist/{interpreter-b6f22ee2.cjs.js → interpreter-586abde4.cjs.js} +92 -13
  23. package/dist/{interpreter-ed3f81f7.development.cjs.js → interpreter-bae5c279.development.cjs.js} +92 -13
  24. package/dist/{interpreter-c80ce92e.esm.js → interpreter-ed0fac7e.esm.js} +92 -13
  25. package/dist/{raise-7faa9b3b.cjs.js → raise-27909189.cjs.js} +83 -80
  26. package/dist/{raise-c989c7fb.esm.js → raise-2b2fdec3.esm.js} +83 -80
  27. package/dist/{raise-42073973.development.esm.js → raise-37f9f3b8.development.esm.js} +84 -81
  28. package/dist/{raise-b69a3d16.development.cjs.js → raise-8325e2df.development.cjs.js} +84 -81
  29. package/dist/{send-34160163.cjs.js → send-4fdf275e.cjs.js} +22 -22
  30. package/dist/{send-4b616da9.esm.js → send-59f66c58.esm.js} +22 -22
  31. package/dist/{send-58725522.development.cjs.js → send-c45d0d2c.development.cjs.js} +22 -22
  32. package/dist/{send-bff8c910.development.esm.js → send-f6b49072.development.esm.js} +22 -22
  33. package/dist/xstate.cjs.js +18 -17
  34. package/dist/xstate.development.cjs.js +18 -17
  35. package/dist/xstate.development.esm.js +21 -20
  36. package/dist/xstate.esm.js +21 -20
  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 +2 -2
  40. package/guards/dist/xstate-guards.development.cjs.js +2 -2
  41. package/guards/dist/xstate-guards.development.esm.js +2 -2
  42. package/guards/dist/xstate-guards.esm.js +2 -2
  43. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  44. package/package.json +1 -1
@@ -174,7 +174,7 @@ function createSystem(rootActor) {
174
174
  system._sendInspectionEvent({
175
175
  type: '@xstate.event',
176
176
  sourceRef: source,
177
- targetRef: target,
177
+ actorRef: target,
178
178
  event
179
179
  });
180
180
  target._send(event);
@@ -359,6 +359,10 @@ const defaultOptions = {
359
359
  logger: console.log.bind(console),
360
360
  devTools: false
361
361
  };
362
+
363
+ /**
364
+ * 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.
365
+ */
362
366
  class Actor {
363
367
  /**
364
368
  * The current internal state of the actor.
@@ -384,6 +388,10 @@ class Actor {
384
388
  * The globally unique process ID for this invocation.
385
389
  */
386
390
 
391
+ /**
392
+ * The system to which this actor belongs.
393
+ */
394
+
387
395
  /**
388
396
  * Creates a new actor instance for the given logic with the provided options, if any.
389
397
  *
@@ -403,7 +411,7 @@ class Actor {
403
411
  this.status = ActorStatus.NotStarted;
404
412
  this._parent = void 0;
405
413
  this.ref = void 0;
406
- this._actorContext = void 0;
414
+ this._actorScope = void 0;
407
415
  this._systemId = void 0;
408
416
  this.sessionId = void 0;
409
417
  this.system = void 0;
@@ -427,10 +435,6 @@ class Actor {
427
435
  // Always inspect at the system-level
428
436
  this.system.inspect(toObserver(inspect));
429
437
  }
430
- if (systemId) {
431
- this._systemId = systemId;
432
- this.system._set(systemId, this);
433
- }
434
438
  this.sessionId = this.system._bookId();
435
439
  this.id = id ?? this.sessionId;
436
440
  this.logger = logger;
@@ -439,7 +443,7 @@ class Actor {
439
443
  this.options = resolvedOptions;
440
444
  this.src = resolvedOptions.src;
441
445
  this.ref = this;
442
- this._actorContext = {
446
+ this._actorScope = {
443
447
  self: this,
444
448
  id: this.id,
445
449
  sessionId: this.sessionId,
@@ -464,9 +468,13 @@ class Actor {
464
468
  actorRef: this
465
469
  });
466
470
  this._initState();
471
+ if (systemId && this._state.status === 'active') {
472
+ this._systemId = systemId;
473
+ this.system._set(systemId, this);
474
+ }
467
475
  }
468
476
  _initState() {
469
- 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);
477
+ 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);
470
478
  }
471
479
 
472
480
  // array of functions to defer
@@ -512,6 +520,56 @@ class Actor {
512
520
  snapshot
513
521
  });
514
522
  }
523
+
524
+ /**
525
+ * Subscribe an observer to an actor’s snapshot values.
526
+ *
527
+ * @remarks
528
+ * The observer will receive the actor’s snapshot value when it is emitted. The observer can be:
529
+ * - A plain function that receives the latest snapshot, or
530
+ * - An observer object whose `.next(snapshot)` method receives the latest snapshot
531
+ *
532
+ * @example
533
+ * ```ts
534
+ * // Observer as a plain function
535
+ * const subscription = actor.subscribe((snapshot) => {
536
+ * console.log(snapshot);
537
+ * });
538
+ * ```
539
+ *
540
+ * @example
541
+ * ```ts
542
+ * // Observer as an object
543
+ * const subscription = actor.subscribe({
544
+ * next(snapshot) {
545
+ * console.log(snapshot);
546
+ * },
547
+ * error(err) {
548
+ * // ...
549
+ * },
550
+ * complete() {
551
+ * // ...
552
+ * },
553
+ * });
554
+ * ```
555
+ *
556
+ * 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:
557
+ *
558
+ * @example
559
+ * ```ts
560
+ * const subscription = actor.subscribe((snapshot) => {
561
+ * // ...
562
+ * });
563
+ *
564
+ * // Unsubscribe the observer
565
+ * subscription.unsubscribe();
566
+ * ```
567
+ *
568
+ * When the actor is stopped, all of its observers will automatically be unsubscribed.
569
+ *
570
+ * @param observer - Either a plain function that receives the latest snapshot, or an observer object whose `.next(snapshot)` method receives the latest snapshot
571
+ */
572
+
515
573
  subscribe(nextListenerOrObserver, errorListener, completeListener) {
516
574
  const observer = toObserver(nextListenerOrObserver, errorListener, completeListener);
517
575
  if (this.status !== ActorStatus.Stopped) {
@@ -547,7 +605,7 @@ class Actor {
547
605
  this.system._sendInspectionEvent({
548
606
  type: '@xstate.event',
549
607
  sourceRef: this._parent,
550
- targetRef: this,
608
+ actorRef: this,
551
609
  event: initEvent
552
610
  });
553
611
  const status = this._state.status;
@@ -563,7 +621,7 @@ class Actor {
563
621
  }
564
622
  if (this.logic.start) {
565
623
  try {
566
- this.logic.start(this._state, this._actorContext);
624
+ this.logic.start(this._state, this._actorScope);
567
625
  } catch (err) {
568
626
  this._stopProcedure();
569
627
  this._error(err);
@@ -587,7 +645,7 @@ class Actor {
587
645
  let nextState;
588
646
  let caughtError;
589
647
  try {
590
- nextState = this.logic.transition(this._state, event, this._actorContext);
648
+ nextState = this.logic.transition(this._state, event, this._actorScope);
591
649
  } catch (err) {
592
650
  // we wrap it in a box so we can rethrow it later even if falsy value gets caught here
593
651
  caughtError = {
@@ -715,7 +773,10 @@ class Actor {
715
773
  this.system._relay(undefined, this, event);
716
774
  }
717
775
 
718
- // TODO: make private (and figure out a way to do this within the machine)
776
+ /**
777
+ * TODO: figure out a way to do this within the machine
778
+ * @internal
779
+ */
719
780
  delaySend(params) {
720
781
  const {
721
782
  event,
@@ -732,7 +793,10 @@ class Actor {
732
793
  }
733
794
  }
734
795
 
735
- // TODO: make private (and figure out a way to do this within the machine)
796
+ /**
797
+ * TODO: figure out a way to do this within the machine
798
+ * @internal
799
+ */
736
800
  cancel(sendId) {
737
801
  this.clock.clearTimeout(this.delayedEventsMap[sendId]);
738
802
  delete this.delayedEventsMap[sendId];
@@ -758,6 +822,21 @@ class Actor {
758
822
  [symbolObservable]() {
759
823
  return this;
760
824
  }
825
+
826
+ /**
827
+ * Read an actor’s snapshot synchronously.
828
+ *
829
+ * @remarks
830
+ * The snapshot represent an actor's last emitted value.
831
+ *
832
+ * When an actor receives an event, its internal state may change.
833
+ * An actor may emit a snapshot when a state transition occurs.
834
+ *
835
+ * Note that some actors, such as callback actors generated with `fromCallback`, will not emit snapshots.
836
+ *
837
+ * @see {@link Actor.subscribe} to subscribe to an actor’s snapshot values.
838
+ * @see {@link Actor.getPersistedState} to persist the internal state of an actor (which is more than just a snapshot).
839
+ */
761
840
  getSnapshot() {
762
841
  return this._state;
763
842
  }
@@ -172,7 +172,7 @@ function createSystem(rootActor) {
172
172
  system._sendInspectionEvent({
173
173
  type: '@xstate.event',
174
174
  sourceRef: source,
175
- targetRef: target,
175
+ actorRef: target,
176
176
  event
177
177
  });
178
178
  target._send(event);
@@ -354,6 +354,10 @@ const defaultOptions = {
354
354
  logger: console.log.bind(console),
355
355
  devTools: false
356
356
  };
357
+
358
+ /**
359
+ * 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.
360
+ */
357
361
  class Actor {
358
362
  /**
359
363
  * The current internal state of the actor.
@@ -379,6 +383,10 @@ class Actor {
379
383
  * The globally unique process ID for this invocation.
380
384
  */
381
385
 
386
+ /**
387
+ * The system to which this actor belongs.
388
+ */
389
+
382
390
  /**
383
391
  * Creates a new actor instance for the given logic with the provided options, if any.
384
392
  *
@@ -398,7 +406,7 @@ class Actor {
398
406
  this.status = ActorStatus.NotStarted;
399
407
  this._parent = void 0;
400
408
  this.ref = void 0;
401
- this._actorContext = void 0;
409
+ this._actorScope = void 0;
402
410
  this._systemId = void 0;
403
411
  this.sessionId = void 0;
404
412
  this.system = void 0;
@@ -422,10 +430,6 @@ class Actor {
422
430
  // Always inspect at the system-level
423
431
  this.system.inspect(toObserver(inspect));
424
432
  }
425
- if (systemId) {
426
- this._systemId = systemId;
427
- this.system._set(systemId, this);
428
- }
429
433
  this.sessionId = this.system._bookId();
430
434
  this.id = id ?? this.sessionId;
431
435
  this.logger = logger;
@@ -434,7 +438,7 @@ class Actor {
434
438
  this.options = resolvedOptions;
435
439
  this.src = resolvedOptions.src;
436
440
  this.ref = this;
437
- this._actorContext = {
441
+ this._actorScope = {
438
442
  self: this,
439
443
  id: this.id,
440
444
  sessionId: this.sessionId,
@@ -459,9 +463,13 @@ class Actor {
459
463
  actorRef: this
460
464
  });
461
465
  this._initState();
466
+ if (systemId && this._state.status === 'active') {
467
+ this._systemId = systemId;
468
+ this.system._set(systemId, this);
469
+ }
462
470
  }
463
471
  _initState() {
464
- 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);
472
+ 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);
465
473
  }
466
474
 
467
475
  // array of functions to defer
@@ -507,6 +515,56 @@ class Actor {
507
515
  snapshot
508
516
  });
509
517
  }
518
+
519
+ /**
520
+ * Subscribe an observer to an actor’s snapshot values.
521
+ *
522
+ * @remarks
523
+ * The observer will receive the actor’s snapshot value when it is emitted. The observer can be:
524
+ * - A plain function that receives the latest snapshot, or
525
+ * - An observer object whose `.next(snapshot)` method receives the latest snapshot
526
+ *
527
+ * @example
528
+ * ```ts
529
+ * // Observer as a plain function
530
+ * const subscription = actor.subscribe((snapshot) => {
531
+ * console.log(snapshot);
532
+ * });
533
+ * ```
534
+ *
535
+ * @example
536
+ * ```ts
537
+ * // Observer as an object
538
+ * const subscription = actor.subscribe({
539
+ * next(snapshot) {
540
+ * console.log(snapshot);
541
+ * },
542
+ * error(err) {
543
+ * // ...
544
+ * },
545
+ * complete() {
546
+ * // ...
547
+ * },
548
+ * });
549
+ * ```
550
+ *
551
+ * 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:
552
+ *
553
+ * @example
554
+ * ```ts
555
+ * const subscription = actor.subscribe((snapshot) => {
556
+ * // ...
557
+ * });
558
+ *
559
+ * // Unsubscribe the observer
560
+ * subscription.unsubscribe();
561
+ * ```
562
+ *
563
+ * When the actor is stopped, all of its observers will automatically be unsubscribed.
564
+ *
565
+ * @param observer - Either a plain function that receives the latest snapshot, or an observer object whose `.next(snapshot)` method receives the latest snapshot
566
+ */
567
+
510
568
  subscribe(nextListenerOrObserver, errorListener, completeListener) {
511
569
  const observer = toObserver(nextListenerOrObserver, errorListener, completeListener);
512
570
  if (this.status !== ActorStatus.Stopped) {
@@ -542,7 +600,7 @@ class Actor {
542
600
  this.system._sendInspectionEvent({
543
601
  type: '@xstate.event',
544
602
  sourceRef: this._parent,
545
- targetRef: this,
603
+ actorRef: this,
546
604
  event: initEvent
547
605
  });
548
606
  const status = this._state.status;
@@ -558,7 +616,7 @@ class Actor {
558
616
  }
559
617
  if (this.logic.start) {
560
618
  try {
561
- this.logic.start(this._state, this._actorContext);
619
+ this.logic.start(this._state, this._actorScope);
562
620
  } catch (err) {
563
621
  this._stopProcedure();
564
622
  this._error(err);
@@ -582,7 +640,7 @@ class Actor {
582
640
  let nextState;
583
641
  let caughtError;
584
642
  try {
585
- nextState = this.logic.transition(this._state, event, this._actorContext);
643
+ nextState = this.logic.transition(this._state, event, this._actorScope);
586
644
  } catch (err) {
587
645
  // we wrap it in a box so we can rethrow it later even if falsy value gets caught here
588
646
  caughtError = {
@@ -702,7 +760,10 @@ class Actor {
702
760
  this.system._relay(undefined, this, event);
703
761
  }
704
762
 
705
- // TODO: make private (and figure out a way to do this within the machine)
763
+ /**
764
+ * TODO: figure out a way to do this within the machine
765
+ * @internal
766
+ */
706
767
  delaySend(params) {
707
768
  const {
708
769
  event,
@@ -719,7 +780,10 @@ class Actor {
719
780
  }
720
781
  }
721
782
 
722
- // TODO: make private (and figure out a way to do this within the machine)
783
+ /**
784
+ * TODO: figure out a way to do this within the machine
785
+ * @internal
786
+ */
723
787
  cancel(sendId) {
724
788
  this.clock.clearTimeout(this.delayedEventsMap[sendId]);
725
789
  delete this.delayedEventsMap[sendId];
@@ -745,6 +809,21 @@ class Actor {
745
809
  [symbolObservable]() {
746
810
  return this;
747
811
  }
812
+
813
+ /**
814
+ * Read an actor’s snapshot synchronously.
815
+ *
816
+ * @remarks
817
+ * The snapshot represent an actor's last emitted value.
818
+ *
819
+ * When an actor receives an event, its internal state may change.
820
+ * An actor may emit a snapshot when a state transition occurs.
821
+ *
822
+ * Note that some actors, such as callback actors generated with `fromCallback`, will not emit snapshots.
823
+ *
824
+ * @see {@link Actor.subscribe} to subscribe to an actor’s snapshot values.
825
+ * @see {@link Actor.getPersistedState} to persist the internal state of an actor (which is more than just a snapshot).
826
+ */
748
827
  getSnapshot() {
749
828
  return this._state;
750
829
  }