xstate 4.10.0 → 4.14.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.
- package/CHANGELOG.md +205 -0
- package/LICENSE +22 -0
- package/README.md +2 -2
- package/dist/xstate.interpreter.js +1 -1
- package/dist/xstate.js +1 -1
- package/dist/xstate.web.js +2 -2
- package/es/Actor.d.ts +5 -3
- package/es/Actor.js +26 -4
- package/es/State.d.ts +5 -2
- package/es/StateNode.d.ts +8 -8
- package/es/StateNode.js +61 -41
- package/es/actions.d.ts +9 -5
- package/es/actions.js +28 -13
- package/es/index.d.ts +2 -2
- package/es/index.js +1 -1
- package/es/interpreter.d.ts +29 -15
- package/es/interpreter.js +99 -112
- package/es/invokeUtils.d.ts +7 -0
- package/es/invokeUtils.js +39 -0
- package/es/match.d.ts +4 -1
- package/es/serviceScope.d.ts +10 -0
- package/es/serviceScope.js +18 -0
- package/es/stateUtils.d.ts +1 -1
- package/es/types.d.ts +79 -32
- package/es/utils.d.ts +4 -3
- package/es/utils.js +12 -3
- package/lib/Actor.d.ts +5 -3
- package/lib/Actor.js +24 -3
- package/lib/Machine.js +1 -0
- package/lib/SimulatedClock.js +1 -0
- package/lib/State.d.ts +5 -2
- package/lib/State.js +1 -0
- package/lib/StateNode.d.ts +8 -8
- package/lib/StateNode.js +69 -42
- package/lib/actionTypes.js +1 -0
- package/lib/actions.d.ts +9 -5
- package/lib/actions.js +27 -19
- package/lib/constants.js +1 -0
- package/lib/devTools.js +1 -0
- package/lib/each.js +1 -0
- package/lib/environment.js +1 -0
- package/lib/index.d.ts +2 -2
- package/lib/index.js +29 -20
- package/lib/interpreter.d.ts +28 -14
- package/lib/interpreter.js +82 -88
- package/lib/invokeUtils.d.ts +7 -0
- package/lib/invokeUtils.js +42 -0
- package/lib/json.js +1 -0
- package/lib/mapState.js +1 -0
- package/lib/match.d.ts +4 -1
- package/lib/match.js +1 -0
- package/lib/patterns.js +1 -0
- package/lib/registry.js +1 -0
- package/lib/scheduler.js +1 -0
- package/lib/scxml.js +1 -0
- package/lib/serviceScope.d.ts +10 -0
- package/lib/serviceScope.js +15 -0
- package/lib/stateUtils.d.ts +1 -1
- package/lib/stateUtils.js +1 -0
- package/lib/types.d.ts +79 -32
- package/lib/types.js +1 -0
- package/lib/utils.d.ts +4 -3
- package/lib/utils.js +9 -2
- package/package.json +6 -6
package/es/actions.d.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import { Action, Event, EventObject, SingleOrArray, SendAction, SendActionOptions, CancelAction, ActionObject, ActionType, Assigner, PropertyAssigner, AssignAction, ActionFunction, ActionFunctionMap, ActivityActionObject, ActionTypes, ActivityDefinition, RaiseAction, RaiseActionObject, DoneEvent, ErrorPlatformEvent, DoneEventObject, SendExpr, SendActionObject, PureAction, LogExpr, LogAction, LogActionObject, DelayFunctionMap, SCXML, ExprWithMeta, ChooseConditon, ChooseAction, AnyEventObject } from './types';
|
|
1
|
+
import { Action, Event, EventObject, SingleOrArray, SendAction, SendActionOptions, CancelAction, ActionObject, ActionType, Assigner, PropertyAssigner, AssignAction, ActionFunction, ActionFunctionMap, ActivityActionObject, ActionTypes, ActivityDefinition, RaiseAction, RaiseActionObject, DoneEvent, ErrorPlatformEvent, DoneEventObject, SendExpr, SendActionObject, PureAction, LogExpr, LogAction, LogActionObject, DelayFunctionMap, SCXML, ExprWithMeta, ChooseConditon, ChooseAction, AnyEventObject, Expr } from './types';
|
|
2
2
|
import * as actionTypes from './actionTypes';
|
|
3
3
|
import { State } from './State';
|
|
4
4
|
import { StateNode } from './StateNode';
|
|
5
|
+
import { StopAction, StopActionObject } from '.';
|
|
5
6
|
export { actionTypes };
|
|
6
7
|
export declare const initEvent: SCXML.Event<{
|
|
7
8
|
type: ActionTypes;
|
|
8
9
|
}>;
|
|
9
10
|
export declare function getActionFunction<TContext, TEvent extends EventObject>(actionType: ActionType, actionFunctionMap?: ActionFunctionMap<TContext, TEvent>): ActionObject<TContext, TEvent> | ActionFunction<TContext, TEvent> | undefined;
|
|
10
11
|
export declare function toActionObject<TContext, TEvent extends EventObject>(action: Action<TContext, TEvent>, actionFunctionMap?: ActionFunctionMap<TContext, TEvent>): ActionObject<TContext, TEvent>;
|
|
11
|
-
export declare const toActionObjects: <TContext, TEvent extends EventObject>(action?: string |
|
|
12
|
+
export declare const toActionObjects: <TContext, TEvent extends EventObject>(action?: string | ActionObject<TContext, TEvent> | ActionFunction<TContext, TEvent> | Action<TContext, TEvent>[] | undefined, actionFunctionMap?: Record<string, ActionObject<TContext, TEvent> | ActionFunction<TContext, TEvent>> | undefined) => ActionObject<TContext, TEvent>[];
|
|
12
13
|
export declare function toActivityDefinition<TContext, TEvent extends EventObject>(action: string | ActivityDefinition<TContext, TEvent>): ActivityDefinition<TContext, TEvent>;
|
|
13
14
|
/**
|
|
14
15
|
* Raises an event. This places the event in the internal event queue, so that
|
|
@@ -16,7 +17,7 @@ export declare function toActivityDefinition<TContext, TEvent extends EventObjec
|
|
|
16
17
|
*
|
|
17
18
|
* @param eventType The event to raise.
|
|
18
19
|
*/
|
|
19
|
-
export declare function raise<TContext, TEvent extends EventObject>(event: Event<TEvent>): RaiseAction<TEvent> | SendAction<TContext,
|
|
20
|
+
export declare function raise<TContext, TEvent extends EventObject>(event: Event<TEvent>): RaiseAction<TEvent> | SendAction<TContext, AnyEventObject, TEvent>;
|
|
20
21
|
export declare function resolveRaise<TEvent extends EventObject>(action: RaiseAction<TEvent>): RaiseActionObject<TEvent>;
|
|
21
22
|
/**
|
|
22
23
|
* Sends an event. This returns an action that will be read by an interpreter to
|
|
@@ -77,9 +78,12 @@ export declare function start<TContext, TEvent extends EventObject>(activity: st
|
|
|
77
78
|
/**
|
|
78
79
|
* Stops an activity.
|
|
79
80
|
*
|
|
80
|
-
* @param
|
|
81
|
+
* @param actorRef The activity to stop.
|
|
81
82
|
*/
|
|
82
|
-
export declare function stop<TContext, TEvent extends EventObject>(
|
|
83
|
+
export declare function stop<TContext, TEvent extends EventObject>(actorRef: string | ActivityDefinition<TContext, TEvent> | Expr<TContext, TEvent, string | {
|
|
84
|
+
id: string;
|
|
85
|
+
}>): StopAction<TContext, TEvent>;
|
|
86
|
+
export declare function resolveStop<TContext, TEvent extends EventObject>(action: StopAction<TContext, TEvent>, context: TContext, _event: SCXML.Event<TEvent>): StopActionObject;
|
|
83
87
|
/**
|
|
84
88
|
* Updates the current context of the machine.
|
|
85
89
|
*
|
package/es/actions.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { __assign, __read
|
|
1
|
+
import { __assign, __read } from './_virtual/_tslib.js';
|
|
2
2
|
import { IS_PRODUCTION } from './environment.js';
|
|
3
3
|
import { toSCXMLEvent, isFunction, toEventObject, getEventType, isString, partition, updateContext, flatten, toArray, toGuard, evaluateGuard, warn, isArray } from './utils.js';
|
|
4
4
|
import { SpecialTargets, ActionTypes } from './types.js';
|
|
5
|
-
import { send as send$1, update, assign as assign$1, init, raise as raise$1, log as log$1, cancel as cancel$1, error as error$1, pure as pure$1, choose as choose$1 } from './actionTypes.js';
|
|
5
|
+
import { send as send$1, update, assign as assign$1, init, raise as raise$1, log as log$1, cancel as cancel$1, error as error$1, stop as stop$1, pure as pure$1, choose as choose$1 } from './actionTypes.js';
|
|
6
6
|
var initEvent = /*#__PURE__*/toSCXMLEvent({
|
|
7
7
|
type: init
|
|
8
8
|
});
|
|
@@ -44,12 +44,10 @@ function toActionObject(action, actionFunctionMap) {
|
|
|
44
44
|
exec: exec
|
|
45
45
|
});
|
|
46
46
|
} else if (exec) {
|
|
47
|
-
var
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
type: type
|
|
52
|
-
}, exec), other);
|
|
47
|
+
var actionType = exec.type || action.type;
|
|
48
|
+
actionObject = __assign(__assign(__assign({}, exec), action), {
|
|
49
|
+
type: actionType
|
|
50
|
+
});
|
|
53
51
|
} else {
|
|
54
52
|
actionObject = action;
|
|
55
53
|
}
|
|
@@ -262,18 +260,30 @@ function start(activity) {
|
|
|
262
260
|
/**
|
|
263
261
|
* Stops an activity.
|
|
264
262
|
*
|
|
265
|
-
* @param
|
|
263
|
+
* @param actorRef The activity to stop.
|
|
266
264
|
*/
|
|
267
265
|
|
|
268
266
|
|
|
269
|
-
function stop(
|
|
270
|
-
var
|
|
267
|
+
function stop(actorRef) {
|
|
268
|
+
var activity = isFunction(actorRef) ? actorRef : toActivityDefinition(actorRef);
|
|
271
269
|
return {
|
|
272
270
|
type: ActionTypes.Stop,
|
|
273
|
-
activity:
|
|
271
|
+
activity: activity,
|
|
274
272
|
exec: undefined
|
|
275
273
|
};
|
|
276
274
|
}
|
|
275
|
+
|
|
276
|
+
function resolveStop(action, context, _event) {
|
|
277
|
+
var actorRefOrString = isFunction(action.activity) ? action.activity(context, _event.data) : action.activity;
|
|
278
|
+
var resolvedActorRef = typeof actorRefOrString === 'string' ? {
|
|
279
|
+
id: actorRefOrString
|
|
280
|
+
} : actorRefOrString;
|
|
281
|
+
var actionObject = {
|
|
282
|
+
type: ActionTypes.Stop,
|
|
283
|
+
activity: resolvedActorRef
|
|
284
|
+
};
|
|
285
|
+
return actionObject;
|
|
286
|
+
}
|
|
277
287
|
/**
|
|
278
288
|
* Updates the current context of the machine.
|
|
279
289
|
*
|
|
@@ -468,6 +478,11 @@ function resolveActions(machine, currentState, currentContext, _event, actions)
|
|
|
468
478
|
return resolved[0];
|
|
469
479
|
}
|
|
470
480
|
|
|
481
|
+
case stop$1:
|
|
482
|
+
{
|
|
483
|
+
return resolveStop(actionObject, updatedContext, _event);
|
|
484
|
+
}
|
|
485
|
+
|
|
471
486
|
default:
|
|
472
487
|
return toActionObject(actionObject, machine.options.actions);
|
|
473
488
|
}
|
|
@@ -475,4 +490,4 @@ function resolveActions(machine, currentState, currentContext, _event, actions)
|
|
|
475
490
|
return [resolvedActions, updatedContext];
|
|
476
491
|
}
|
|
477
492
|
|
|
478
|
-
export { after, assign, cancel, choose, done, doneInvoke, error, escalate, forwardTo, getActionFunction, initEvent, log, pure, raise, resolveActions, resolveLog, resolveRaise, resolveSend, respond, send, sendParent, sendUpdate, start, stop, toActionObject, toActionObjects, toActivityDefinition };
|
|
493
|
+
export { after, assign, cancel, choose, done, doneInvoke, error, escalate, forwardTo, getActionFunction, initEvent, log, pure, raise, resolveActions, resolveLog, resolveRaise, resolveSend, resolveStop, respond, send, sendParent, sendUpdate, start, stop, toActionObject, toActionObjects, toActivityDefinition };
|
package/es/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { State } from './State';
|
|
|
5
5
|
import { Machine, createMachine } from './Machine';
|
|
6
6
|
import { Actor } from './Actor';
|
|
7
7
|
import { raise, send, sendParent, sendUpdate, log, start, stop, assign, after, done, respond, doneInvoke, forwardTo, escalate, choose, pure } from './actions';
|
|
8
|
-
import { interpret, Interpreter, spawn } from './interpreter';
|
|
8
|
+
import { interpret, Interpreter, spawn, InterpreterStatus } from './interpreter';
|
|
9
9
|
import { matchState } from './match';
|
|
10
10
|
declare const actions: {
|
|
11
11
|
raise: typeof raise;
|
|
@@ -25,6 +25,6 @@ declare const actions: {
|
|
|
25
25
|
choose: typeof choose;
|
|
26
26
|
pure: typeof pure;
|
|
27
27
|
};
|
|
28
|
-
export { Actor, Machine, StateNode, State, matchesState, mapState, actions, assign, send, sendParent, sendUpdate, forwardTo, interpret, Interpreter, matchState, spawn, doneInvoke, createMachine };
|
|
28
|
+
export { Actor, Machine, StateNode, State, matchesState, mapState, actions, assign, send, sendParent, sendUpdate, forwardTo, interpret, Interpreter, InterpreterStatus, matchState, spawn, doneInvoke, createMachine };
|
|
29
29
|
export * from './types';
|
|
30
30
|
//# sourceMappingURL=index.d.ts.map
|
package/es/index.js
CHANGED
|
@@ -6,7 +6,7 @@ export { assign, doneInvoke, forwardTo, send, sendParent, sendUpdate } from './a
|
|
|
6
6
|
export { State } from './State.js';
|
|
7
7
|
export { StateNode } from './StateNode.js';
|
|
8
8
|
export { Machine, createMachine } from './Machine.js';
|
|
9
|
-
export { Interpreter, interpret, spawn } from './interpreter.js';
|
|
9
|
+
export { Interpreter, InterpreterStatus, interpret, spawn } from './interpreter.js';
|
|
10
10
|
export { matchState } from './match.js';
|
|
11
11
|
var actions = {
|
|
12
12
|
raise: raise,
|
package/es/interpreter.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import { StateMachine, Event, EventObject, DefaultContext, StateSchema, StateValue, InterpreterOptions, DoneEvent, Unsubscribable, MachineOptions, SCXML, EventData, Observer, Spawnable, Typestate } from './types';
|
|
1
|
+
import { StateMachine, Event, EventObject, DefaultContext, StateSchema, StateValue, InterpreterOptions, SingleOrArray, DoneEvent, Unsubscribable, MachineOptions, SCXML, EventData, Observer, Spawnable, Typestate } from './types';
|
|
2
2
|
import { State } from './State';
|
|
3
3
|
import { Actor } from './Actor';
|
|
4
|
-
export declare type StateListener<TContext, TEvent extends EventObject, TStateSchema extends StateSchema<TContext> = any, TTypestate extends Typestate<TContext> =
|
|
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;
|
|
5
8
|
export declare type ContextListener<TContext = DefaultContext> = (context: TContext, prevContext: TContext | undefined) => void;
|
|
6
9
|
export declare type EventListener<TEvent extends EventObject = EventObject> = (event: TEvent) => void;
|
|
7
10
|
export declare type Listener = () => void;
|
|
@@ -14,7 +17,15 @@ interface SpawnOptions {
|
|
|
14
17
|
autoForward?: boolean;
|
|
15
18
|
sync?: boolean;
|
|
16
19
|
}
|
|
17
|
-
export declare
|
|
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 Actor<State<TContext, TEvent, TStateSchema, TTypestate>, TEvent> {
|
|
18
29
|
machine: StateMachine<TContext, TStateSchema, TEvent, TTypestate>;
|
|
19
30
|
/**
|
|
20
31
|
* The default interpreter options:
|
|
@@ -46,7 +57,7 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
|
|
|
46
57
|
* Whether the service is started.
|
|
47
58
|
*/
|
|
48
59
|
initialized: boolean;
|
|
49
|
-
|
|
60
|
+
status: InterpreterStatus;
|
|
50
61
|
parent?: Interpreter<any>;
|
|
51
62
|
id: string;
|
|
52
63
|
/**
|
|
@@ -81,36 +92,36 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
|
|
|
81
92
|
* Adds an event listener that is notified whenever an event is sent to the running interpreter.
|
|
82
93
|
* @param listener The event listener
|
|
83
94
|
*/
|
|
84
|
-
onEvent(listener: EventListener): Interpreter<TContext, TStateSchema, TEvent>;
|
|
95
|
+
onEvent(listener: EventListener): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
|
|
85
96
|
/**
|
|
86
97
|
* Adds an event listener that is notified whenever a `send` event occurs.
|
|
87
98
|
* @param listener The event listener
|
|
88
99
|
*/
|
|
89
|
-
onSend(listener: EventListener): Interpreter<TContext, TStateSchema, TEvent>;
|
|
100
|
+
onSend(listener: EventListener): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
|
|
90
101
|
/**
|
|
91
102
|
* Adds a context listener that is notified whenever the state context changes.
|
|
92
103
|
* @param listener The context listener
|
|
93
104
|
*/
|
|
94
|
-
onChange(listener: ContextListener<TContext>): Interpreter<TContext, TStateSchema, TEvent>;
|
|
105
|
+
onChange(listener: ContextListener<TContext>): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
|
|
95
106
|
/**
|
|
96
107
|
* Adds a listener that is notified when the machine is stopped.
|
|
97
108
|
* @param listener The listener
|
|
98
109
|
*/
|
|
99
|
-
onStop(listener: Listener): Interpreter<TContext, TStateSchema, TEvent>;
|
|
110
|
+
onStop(listener: Listener): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
|
|
100
111
|
/**
|
|
101
112
|
* Adds a state listener that is notified when the statechart has reached its final state.
|
|
102
113
|
* @param listener The state listener
|
|
103
114
|
*/
|
|
104
|
-
onDone(listener: EventListener<DoneEvent>): Interpreter<TContext, TStateSchema, TEvent>;
|
|
115
|
+
onDone(listener: EventListener<DoneEvent>): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
|
|
105
116
|
/**
|
|
106
117
|
* Removes a listener.
|
|
107
118
|
* @param listener The listener to remove
|
|
108
119
|
*/
|
|
109
|
-
off(listener: (...args: any[]) => void): Interpreter<TContext, TStateSchema, TEvent>;
|
|
120
|
+
off(listener: (...args: any[]) => void): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
|
|
110
121
|
/**
|
|
111
122
|
* Alias for Interpreter.prototype.start
|
|
112
123
|
*/
|
|
113
|
-
init: (initialState?: string | State<TContext, TEvent, TStateSchema, TTypestate> |
|
|
124
|
+
init: (initialState?: string | import("./types").StateValueMap | State<TContext, TEvent, TStateSchema, TTypestate> | undefined) => Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
|
|
114
125
|
/**
|
|
115
126
|
* Starts the interpreter from the given state, or the initial state.
|
|
116
127
|
* @param initialState The state to start the statechart from
|
|
@@ -121,7 +132,7 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
|
|
|
121
132
|
*
|
|
122
133
|
* This will also notify the `onStop` listeners.
|
|
123
134
|
*/
|
|
124
|
-
stop(): Interpreter<TContext, TStateSchema, TEvent>;
|
|
135
|
+
stop(): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
|
|
125
136
|
/**
|
|
126
137
|
* Sends an event to the running interpreter to trigger a transition.
|
|
127
138
|
*
|
|
@@ -131,7 +142,7 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
|
|
|
131
142
|
*
|
|
132
143
|
* @param event The event(s) to send
|
|
133
144
|
*/
|
|
134
|
-
send: (event:
|
|
145
|
+
send: (event: SingleOrArray<Event<TEvent>> | SCXML.Event<TEvent>, payload?: EventData | undefined) => State<TContext, TEvent, TStateSchema, TTypestate>;
|
|
135
146
|
private batch;
|
|
136
147
|
/**
|
|
137
148
|
* Returns a send function bound to this interpreter instance.
|
|
@@ -171,7 +182,7 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
|
|
|
171
182
|
id: string;
|
|
172
183
|
};
|
|
173
184
|
}
|
|
174
|
-
export declare function spawn<TC, TE extends EventObject>(entity: StateMachine<TC, any, TE>, nameOrOptions?: string | SpawnOptions):
|
|
185
|
+
export declare function spawn<TC, TE extends EventObject>(entity: StateMachine<TC, any, TE>, nameOrOptions?: string | SpawnOptions): Actor<State<TC, TE>, TE>;
|
|
175
186
|
export declare function spawn(entity: Spawnable, nameOrOptions?: string | SpawnOptions): Actor;
|
|
176
187
|
/**
|
|
177
188
|
* Creates a new Interpreter instance for the given machine with the provided options, if any.
|
|
@@ -179,6 +190,9 @@ export declare function spawn(entity: Spawnable, nameOrOptions?: string | SpawnO
|
|
|
179
190
|
* @param machine The machine to interpret
|
|
180
191
|
* @param options Interpreter options
|
|
181
192
|
*/
|
|
182
|
-
export declare function interpret<TContext = DefaultContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> =
|
|
193
|
+
export declare function interpret<TContext = DefaultContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> = {
|
|
194
|
+
value: any;
|
|
195
|
+
context: TContext;
|
|
196
|
+
}>(machine: StateMachine<TContext, TStateSchema, TEvent, TTypestate>, options?: Partial<InterpreterOptions>): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
|
|
183
197
|
export {};
|
|
184
198
|
//# sourceMappingURL=interpreter.d.ts.map
|