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.
- package/actions/dist/xstate-actions.cjs.js +3 -3
- package/actions/dist/xstate-actions.development.cjs.js +3 -3
- package/actions/dist/xstate-actions.development.esm.js +3 -3
- package/actions/dist/xstate-actions.esm.js +3 -3
- package/actions/dist/xstate-actions.umd.min.js +1 -1
- package/actions/dist/xstate-actions.umd.min.js.map +1 -1
- package/actors/dist/xstate-actors.cjs.js +60 -3
- package/actors/dist/xstate-actors.development.cjs.js +60 -3
- package/actors/dist/xstate-actors.development.esm.js +60 -3
- package/actors/dist/xstate-actors.esm.js +60 -3
- package/actors/dist/xstate-actors.umd.min.js +1 -1
- package/actors/dist/xstate-actors.umd.min.js.map +1 -1
- package/dist/declarations/src/StateMachine.d.ts +5 -5
- package/dist/declarations/src/actors/callback.d.ts +74 -0
- package/dist/declarations/src/actors/transition.d.ts +2 -2
- package/dist/declarations/src/interpreter.d.ts +70 -9
- package/dist/declarations/src/spawn.d.ts +2 -2
- package/dist/declarations/src/stateUtils.d.ts +4 -4
- package/dist/declarations/src/system.d.ts +7 -3
- package/dist/declarations/src/types.d.ts +7 -7
- package/dist/{interpreter-4005eb36.development.esm.js → interpreter-410d7ca9.development.esm.js} +92 -13
- package/dist/{interpreter-b6f22ee2.cjs.js → interpreter-586abde4.cjs.js} +92 -13
- package/dist/{interpreter-ed3f81f7.development.cjs.js → interpreter-bae5c279.development.cjs.js} +92 -13
- package/dist/{interpreter-c80ce92e.esm.js → interpreter-ed0fac7e.esm.js} +92 -13
- package/dist/{raise-7faa9b3b.cjs.js → raise-27909189.cjs.js} +83 -80
- package/dist/{raise-c989c7fb.esm.js → raise-2b2fdec3.esm.js} +83 -80
- package/dist/{raise-42073973.development.esm.js → raise-37f9f3b8.development.esm.js} +84 -81
- package/dist/{raise-b69a3d16.development.cjs.js → raise-8325e2df.development.cjs.js} +84 -81
- package/dist/{send-34160163.cjs.js → send-4fdf275e.cjs.js} +22 -22
- package/dist/{send-4b616da9.esm.js → send-59f66c58.esm.js} +22 -22
- package/dist/{send-58725522.development.cjs.js → send-c45d0d2c.development.cjs.js} +22 -22
- package/dist/{send-bff8c910.development.esm.js → send-f6b49072.development.esm.js} +22 -22
- package/dist/xstate.cjs.js +18 -17
- package/dist/xstate.development.cjs.js +18 -17
- package/dist/xstate.development.esm.js +21 -20
- package/dist/xstate.esm.js +21 -20
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/guards/dist/xstate-guards.cjs.js +2 -2
- package/guards/dist/xstate-guards.development.cjs.js +2 -2
- package/guards/dist/xstate-guards.development.esm.js +2 -2
- package/guards/dist/xstate-guards.esm.js +2 -2
- package/guards/dist/xstate-guards.umd.min.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ActorRefFrom,
|
|
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(
|
|
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 {
|
|
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,
|
|
49
|
-
export declare function resolveActionsAndContext(currentState: AnyState, event: AnyEventObject,
|
|
50
|
-
export declare function macrostep(state: AnyState, event: EventObject,
|
|
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
|
};
|
|
@@ -2,21 +2,25 @@ import { AnyEventObject, ActorSystem, ActorSystemInfo, AnyActorRef, Snapshot } f
|
|
|
2
2
|
export declare function createSystem<T extends ActorSystemInfo>(rootActor: AnyActorRef): ActorSystem<T>;
|
|
3
3
|
export interface BaseInspectionEventProperties {
|
|
4
4
|
rootId: string;
|
|
5
|
+
/**
|
|
6
|
+
* The relevant actorRef for the inspection event.
|
|
7
|
+
* - For snapshot events, this is the `actorRef` of the snapshot.
|
|
8
|
+
* - For event events, this is the target `actorRef` (recipient of event).
|
|
9
|
+
* - For actor events, this is the `actorRef` of the registered actor.
|
|
10
|
+
*/
|
|
11
|
+
actorRef: AnyActorRef;
|
|
5
12
|
}
|
|
6
13
|
export interface InspectedSnapshotEvent extends BaseInspectionEventProperties {
|
|
7
14
|
type: '@xstate.snapshot';
|
|
8
|
-
actorRef: AnyActorRef;
|
|
9
15
|
event: AnyEventObject;
|
|
10
16
|
snapshot: Snapshot<unknown>;
|
|
11
17
|
}
|
|
12
18
|
export interface InspectedEventEvent extends BaseInspectionEventProperties {
|
|
13
19
|
type: '@xstate.event';
|
|
14
20
|
sourceRef: AnyActorRef | undefined;
|
|
15
|
-
targetRef: AnyActorRef;
|
|
16
21
|
event: AnyEventObject;
|
|
17
22
|
}
|
|
18
23
|
export interface InspectedActorEvent extends BaseInspectionEventProperties {
|
|
19
24
|
type: '@xstate.actor';
|
|
20
|
-
actorRef: AnyActorRef;
|
|
21
25
|
}
|
|
22
26
|
export type InspectionEvent = InspectedSnapshotEvent | InspectedEventEvent | InspectedActorEvent;
|
|
@@ -658,7 +658,7 @@ any, // tag
|
|
|
658
658
|
any, // input
|
|
659
659
|
any, // output
|
|
660
660
|
infer TResolvedTypesMeta> ? TResolvedTypesMeta : never;
|
|
661
|
-
export interface
|
|
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
|
|
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:
|
|
691
|
-
getInitialState: (
|
|
692
|
-
restoreState?: (persistedState: Snapshot<unknown>,
|
|
693
|
-
start?: (state: TSnapshot,
|
|
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
|
|
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>;
|
package/dist/{interpreter-4005eb36.development.esm.js → interpreter-410d7ca9.development.esm.js}
RENAMED
|
@@ -172,7 +172,7 @@ function createSystem(rootActor) {
|
|
|
172
172
|
system._sendInspectionEvent({
|
|
173
173
|
type: '@xstate.event',
|
|
174
174
|
sourceRef: source,
|
|
175
|
-
|
|
175
|
+
actorRef: target,
|
|
176
176
|
event
|
|
177
177
|
});
|
|
178
178
|
target._send(event);
|
|
@@ -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.
|
|
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.
|
|
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.
|
|
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) {
|
|
@@ -545,7 +603,7 @@ class Actor {
|
|
|
545
603
|
this.system._sendInspectionEvent({
|
|
546
604
|
type: '@xstate.event',
|
|
547
605
|
sourceRef: this._parent,
|
|
548
|
-
|
|
606
|
+
actorRef: this,
|
|
549
607
|
event: initEvent
|
|
550
608
|
});
|
|
551
609
|
const status = this._state.status;
|
|
@@ -561,7 +619,7 @@ class Actor {
|
|
|
561
619
|
}
|
|
562
620
|
if (this.logic.start) {
|
|
563
621
|
try {
|
|
564
|
-
this.logic.start(this._state, this.
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
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];
|
|
@@ -756,6 +820,21 @@ class Actor {
|
|
|
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
|
}
|
|
@@ -174,7 +174,7 @@ function createSystem(rootActor) {
|
|
|
174
174
|
system._sendInspectionEvent({
|
|
175
175
|
type: '@xstate.event',
|
|
176
176
|
sourceRef: source,
|
|
177
|
-
|
|
177
|
+
actorRef: target,
|
|
178
178
|
event
|
|
179
179
|
});
|
|
180
180
|
target._send(event);
|
|
@@ -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.
|
|
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.
|
|
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.
|
|
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) {
|
|
@@ -544,7 +602,7 @@ class Actor {
|
|
|
544
602
|
this.system._sendInspectionEvent({
|
|
545
603
|
type: '@xstate.event',
|
|
546
604
|
sourceRef: this._parent,
|
|
547
|
-
|
|
605
|
+
actorRef: this,
|
|
548
606
|
event: initEvent
|
|
549
607
|
});
|
|
550
608
|
const status = this._state.status;
|
|
@@ -560,7 +618,7 @@ class Actor {
|
|
|
560
618
|
}
|
|
561
619
|
if (this.logic.start) {
|
|
562
620
|
try {
|
|
563
|
-
this.logic.start(this._state, this.
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
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
|
}
|