xstate 5.0.0-beta.38 → 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 (43) 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/types.d.ts +7 -7
  20. package/dist/{interpreter-ed3f81f7.development.cjs.js → interpreter-03a5c3f5.development.cjs.js} +90 -11
  21. package/dist/{interpreter-c80ce92e.esm.js → interpreter-1e8c1c0c.esm.js} +90 -11
  22. package/dist/{interpreter-b6f22ee2.cjs.js → interpreter-5dfcd203.cjs.js} +90 -11
  23. package/dist/{interpreter-4005eb36.development.esm.js → interpreter-70cd9217.development.esm.js} +90 -11
  24. package/dist/{raise-b69a3d16.development.cjs.js → raise-17cb3d9d.development.cjs.js} +84 -81
  25. package/dist/{raise-7faa9b3b.cjs.js → raise-291d2181.cjs.js} +83 -80
  26. package/dist/{raise-c989c7fb.esm.js → raise-62de3670.esm.js} +83 -80
  27. package/dist/{raise-42073973.development.esm.js → raise-e044f460.development.esm.js} +84 -81
  28. package/dist/{send-4b616da9.esm.js → send-1249d4ac.esm.js} +22 -22
  29. package/dist/{send-58725522.development.cjs.js → send-33433787.development.cjs.js} +22 -22
  30. package/dist/{send-34160163.cjs.js → send-af152aca.cjs.js} +22 -22
  31. package/dist/{send-bff8c910.development.esm.js → send-f1a2a827.development.esm.js} +22 -22
  32. package/dist/xstate.cjs.js +17 -17
  33. package/dist/xstate.development.cjs.js +17 -17
  34. package/dist/xstate.development.esm.js +20 -20
  35. package/dist/xstate.esm.js +20 -20
  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 +2 -2
  39. package/guards/dist/xstate-guards.development.cjs.js +2 -2
  40. package/guards/dist/xstate-guards.development.esm.js +2 -2
  41. package/guards/dist/xstate-guards.esm.js +2 -2
  42. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  43. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- import { ActorRefFrom, AnyActorContext, AnyActorLogic, AnyActorRef, AnyEventObject, AnyState, InputFrom, IsLiteralString, ProvidedActor } from "./types.js";
1
+ import { ActorRefFrom, AnyActorScope, AnyActorLogic, AnyActorRef, AnyEventObject, AnyState, InputFrom, IsLiteralString, ProvidedActor } from "./types.js";
2
2
  export type SpawnOptions<TActor extends ProvidedActor, TSrc extends TActor['src']> = TActor extends {
3
3
  src: TSrc;
4
4
  } ? 'id' extends keyof TActor ? [
@@ -24,4 +24,4 @@ export type Spawner<TActor extends ProvidedActor> = IsLiteralString<TActor['src'
24
24
  input?: unknown;
25
25
  syncSnapshot?: boolean;
26
26
  }) => TLogic extends string ? AnyActorRef : ActorRefFrom<TLogic>;
27
- export declare function createSpawner(actorContext: AnyActorContext, { machine, context }: AnyState, event: AnyEventObject, spawnedChildren: Record<string, AnyActorRef>): Spawner<any>;
27
+ export declare function createSpawner(actorScope: AnyActorScope, { machine, context }: AnyState, event: AnyEventObject, spawnedChildren: Record<string, AnyActorRef>): Spawner<any>;
@@ -1,6 +1,6 @@
1
1
  import { State } from "./State.js";
2
2
  import type { StateNode } from "./StateNode.js";
3
- import { AnyActorContext, AnyEventObject, AnyHistoryValue, AnyState, AnyStateNode, AnyTransitionDefinition, DelayedTransitionDefinition, EventObject, InitialTransitionConfig, InitialTransitionDefinition, MachineContext, StateValue, StateValueMap, TransitionDefinition, TODO, UnknownAction, AnyTransitionConfig } from "./types.js";
3
+ import { AnyEventObject, AnyHistoryValue, AnyState, AnyStateNode, AnyTransitionDefinition, DelayedTransitionDefinition, EventObject, InitialTransitionConfig, InitialTransitionDefinition, MachineContext, StateValue, StateValueMap, TransitionDefinition, TODO, UnknownAction, AnyTransitionConfig, AnyActorScope } from "./types.js";
4
4
  type Configuration<TContext extends MachineContext, TE extends EventObject> = Iterable<StateNode<TContext, TE>>;
5
5
  type AnyConfiguration = Configuration<any, any>;
6
6
  type AdjList = Map<AnyStateNode, Array<AnyStateNode>>;
@@ -45,9 +45,9 @@ export declare function removeConflictingTransitions(enabledTransitions: Array<A
45
45
  /**
46
46
  * https://www.w3.org/TR/scxml/#microstepProcedure
47
47
  */
48
- export declare function microstep<TContext extends MachineContext, TEvent extends EventObject>(transitions: Array<AnyTransitionDefinition>, currentState: AnyState, actorCtx: AnyActorContext, event: AnyEventObject, isInitial: boolean, internalQueue: Array<AnyEventObject>): AnyState;
49
- export declare function resolveActionsAndContext(currentState: AnyState, event: AnyEventObject, actorCtx: AnyActorContext, actions: UnknownAction[], internalQueue: AnyEventObject[], deferredActorIds?: string[]): AnyState;
50
- export declare function macrostep(state: AnyState, event: EventObject, actorCtx: AnyActorContext, internalQueue?: AnyEventObject[]): {
48
+ export declare function microstep<TContext extends MachineContext, TEvent extends EventObject>(transitions: Array<AnyTransitionDefinition>, currentState: AnyState, actorScope: AnyActorScope, event: AnyEventObject, isInitial: boolean, internalQueue: Array<AnyEventObject>): AnyState;
49
+ export declare function resolveActionsAndContext(currentState: AnyState, event: AnyEventObject, actorScope: AnyActorScope, actions: UnknownAction[], internalQueue: AnyEventObject[], deferredActorIds?: string[]): AnyState;
50
+ export declare function macrostep(state: AnyState, event: EventObject, actorScope: AnyActorScope, internalQueue?: AnyEventObject[]): {
51
51
  state: typeof state;
52
52
  microstates: Array<typeof state>;
53
53
  };
@@ -658,7 +658,7 @@ any, // tag
658
658
  any, // input
659
659
  any, // output
660
660
  infer TResolvedTypesMeta> ? TResolvedTypesMeta : never;
661
- export interface ActorContext<TSnapshot extends Snapshot<unknown>, TEvent extends EventObject, TSystem extends ActorSystem<any> = ActorSystem<any>> {
661
+ export interface ActorScope<TSnapshot extends Snapshot<unknown>, TEvent extends EventObject, TSystem extends ActorSystem<any> = ActorSystem<any>> {
662
662
  self: ActorRef<TEvent, TSnapshot>;
663
663
  id: string;
664
664
  sessionId: string;
@@ -667,7 +667,7 @@ export interface ActorContext<TSnapshot extends Snapshot<unknown>, TEvent extend
667
667
  system: TSystem;
668
668
  stopChild: (child: AnyActorRef) => void;
669
669
  }
670
- export type AnyActorContext = ActorContext<any, any, AnyActorSystem>;
670
+ export type AnyActorScope = ActorScope<any, any, AnyActorSystem>;
671
671
  export type Snapshot<TOutput> = {
672
672
  status: 'active';
673
673
  output: undefined;
@@ -687,10 +687,10 @@ export type Snapshot<TOutput> = {
687
687
  };
688
688
  export interface ActorLogic<TSnapshot extends Snapshot<unknown>, TEvent extends EventObject, TInput = unknown, TSystem extends ActorSystem<any> = ActorSystem<any>> {
689
689
  config?: unknown;
690
- transition: (state: TSnapshot, message: TEvent, ctx: ActorContext<TSnapshot, TEvent, TSystem>) => TSnapshot;
691
- getInitialState: (actorCtx: ActorContext<TSnapshot, TEvent, TSystem>, input: TInput) => TSnapshot;
692
- restoreState?: (persistedState: Snapshot<unknown>, actorCtx: ActorContext<TSnapshot, TEvent>) => TSnapshot;
693
- start?: (state: TSnapshot, actorCtx: ActorContext<TSnapshot, TEvent>) => void;
690
+ transition: (state: TSnapshot, message: TEvent, ctx: ActorScope<TSnapshot, TEvent, TSystem>) => TSnapshot;
691
+ getInitialState: (actorScope: ActorScope<TSnapshot, TEvent, TSystem>, input: TInput) => TSnapshot;
692
+ restoreState?: (persistedState: Snapshot<unknown>, actorScope: ActorScope<TSnapshot, TEvent>) => TSnapshot;
693
+ start?: (state: TSnapshot, actorScope: ActorScope<TSnapshot, TEvent>) => void;
694
694
  /**
695
695
  * @returns Persisted state
696
696
  */
@@ -700,7 +700,7 @@ export type AnyActorLogic = ActorLogic<any, // snapshot
700
700
  any, // event
701
701
  any, // input
702
702
  any>;
703
- export type SnapshotFrom<T> = ReturnTypeOrValue<T> extends infer R ? R extends ActorRef<infer _, infer TSnapshot> ? TSnapshot : R extends Actor<infer TLogic> ? SnapshotFrom<TLogic> : R extends StateMachine<infer _TContext, infer _TEvent, infer _TActor, infer _TAction, infer _TGuard, infer _TDelay, infer _TTag, infer _TInput, infer _TOutput, infer _TResolvedTypesMeta> ? StateFrom<R> : R extends ActorLogic<any, any, any, any> ? ReturnType<R['transition']> : R extends ActorContext<infer TSnapshot, infer _, infer __> ? TSnapshot : never : never;
703
+ export type SnapshotFrom<T> = ReturnTypeOrValue<T> extends infer R ? R extends ActorRef<infer _, infer TSnapshot> ? TSnapshot : R extends Actor<infer TLogic> ? SnapshotFrom<TLogic> : R extends StateMachine<infer _TContext, infer _TEvent, infer _TActor, infer _TAction, infer _TGuard, infer _TDelay, infer _TTag, infer _TInput, infer _TOutput, infer _TResolvedTypesMeta> ? StateFrom<R> : R extends ActorLogic<any, any, any, any> ? ReturnType<R['transition']> : R extends ActorScope<infer TSnapshot, infer _, infer __> ? TSnapshot : never : never;
704
704
  export type EventFromLogic<TLogic extends ActorLogic<any, any, any, any>> = TLogic extends ActorLogic<infer _, infer TEvent, infer __, infer _____> ? TEvent : never;
705
705
  type ResolveEventType<T> = ReturnTypeOrValue<T> extends infer R ? R extends StateMachine<infer _TContext, infer TEvent, infer _TActor, infer _TAction, infer _TGuard, infer _TDelay, infer _TTag, infer _TInput, infer _TOutput, infer _TResolvedTypesMeta> ? TEvent : R extends State<infer _TContext, infer TEvent, infer _TActor, infer _TOutput, infer _TResolvedTypesMeta> ? TEvent : R extends ActorRef<infer TEvent, infer _> ? TEvent : never : never;
706
706
  export type EventFrom<T, K extends Prop<TEvent, 'type'> = never, TEvent extends EventObject = ResolveEventType<T>> = IsNever<K> extends true ? TEvent : ExtractEvent<TEvent, K>;
@@ -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) {
@@ -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
  }
@@ -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) {
@@ -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
  }
@@ -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];
@@ -747,6 +811,21 @@ class Actor {
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
  }