xstate 4.22.0 → 4.23.0

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