xstate 4.20.1 → 4.23.1

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 (92) hide show
  1. package/CHANGELOG.md +134 -0
  2. package/dist/xstate.interpreter.js +1 -1
  3. package/dist/xstate.js +1 -1
  4. package/dist/xstate.web.js +2 -2
  5. package/es/Actor.js +17 -5
  6. package/es/Machine.d.ts +6 -3
  7. package/es/Machine.js +3 -6
  8. package/es/State.js +1 -3
  9. package/es/StateNode.d.ts +4 -3
  10. package/es/StateNode.js +34 -20
  11. package/es/_virtual/_tslib.js +59 -73
  12. package/es/actionTypes.js +3 -2
  13. package/es/actions.d.ts +1 -1
  14. package/es/actions.js +51 -37
  15. package/es/behaviors.d.ts +37 -0
  16. package/es/behaviors.js +65 -0
  17. package/es/constants.js +2 -1
  18. package/es/devTools.js +1 -1
  19. package/es/environment.js +2 -1
  20. package/es/index.js +3 -1
  21. package/es/interpreter.d.ts +10 -3
  22. package/es/interpreter.js +44 -22
  23. package/es/invokeUtils.js +4 -3
  24. package/es/mapState.js +1 -1
  25. package/es/match.js +1 -1
  26. package/es/model.d.ts +2 -36
  27. package/es/model.types.d.ts +37 -0
  28. package/es/registry.js +2 -1
  29. package/es/scheduler.js +2 -1
  30. package/es/schema.js +1 -1
  31. package/es/serviceScope.js +1 -3
  32. package/es/stateUtils.js +1 -9
  33. package/es/types.d.ts +26 -5
  34. package/es/types.js +1 -1
  35. package/es/utils.d.ts +3 -2
  36. package/es/utils.js +4 -40
  37. package/lib/Actor.d.ts +25 -25
  38. package/lib/Actor.js +85 -66
  39. package/lib/Machine.d.ts +17 -14
  40. package/lib/Machine.js +14 -14
  41. package/lib/SimulatedClock.d.ts +16 -16
  42. package/lib/State.d.ts +108 -108
  43. package/lib/State.js +246 -236
  44. package/lib/StateNode.d.ts +279 -278
  45. package/lib/StateNode.js +1535 -1339
  46. package/lib/_virtual/_tslib.js +81 -0
  47. package/lib/actionTypes.d.ts +19 -19
  48. package/lib/actionTypes.js +43 -23
  49. package/lib/actions.d.ts +138 -138
  50. package/lib/actions.js +465 -387
  51. package/lib/behaviors.d.ts +37 -0
  52. package/lib/behaviors.js +69 -0
  53. package/lib/constants.d.ts +5 -5
  54. package/lib/constants.js +13 -7
  55. package/lib/devTools.d.ts +15 -15
  56. package/lib/devTools.js +37 -26
  57. package/lib/each.d.ts +3 -3
  58. package/lib/environment.d.ts +1 -1
  59. package/lib/environment.js +7 -4
  60. package/lib/index.d.ts +30 -30
  61. package/lib/index.js +67 -57
  62. package/lib/interpreter.d.ts +205 -198
  63. package/lib/interpreter.js +1306 -1054
  64. package/lib/invoke.d.ts +10 -10
  65. package/lib/invokeUtils.d.ts +6 -6
  66. package/lib/invokeUtils.js +40 -37
  67. package/lib/json.d.ts +30 -30
  68. package/lib/mapState.d.ts +3 -3
  69. package/lib/mapState.js +31 -32
  70. package/lib/match.d.ts +8 -8
  71. package/lib/match.js +33 -47
  72. package/lib/model.d.ts +4 -38
  73. package/lib/model.js +5 -1
  74. package/lib/model.types.d.ts +37 -0
  75. package/lib/model.types.js +2 -0
  76. package/lib/patterns.d.ts +13 -13
  77. package/lib/registry.d.ts +8 -8
  78. package/lib/registry.js +21 -18
  79. package/lib/scheduler.d.ts +16 -16
  80. package/lib/scheduler.js +79 -70
  81. package/lib/schema.d.ts +1 -1
  82. package/lib/schema.js +6 -4
  83. package/lib/scxml.d.ts +5 -5
  84. package/lib/serviceScope.d.ts +3 -3
  85. package/lib/serviceScope.js +16 -12
  86. package/lib/stateUtils.d.ts +14 -14
  87. package/lib/stateUtils.js +231 -199
  88. package/lib/types.d.ts +928 -907
  89. package/lib/types.js +29 -29
  90. package/lib/utils.d.ts +68 -67
  91. package/lib/utils.js +530 -529
  92. package/package.json +6 -6
@@ -1,199 +1,206 @@
1
- import { StateMachine, Event, EventObject, DefaultContext, StateSchema, StateValue, InterpreterOptions, SingleOrArray, DoneEvent, MachineOptions, SCXML, EventData, Observer, Spawnable, Typestate } from './types';
2
- import { State } from './State';
3
- import { ActorRef, ActorRefFrom, Subscription } from '.';
4
- export declare type StateListener<TContext, TEvent extends EventObject, TStateSchema extends StateSchema<TContext> = any, TTypestate extends Typestate<TContext> = {
5
- value: any;
6
- context: TContext;
7
- }> = (state: State<TContext, TEvent, TStateSchema, TTypestate>, event: TEvent) => void;
8
- export declare type ContextListener<TContext = DefaultContext> = (context: TContext, prevContext: TContext | undefined) => void;
9
- export declare type EventListener<TEvent extends EventObject = EventObject> = (event: TEvent) => void;
10
- export declare type Listener = () => void;
11
- export interface Clock {
12
- setTimeout(fn: (...args: any[]) => void, timeout: number): any;
13
- clearTimeout(id: any): void;
14
- }
15
- interface SpawnOptions {
16
- name?: string;
17
- autoForward?: boolean;
18
- sync?: boolean;
19
- }
20
- export declare enum InterpreterStatus {
21
- NotStarted = 0,
22
- Running = 1,
23
- Stopped = 2
24
- }
25
- export declare class Interpreter<TContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> = {
26
- value: any;
27
- context: TContext;
28
- }> implements ActorRef<TEvent, State<TContext, TEvent, TStateSchema, TTypestate>> {
29
- machine: StateMachine<TContext, TStateSchema, TEvent, TTypestate>;
30
- /**
31
- * The default interpreter options:
32
- *
33
- * - `clock` uses the global `setTimeout` and `clearTimeout` functions
34
- * - `logger` uses the global `console.log()` method
35
- */
36
- static defaultOptions: InterpreterOptions;
37
- /**
38
- * The current state of the interpreted machine.
39
- */
40
- private _state?;
41
- private _initialState?;
42
- /**
43
- * The clock that is responsible for setting and clearing timeouts, such as delayed events and transitions.
44
- */
45
- clock: Clock;
46
- options: Readonly<InterpreterOptions>;
47
- private scheduler;
48
- private delayedEventsMap;
49
- private listeners;
50
- private contextListeners;
51
- private stopListeners;
52
- private doneListeners;
53
- private eventListeners;
54
- private sendListeners;
55
- private logger;
56
- /**
57
- * Whether the service is started.
58
- */
59
- initialized: boolean;
60
- status: InterpreterStatus;
61
- parent?: Interpreter<any>;
62
- id: string;
63
- /**
64
- * The globally unique process ID for this invocation.
65
- */
66
- sessionId: string;
67
- children: Map<string | number, ActorRef<any>>;
68
- private forwardTo;
69
- private devTools?;
70
- /**
71
- * Creates a new Interpreter instance (i.e., service) for the given machine with the provided options, if any.
72
- *
73
- * @param machine The machine to be interpreted
74
- * @param options Interpreter options
75
- */
76
- constructor(machine: StateMachine<TContext, TStateSchema, TEvent, TTypestate>, options?: Partial<InterpreterOptions>);
77
- get initialState(): State<TContext, TEvent, TStateSchema, TTypestate>;
78
- get state(): State<TContext, TEvent, TStateSchema, TTypestate>;
79
- static interpret: typeof interpret;
80
- /**
81
- * Executes the actions of the given state, with that state's `context` and `event`.
82
- *
83
- * @param state The state whose actions will be executed
84
- * @param actionsConfig The action implementations to use
85
- */
86
- execute(state: State<TContext, TEvent, TStateSchema, TTypestate>, actionsConfig?: MachineOptions<TContext, TEvent>['actions']): void;
87
- private update;
88
- onTransition(listener: StateListener<TContext, TEvent, TStateSchema, TTypestate>): this;
89
- subscribe(observer: Observer<State<TContext, TEvent, any, TTypestate>>): Subscription;
90
- subscribe(nextListener?: (state: State<TContext, TEvent, any, TTypestate>) => void, errorListener?: (error: any) => void, completeListener?: () => void): Subscription;
91
- /**
92
- * Adds an event listener that is notified whenever an event is sent to the running interpreter.
93
- * @param listener The event listener
94
- */
95
- onEvent(listener: EventListener): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
96
- /**
97
- * Adds an event listener that is notified whenever a `send` event occurs.
98
- * @param listener The event listener
99
- */
100
- onSend(listener: EventListener): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
101
- /**
102
- * Adds a context listener that is notified whenever the state context changes.
103
- * @param listener The context listener
104
- */
105
- onChange(listener: ContextListener<TContext>): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
106
- /**
107
- * Adds a listener that is notified when the machine is stopped.
108
- * @param listener The listener
109
- */
110
- onStop(listener: Listener): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
111
- /**
112
- * Adds a state listener that is notified when the statechart has reached its final state.
113
- * @param listener The state listener
114
- */
115
- onDone(listener: EventListener<DoneEvent>): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
116
- /**
117
- * Removes a listener.
118
- * @param listener The listener to remove
119
- */
120
- off(listener: (...args: any[]) => void): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
121
- /**
122
- * Alias for Interpreter.prototype.start
123
- */
124
- init: (initialState?: StateValue | State<TContext, TEvent, TStateSchema, TTypestate> | undefined) => Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
125
- /**
126
- * Starts the interpreter from the given state, or the initial state.
127
- * @param initialState The state to start the statechart from
128
- */
129
- start(initialState?: State<TContext, TEvent, TStateSchema, TTypestate> | StateValue): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
130
- /**
131
- * Stops the interpreter and unsubscribe all listeners.
132
- *
133
- * This will also notify the `onStop` listeners.
134
- */
135
- stop(): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
136
- /**
137
- * Sends an event to the running interpreter to trigger a transition.
138
- *
139
- * An array of events (batched) can be sent as well, which will send all
140
- * batched events to the running interpreter. The listeners will be
141
- * notified only **once** when all events are processed.
142
- *
143
- * @param event The event(s) to send
144
- */
145
- send: (event: SingleOrArray<Event<TEvent>> | SCXML.Event<TEvent>, payload?: EventData | undefined) => State<TContext, TEvent, TStateSchema, TTypestate>;
146
- private batch;
147
- /**
148
- * Returns a send function bound to this interpreter instance.
149
- *
150
- * @param event The event to be sent by the sender.
151
- */
152
- sender(event: Event<TEvent>): () => State<TContext, TEvent, TStateSchema, TTypestate>;
153
- private sendTo;
154
- /**
155
- * Returns the next state given the interpreter's current state and the event.
156
- *
157
- * This is a pure method that does _not_ update the interpreter's state.
158
- *
159
- * @param event The event to determine the next state
160
- */
161
- nextState(event: Event<TEvent> | SCXML.Event<TEvent>): State<TContext, TEvent, TStateSchema, TTypestate>;
162
- private forward;
163
- private defer;
164
- private cancel;
165
- private exec;
166
- private removeChild;
167
- private stopChild;
168
- spawn(entity: Spawnable, name: string, options?: SpawnOptions): ActorRef<any>;
169
- spawnMachine<TChildContext, TChildStateSchema, TChildEvent extends EventObject>(machine: StateMachine<TChildContext, TChildStateSchema, TChildEvent>, options?: {
170
- id?: string;
171
- autoForward?: boolean;
172
- sync?: boolean;
173
- }): ActorRef<TChildEvent, State<TChildContext, TChildEvent>>;
174
- private spawnPromise;
175
- private spawnCallback;
176
- private spawnObservable;
177
- private spawnActor;
178
- private spawnActivity;
179
- private spawnEffect;
180
- private attachDev;
181
- toJSON(): {
182
- id: string;
183
- };
184
- getSnapshot(): State<TContext, TEvent, TStateSchema, TTypestate>;
185
- }
186
- export declare function spawn<TC, TE extends EventObject>(entity: StateMachine<TC, any, TE>, nameOrOptions?: string | SpawnOptions): ActorRefFrom<StateMachine<TC, any, TE>>;
187
- export declare function spawn(entity: Spawnable, nameOrOptions?: string | SpawnOptions): ActorRef<any>;
188
- /**
189
- * Creates a new Interpreter instance for the given machine with the provided options, if any.
190
- *
191
- * @param machine The machine to interpret
192
- * @param options Interpreter options
193
- */
194
- export declare function interpret<TContext = DefaultContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> = {
195
- value: any;
196
- context: TContext;
197
- }>(machine: StateMachine<TContext, TStateSchema, TEvent, TTypestate>, options?: Partial<InterpreterOptions>): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
198
- export {};
1
+ import { StateMachine, Event, EventObject, DefaultContext, StateSchema, StateValue, InterpreterOptions, SingleOrArray, Subscribable, DoneEvent, MachineOptions, SCXML, EventData, Observer, Spawnable, Typestate, ActorRef, ActorRefFrom, Behavior, Subscription } from './types';
2
+ import { State } from './State';
3
+ export declare type StateListener<TContext, TEvent extends EventObject, TStateSchema extends StateSchema<TContext> = any, TTypestate extends Typestate<TContext> = {
4
+ value: any;
5
+ context: TContext;
6
+ }> = (state: State<TContext, TEvent, TStateSchema, TTypestate>, event: TEvent) => void;
7
+ export declare type ContextListener<TContext = DefaultContext> = (context: TContext, prevContext: TContext | undefined) => void;
8
+ export declare type EventListener<TEvent extends EventObject = EventObject> = (event: TEvent) => void;
9
+ export declare type Listener = () => void;
10
+ export interface Clock {
11
+ setTimeout(fn: (...args: any[]) => void, timeout: number): any;
12
+ clearTimeout(id: any): void;
13
+ }
14
+ interface SpawnOptions {
15
+ name?: string;
16
+ autoForward?: boolean;
17
+ sync?: boolean;
18
+ }
19
+ export declare enum InterpreterStatus {
20
+ NotStarted = 0,
21
+ Running = 1,
22
+ Stopped = 2
23
+ }
24
+ declare global {
25
+ interface SymbolConstructor {
26
+ readonly observable: symbol;
27
+ }
28
+ }
29
+ export declare class Interpreter<TContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> = {
30
+ value: any;
31
+ context: TContext;
32
+ }> implements ActorRef<TEvent, State<TContext, TEvent, TStateSchema, TTypestate>> {
33
+ machine: StateMachine<TContext, TStateSchema, TEvent, TTypestate>;
34
+ /**
35
+ * The default interpreter options:
36
+ *
37
+ * - `clock` uses the global `setTimeout` and `clearTimeout` functions
38
+ * - `logger` uses the global `console.log()` method
39
+ */
40
+ static defaultOptions: InterpreterOptions;
41
+ /**
42
+ * The current state of the interpreted machine.
43
+ */
44
+ private _state?;
45
+ private _initialState?;
46
+ /**
47
+ * The clock that is responsible for setting and clearing timeouts, such as delayed events and transitions.
48
+ */
49
+ clock: Clock;
50
+ options: Readonly<InterpreterOptions>;
51
+ private scheduler;
52
+ private delayedEventsMap;
53
+ private listeners;
54
+ private contextListeners;
55
+ private stopListeners;
56
+ private doneListeners;
57
+ private eventListeners;
58
+ private sendListeners;
59
+ private logger;
60
+ /**
61
+ * Whether the service is started.
62
+ */
63
+ initialized: boolean;
64
+ status: InterpreterStatus;
65
+ parent?: Interpreter<any>;
66
+ id: string;
67
+ /**
68
+ * The globally unique process ID for this invocation.
69
+ */
70
+ sessionId: string;
71
+ children: Map<string | number, ActorRef<any>>;
72
+ private forwardTo;
73
+ private devTools?;
74
+ /**
75
+ * Creates a new Interpreter instance (i.e., service) for the given machine with the provided options, if any.
76
+ *
77
+ * @param machine The machine to be interpreted
78
+ * @param options Interpreter options
79
+ */
80
+ constructor(machine: StateMachine<TContext, TStateSchema, TEvent, TTypestate>, options?: Partial<InterpreterOptions>);
81
+ get initialState(): State<TContext, TEvent, TStateSchema, TTypestate>;
82
+ get state(): State<TContext, TEvent, TStateSchema, TTypestate>;
83
+ static interpret: typeof interpret;
84
+ /**
85
+ * Executes the actions of the given state, with that state's `context` and `event`.
86
+ *
87
+ * @param state The state whose actions will be executed
88
+ * @param actionsConfig The action implementations to use
89
+ */
90
+ execute(state: State<TContext, TEvent, TStateSchema, TTypestate>, actionsConfig?: MachineOptions<TContext, TEvent>['actions']): void;
91
+ private update;
92
+ onTransition(listener: StateListener<TContext, TEvent, TStateSchema, TTypestate>): this;
93
+ subscribe(nextListener?: (state: State<TContext, TEvent, any, TTypestate>) => void, errorListener?: (error: any) => void, completeListener?: () => void): Subscription;
94
+ subscribe(observer: Observer<State<TContext, TEvent, any, TTypestate>>): Subscription;
95
+ /**
96
+ * Adds an event listener that is notified whenever an event is sent to the running interpreter.
97
+ * @param listener The event listener
98
+ */
99
+ onEvent(listener: EventListener): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
100
+ /**
101
+ * Adds an event listener that is notified whenever a `send` event occurs.
102
+ * @param listener The event listener
103
+ */
104
+ onSend(listener: EventListener): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
105
+ /**
106
+ * Adds a context listener that is notified whenever the state context changes.
107
+ * @param listener The context listener
108
+ */
109
+ onChange(listener: ContextListener<TContext>): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
110
+ /**
111
+ * Adds a listener that is notified when the machine is stopped.
112
+ * @param listener The listener
113
+ */
114
+ onStop(listener: Listener): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
115
+ /**
116
+ * Adds a state listener that is notified when the statechart has reached its final state.
117
+ * @param listener The state listener
118
+ */
119
+ onDone(listener: EventListener<DoneEvent>): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
120
+ /**
121
+ * Removes a listener.
122
+ * @param listener The listener to remove
123
+ */
124
+ off(listener: (...args: any[]) => void): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
125
+ /**
126
+ * Alias for Interpreter.prototype.start
127
+ */
128
+ init: (initialState?: StateValue | State<TContext, TEvent, TStateSchema, TTypestate> | undefined) => Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
129
+ /**
130
+ * Starts the interpreter from the given state, or the initial state.
131
+ * @param initialState The state to start the statechart from
132
+ */
133
+ start(initialState?: State<TContext, TEvent, TStateSchema, TTypestate> | StateValue): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
134
+ /**
135
+ * Stops the interpreter and unsubscribe all listeners.
136
+ *
137
+ * This will also notify the `onStop` listeners.
138
+ */
139
+ stop(): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
140
+ /**
141
+ * Sends an event to the running interpreter to trigger a transition.
142
+ *
143
+ * An array of events (batched) can be sent as well, which will send all
144
+ * batched events to the running interpreter. The listeners will be
145
+ * notified only **once** when all events are processed.
146
+ *
147
+ * @param event The event(s) to send
148
+ */
149
+ send: (event: SingleOrArray<Event<TEvent>> | SCXML.Event<TEvent>, payload?: EventData | undefined) => State<TContext, TEvent, TStateSchema, TTypestate>;
150
+ private batch;
151
+ /**
152
+ * Returns a send function bound to this interpreter instance.
153
+ *
154
+ * @param event The event to be sent by the sender.
155
+ */
156
+ sender(event: Event<TEvent>): () => State<TContext, TEvent, TStateSchema, TTypestate>;
157
+ private sendTo;
158
+ /**
159
+ * Returns the next state given the interpreter's current state and the event.
160
+ *
161
+ * This is a pure method that does _not_ update the interpreter's state.
162
+ *
163
+ * @param event The event to determine the next state
164
+ */
165
+ nextState(event: Event<TEvent> | SCXML.Event<TEvent>): State<TContext, TEvent, TStateSchema, TTypestate>;
166
+ private forward;
167
+ private defer;
168
+ private cancel;
169
+ private exec;
170
+ private removeChild;
171
+ private stopChild;
172
+ spawn(entity: Spawnable, name: string, options?: SpawnOptions): ActorRef<any>;
173
+ spawnMachine<TChildContext, TChildStateSchema, TChildEvent extends EventObject>(machine: StateMachine<TChildContext, TChildStateSchema, TChildEvent>, options?: {
174
+ id?: string;
175
+ autoForward?: boolean;
176
+ sync?: boolean;
177
+ }): ActorRef<TChildEvent, State<TChildContext, TChildEvent>>;
178
+ private spawnBehavior;
179
+ private spawnPromise;
180
+ private spawnCallback;
181
+ private spawnObservable;
182
+ private spawnActor;
183
+ private spawnActivity;
184
+ private spawnEffect;
185
+ private attachDev;
186
+ toJSON(): {
187
+ id: string;
188
+ };
189
+ [Symbol.observable](): Subscribable<State<TContext, TEvent, TStateSchema, TTypestate>>;
190
+ getSnapshot(): State<TContext, TEvent, TStateSchema, TTypestate>;
191
+ }
192
+ export declare function spawn<T extends Behavior<any, any>>(entity: T, nameOrOptions?: string | SpawnOptions): ActorRefFrom<T>;
193
+ export declare function spawn<TC, TE extends EventObject>(entity: StateMachine<TC, any, TE>, nameOrOptions?: string | SpawnOptions): ActorRefFrom<StateMachine<TC, any, TE>>;
194
+ export declare function spawn(entity: Spawnable, nameOrOptions?: string | SpawnOptions): ActorRef<any>;
195
+ /**
196
+ * Creates a new Interpreter instance for the given machine with the provided options, if any.
197
+ *
198
+ * @param machine The machine to interpret
199
+ * @param options Interpreter options
200
+ */
201
+ export declare function interpret<TContext = DefaultContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> = {
202
+ value: any;
203
+ context: TContext;
204
+ }>(machine: StateMachine<TContext, TStateSchema, TEvent, TTypestate>, options?: Partial<InterpreterOptions>): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
205
+ export {};
199
206
  //# sourceMappingURL=interpreter.d.ts.map