xstate 5.0.0-beta.9 → 5.0.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/README.md +10 -8
- package/actions/dist/xstate-actions.cjs.js +19 -5
- package/actions/dist/xstate-actions.cjs.mjs +3 -15
- package/actions/dist/xstate-actions.development.cjs.js +21 -0
- package/actions/dist/xstate-actions.development.cjs.mjs +13 -0
- package/actions/dist/xstate-actions.development.esm.js +3 -0
- package/actions/dist/xstate-actions.esm.js +3 -2
- 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 +621 -4
- package/actors/dist/xstate-actors.cjs.mjs +1 -8
- package/actors/dist/xstate-actors.development.cjs.js +624 -0
- package/actors/dist/xstate-actors.development.cjs.mjs +8 -0
- package/actors/dist/xstate-actors.development.esm.js +615 -0
- package/actors/dist/xstate-actors.esm.js +615 -2
- package/actors/dist/xstate-actors.umd.min.js +1 -1
- package/actors/dist/xstate-actors.umd.min.js.map +1 -1
- package/dev/dist/xstate-dev.cjs.js +45 -4
- package/{dist/index-0f3fdf0c.cjs.prod.js → dev/dist/xstate-dev.development.cjs.js} +6 -7
- package/dev/dist/xstate-dev.development.cjs.mjs +5 -0
- package/{dist/index-50bd0aff.esm.js → dev/dist/xstate-dev.development.esm.js} +6 -8
- package/dev/dist/xstate-dev.esm.js +42 -1
- package/dev/dist/xstate-dev.umd.min.js +1 -1
- package/dev/dist/xstate-dev.umd.min.js.map +1 -1
- package/dist/declarations/src/SimulatedClock.d.ts +1 -1
- package/dist/declarations/src/State.d.ts +47 -73
- package/dist/declarations/src/StateMachine.d.ts +29 -57
- package/dist/declarations/src/StateNode.d.ts +36 -33
- package/dist/declarations/src/actions/assign.d.ts +11 -2
- package/dist/declarations/src/actions/cancel.d.ts +7 -3
- package/dist/declarations/src/actions/enqueueActions.d.ts +32 -0
- package/dist/declarations/src/actions/log.d.ts +7 -3
- package/dist/declarations/src/actions/raise.d.ts +7 -2
- package/dist/declarations/src/actions/send.d.ts +14 -36
- package/dist/declarations/src/actions/spawnChild.d.ts +29 -0
- package/dist/declarations/src/actions/stopChild.d.ts +18 -0
- package/dist/declarations/src/actions.d.ts +8 -48
- package/dist/declarations/src/actors/callback.d.ts +91 -8
- package/dist/declarations/src/actors/index.d.ts +6 -28
- package/dist/declarations/src/actors/observable.d.ts +101 -18
- package/dist/declarations/src/actors/promise.d.ts +80 -10
- package/dist/declarations/src/actors/transition.d.ts +64 -9
- package/dist/declarations/src/constants.d.ts +3 -0
- package/dist/declarations/src/createMachine.d.ts +20 -0
- package/dist/declarations/src/dev/index.d.ts +6 -6
- package/dist/declarations/src/guards.d.ts +41 -8
- package/dist/declarations/src/index.d.ts +18 -23
- package/dist/declarations/src/interpreter.d.ts +149 -41
- package/dist/declarations/src/setup.d.ts +51 -0
- package/dist/declarations/src/spawn.d.ts +23 -2
- package/dist/declarations/src/stateUtils.d.ts +30 -45
- package/dist/declarations/src/system.d.ts +26 -2
- package/dist/declarations/src/typegenTypes.d.ts +34 -22
- package/dist/declarations/src/types.d.ts +527 -669
- package/dist/declarations/src/utils.d.ts +15 -52
- package/dist/declarations/src/waitFor.d.ts +2 -2
- package/dist/log-22e678c5.esm.js +364 -0
- package/dist/log-5e226275.cjs.js +372 -0
- package/dist/log-641cd926.development.cjs.js +394 -0
- package/dist/log-f196f85f.development.esm.js +386 -0
- package/dist/raise-34e25c2c.cjs.js +2368 -0
- package/dist/raise-62704519.development.cjs.js +2422 -0
- package/dist/raise-89c581c4.development.esm.js +2371 -0
- package/dist/raise-8bc422d1.esm.js +2317 -0
- package/dist/xstate.cjs.js +728 -4
- package/dist/xstate.cjs.mjs +9 -9
- package/dist/xstate.development.cjs.js +737 -0
- package/dist/xstate.development.cjs.mjs +39 -0
- package/dist/xstate.development.esm.js +699 -0
- package/dist/xstate.esm.js +561 -770
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/guards/dist/xstate-guards.cjs.js +12 -5
- package/guards/dist/xstate-guards.cjs.mjs +1 -2
- package/guards/dist/xstate-guards.development.cjs.js +14 -0
- package/guards/dist/xstate-guards.development.cjs.mjs +7 -0
- package/guards/dist/xstate-guards.development.esm.js +2 -0
- package/guards/dist/xstate-guards.esm.js +2 -2
- package/guards/dist/xstate-guards.umd.min.js +1 -1
- package/guards/dist/xstate-guards.umd.min.js.map +1 -1
- package/package.json +53 -1
- package/actions/dist/xstate-actions.cjs.dev.js +0 -32
- package/actions/dist/xstate-actions.cjs.prod.js +0 -32
- package/actions/dynamicAction.ts +0 -42
- package/actors/dist/xstate-actors.cjs.dev.js +0 -22
- package/actors/dist/xstate-actors.cjs.prod.js +0 -22
- package/dev/dist/xstate-dev.cjs.dev.js +0 -11
- package/dev/dist/xstate-dev.cjs.prod.js +0 -11
- package/dist/actions-b6357569.cjs.dev.js +0 -4437
- package/dist/actions-bd4a184d.cjs.prod.js +0 -4423
- package/dist/actions-de434a04.esm.js +0 -4348
- package/dist/declarations/actions/dynamicAction.d.ts +0 -5
- package/dist/declarations/src/Machine.d.ts +0 -4
- package/dist/declarations/src/Mailbox.d.ts +0 -12
- package/dist/declarations/src/actionTypes.d.ts +0 -16
- package/dist/declarations/src/actions/choose.d.ts +0 -3
- package/dist/declarations/src/actions/invoke.d.ts +0 -3
- package/dist/declarations/src/actions/pure.d.ts +0 -6
- package/dist/declarations/src/actions/stop.d.ts +0 -7
- package/dist/declarations/src/environment.d.ts +0 -1
- package/dist/declarations/src/mapState.d.ts +0 -3
- package/dist/declarations/src/memo.d.ts +0 -2
- package/dist/index-ebaab3c9.cjs.dev.js +0 -52
- package/dist/xstate.cjs.dev.js +0 -950
- package/dist/xstate.cjs.prod.js +0 -947
- package/guards/dist/xstate-guards.cjs.dev.js +0 -15
- package/guards/dist/xstate-guards.cjs.prod.js +0 -15
package/dist/xstate.esm.js
CHANGED
|
@@ -1,431 +1,395 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* The meta data associated with this state node, which will be returned in State instances.
|
|
59
|
-
*/
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* The output data sent with the "done.state._id_" event if this is a final state node.
|
|
63
|
-
*/
|
|
1
|
+
export { createEmptyActor, fromCallback, fromEventObservable, fromObservable, fromPromise, fromTransition } from '../actors/dist/xstate-actors.esm.js';
|
|
2
|
+
import { S as STATE_DELIMITER, m as mapValues, t as toArray, f as formatTransitions, a as toTransitionConfigArray, b as formatTransition, N as NULL_EVENT, e as evaluateGuard, c as createInvokeId, g as getDelayedTransitions, d as formatInitialTransition, h as getCandidates, r as resolveStateValue, i as getAllStateNodes, j as getStateNodes, k as createMachineSnapshot, l as isInFinalState, n as macrostep, o as transitionNode, p as resolveActionsAndContext, q as createInitEvent, s as microstep, u as getInitialStateNodes, v as isStateId, w as getStateNodeByPath, x as getPersistedSnapshot, y as resolveReferencedActor, z as createActor, $ as $$ACTOR_TYPE } from './raise-8bc422d1.esm.js';
|
|
3
|
+
export { A as Actor, G as __unsafe_getAllOwnEventDescriptors, H as and, L as cancel, z as createActor, j as getStateNodes, B as interpret, C as isMachineSnapshot, D as matchesState, I as not, J as or, E as pathToStateValue, M as raise, O as spawnChild, K as stateIn, P as stop, Q as stopChild, F as toObserver } from './raise-8bc422d1.esm.js';
|
|
4
|
+
import { a as assign } from './log-22e678c5.esm.js';
|
|
5
|
+
export { S as SpecialTargets, a as assign, e as enqueueActions, f as forwardTo, l as log, s as sendParent, b as sendTo } from './log-22e678c5.esm.js';
|
|
6
|
+
import '../dev/dist/xstate-dev.esm.js';
|
|
7
|
+
|
|
8
|
+
class SimulatedClock {
|
|
9
|
+
constructor() {
|
|
10
|
+
this.timeouts = new Map();
|
|
11
|
+
this._now = 0;
|
|
12
|
+
this._id = 0;
|
|
13
|
+
}
|
|
14
|
+
now() {
|
|
15
|
+
return this._now;
|
|
16
|
+
}
|
|
17
|
+
getId() {
|
|
18
|
+
return this._id++;
|
|
19
|
+
}
|
|
20
|
+
setTimeout(fn, timeout) {
|
|
21
|
+
const id = this.getId();
|
|
22
|
+
this.timeouts.set(id, {
|
|
23
|
+
start: this.now(),
|
|
24
|
+
timeout,
|
|
25
|
+
fn
|
|
26
|
+
});
|
|
27
|
+
return id;
|
|
28
|
+
}
|
|
29
|
+
clearTimeout(id) {
|
|
30
|
+
this.timeouts.delete(id);
|
|
31
|
+
}
|
|
32
|
+
set(time) {
|
|
33
|
+
if (this._now > time) {
|
|
34
|
+
throw new Error('Unable to travel back in time');
|
|
35
|
+
}
|
|
36
|
+
this._now = time;
|
|
37
|
+
this.flushTimeouts();
|
|
38
|
+
}
|
|
39
|
+
flushTimeouts() {
|
|
40
|
+
[...this.timeouts].sort(([_idA, timeoutA], [_idB, timeoutB]) => {
|
|
41
|
+
const endA = timeoutA.start + timeoutA.timeout;
|
|
42
|
+
const endB = timeoutB.start + timeoutB.timeout;
|
|
43
|
+
return endB > endA ? -1 : 1;
|
|
44
|
+
}).forEach(([id, timeout]) => {
|
|
45
|
+
if (this.now() - timeout.start >= timeout.timeout) {
|
|
46
|
+
this.timeouts.delete(id);
|
|
47
|
+
timeout.fn.call(null);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
increment(ms) {
|
|
52
|
+
this._now += ms;
|
|
53
|
+
this.flushTimeouts();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
64
56
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
57
|
+
const cache = new WeakMap();
|
|
58
|
+
function memo(object, key, fn) {
|
|
59
|
+
let memoizedData = cache.get(object);
|
|
60
|
+
if (!memoizedData) {
|
|
61
|
+
memoizedData = {
|
|
62
|
+
[key]: fn()
|
|
63
|
+
};
|
|
64
|
+
cache.set(object, memoizedData);
|
|
65
|
+
} else if (!(key in memoizedData)) {
|
|
66
|
+
memoizedData[key] = fn();
|
|
67
|
+
}
|
|
68
|
+
return memoizedData[key];
|
|
69
|
+
}
|
|
68
70
|
|
|
69
|
-
|
|
71
|
+
const EMPTY_OBJECT = {};
|
|
72
|
+
const toSerializableAction = action => {
|
|
73
|
+
if (typeof action === 'string') {
|
|
74
|
+
return {
|
|
75
|
+
type: action
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
if (typeof action === 'function') {
|
|
79
|
+
if ('resolve' in action) {
|
|
80
|
+
return {
|
|
81
|
+
type: action.type
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
type: action.name
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return action;
|
|
89
|
+
};
|
|
90
|
+
class StateNode {
|
|
91
|
+
constructor(
|
|
70
92
|
/**
|
|
71
93
|
* The raw config used to create the machine.
|
|
72
94
|
*/
|
|
73
95
|
config, options) {
|
|
74
|
-
var _this = this;
|
|
75
|
-
_classCallCheck(this, StateNode);
|
|
76
96
|
this.config = config;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
97
|
+
/**
|
|
98
|
+
* The relative key of the state node, which represents its location in the overall state value.
|
|
99
|
+
*/
|
|
100
|
+
this.key = void 0;
|
|
101
|
+
/**
|
|
102
|
+
* The unique ID of the state node.
|
|
103
|
+
*/
|
|
104
|
+
this.id = void 0;
|
|
105
|
+
/**
|
|
106
|
+
* The type of this state node:
|
|
107
|
+
*
|
|
108
|
+
* - `'atomic'` - no child state nodes
|
|
109
|
+
* - `'compound'` - nested child state nodes (XOR)
|
|
110
|
+
* - `'parallel'` - orthogonal nested child state nodes (AND)
|
|
111
|
+
* - `'history'` - history state node
|
|
112
|
+
* - `'final'` - final state node
|
|
113
|
+
*/
|
|
114
|
+
this.type = void 0;
|
|
115
|
+
/**
|
|
116
|
+
* The string path from the root machine node to this node.
|
|
117
|
+
*/
|
|
118
|
+
this.path = void 0;
|
|
119
|
+
/**
|
|
120
|
+
* The child state nodes.
|
|
121
|
+
*/
|
|
122
|
+
this.states = void 0;
|
|
123
|
+
/**
|
|
124
|
+
* The type of history on this state node. Can be:
|
|
125
|
+
*
|
|
126
|
+
* - `'shallow'` - recalls only top-level historical state value
|
|
127
|
+
* - `'deep'` - recalls historical state value at all levels
|
|
128
|
+
*/
|
|
129
|
+
this.history = void 0;
|
|
130
|
+
/**
|
|
131
|
+
* The action(s) to be executed upon entering the state node.
|
|
132
|
+
*/
|
|
133
|
+
this.entry = void 0;
|
|
134
|
+
/**
|
|
135
|
+
* The action(s) to be executed upon exiting the state node.
|
|
136
|
+
*/
|
|
137
|
+
this.exit = void 0;
|
|
138
|
+
/**
|
|
139
|
+
* The parent state node.
|
|
140
|
+
*/
|
|
141
|
+
this.parent = void 0;
|
|
142
|
+
/**
|
|
143
|
+
* The root machine node.
|
|
144
|
+
*/
|
|
145
|
+
this.machine = void 0;
|
|
146
|
+
/**
|
|
147
|
+
* The meta data associated with this state node, which will be returned in State instances.
|
|
148
|
+
*/
|
|
149
|
+
this.meta = void 0;
|
|
150
|
+
/**
|
|
151
|
+
* The output data sent with the "xstate.done.state._id_" event if this is a final state node.
|
|
152
|
+
*/
|
|
153
|
+
this.output = void 0;
|
|
154
|
+
/**
|
|
155
|
+
* The order this state node appears. Corresponds to the implicit document order.
|
|
156
|
+
*/
|
|
157
|
+
this.order = -1;
|
|
158
|
+
this.description = void 0;
|
|
159
|
+
this.tags = [];
|
|
160
|
+
this.transitions = void 0;
|
|
161
|
+
this.always = void 0;
|
|
94
162
|
this.parent = options._parent;
|
|
95
163
|
this.key = options._key;
|
|
96
164
|
this.machine = options._machine;
|
|
97
165
|
this.path = this.parent ? this.parent.path.concat(this.key) : [];
|
|
98
|
-
this.id = this.config.id || [this.machine.id
|
|
166
|
+
this.id = this.config.id || [this.machine.id, ...this.path].join(STATE_DELIMITER);
|
|
99
167
|
this.type = this.config.type || (this.config.states && Object.keys(this.config.states).length ? 'compound' : this.config.history ? 'history' : 'atomic');
|
|
100
168
|
this.description = this.config.description;
|
|
101
169
|
this.order = this.machine.idMap.size;
|
|
102
170
|
this.machine.idMap.set(this.id, this);
|
|
103
|
-
this.states = this.config.states ? mapValues(this.config.states,
|
|
104
|
-
|
|
105
|
-
_parent:
|
|
171
|
+
this.states = this.config.states ? mapValues(this.config.states, (stateConfig, key) => {
|
|
172
|
+
const stateNode = new StateNode(stateConfig, {
|
|
173
|
+
_parent: this,
|
|
106
174
|
_key: key,
|
|
107
|
-
_machine:
|
|
175
|
+
_machine: this.machine
|
|
108
176
|
});
|
|
109
177
|
return stateNode;
|
|
110
178
|
}) : EMPTY_OBJECT;
|
|
111
179
|
if (this.type === 'compound' && !this.config.initial) {
|
|
112
|
-
throw new Error(
|
|
180
|
+
throw new Error(`No initial state specified for compound state node "#${this.id}". Try adding { initial: "${Object.keys(this.states)[0]}" } to the state config.`);
|
|
113
181
|
}
|
|
114
182
|
|
|
115
183
|
// History config
|
|
116
184
|
this.history = this.config.history === true ? 'shallow' : this.config.history || false;
|
|
117
|
-
this.entry =
|
|
118
|
-
this.exit =
|
|
185
|
+
this.entry = toArray(this.config.entry).slice();
|
|
186
|
+
this.exit = toArray(this.config.exit).slice();
|
|
119
187
|
this.meta = this.config.meta;
|
|
120
|
-
this.output = this.type === 'final' ? this.config.output : undefined;
|
|
121
|
-
this.tags = toArray(config.tags);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
this.transitions = formatTransitions(this);
|
|
128
|
-
if (this.config.always) {
|
|
129
|
-
this.always = toTransitionConfigArray(NULL_EVENT, this.config.always).map(function (t) {
|
|
130
|
-
return formatTransition(_this2, t);
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
Object.keys(this.states).forEach(function (key) {
|
|
134
|
-
_this2.states[key]._initialize();
|
|
135
|
-
});
|
|
188
|
+
this.output = this.type === 'final' || !this.parent ? this.config.output : undefined;
|
|
189
|
+
this.tags = toArray(config.tags).slice();
|
|
190
|
+
}
|
|
191
|
+
_initialize() {
|
|
192
|
+
this.transitions = formatTransitions(this);
|
|
193
|
+
if (this.config.always) {
|
|
194
|
+
this.always = toTransitionConfigArray(this.config.always).map(t => formatTransition(this, NULL_EVENT, t));
|
|
136
195
|
}
|
|
196
|
+
Object.keys(this.states).forEach(key => {
|
|
197
|
+
this.states[key]._initialize();
|
|
198
|
+
});
|
|
199
|
+
}
|
|
137
200
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
}, {
|
|
184
|
-
key: "toJSON",
|
|
185
|
-
value: function toJSON() {
|
|
186
|
-
return this.definition;
|
|
187
|
-
}
|
|
201
|
+
/**
|
|
202
|
+
* The well-structured state node definition.
|
|
203
|
+
*/
|
|
204
|
+
get definition() {
|
|
205
|
+
return {
|
|
206
|
+
id: this.id,
|
|
207
|
+
key: this.key,
|
|
208
|
+
version: this.machine.version,
|
|
209
|
+
type: this.type,
|
|
210
|
+
initial: this.initial ? {
|
|
211
|
+
target: this.initial.target,
|
|
212
|
+
source: this,
|
|
213
|
+
actions: this.initial.actions.map(toSerializableAction),
|
|
214
|
+
eventType: null,
|
|
215
|
+
reenter: false,
|
|
216
|
+
toJSON: () => ({
|
|
217
|
+
target: this.initial.target.map(t => `#${t.id}`),
|
|
218
|
+
source: `#${this.id}`,
|
|
219
|
+
actions: this.initial.actions.map(toSerializableAction),
|
|
220
|
+
eventType: null
|
|
221
|
+
})
|
|
222
|
+
} : undefined,
|
|
223
|
+
history: this.history,
|
|
224
|
+
states: mapValues(this.states, state => {
|
|
225
|
+
return state.definition;
|
|
226
|
+
}),
|
|
227
|
+
on: this.on,
|
|
228
|
+
transitions: [...this.transitions.values()].flat().map(t => ({
|
|
229
|
+
...t,
|
|
230
|
+
actions: t.actions.map(toSerializableAction)
|
|
231
|
+
})),
|
|
232
|
+
entry: this.entry.map(toSerializableAction),
|
|
233
|
+
exit: this.exit.map(toSerializableAction),
|
|
234
|
+
meta: this.meta,
|
|
235
|
+
order: this.order || -1,
|
|
236
|
+
output: this.output,
|
|
237
|
+
invoke: this.invoke,
|
|
238
|
+
description: this.description,
|
|
239
|
+
tags: this.tags
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
toJSON() {
|
|
243
|
+
return this.definition;
|
|
244
|
+
}
|
|
188
245
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
246
|
+
/**
|
|
247
|
+
* The logic invoked as actors by this state node.
|
|
248
|
+
*/
|
|
249
|
+
get invoke() {
|
|
250
|
+
return memo(this, 'invoke', () => toArray(this.config.invoke).map((invokeConfig, i) => {
|
|
251
|
+
const {
|
|
252
|
+
src,
|
|
253
|
+
systemId
|
|
254
|
+
} = invokeConfig;
|
|
255
|
+
const resolvedId = invokeConfig.id ?? createInvokeId(this.id, i);
|
|
256
|
+
const resolvedSrc = typeof src === 'string' ? src : `xstate.invoke.${createInvokeId(this.id, i)}`;
|
|
257
|
+
return {
|
|
258
|
+
...invokeConfig,
|
|
259
|
+
src: resolvedSrc,
|
|
260
|
+
id: resolvedId,
|
|
261
|
+
systemId: systemId,
|
|
262
|
+
toJSON() {
|
|
263
|
+
const {
|
|
264
|
+
onDone,
|
|
265
|
+
onError,
|
|
266
|
+
...invokeDefValues
|
|
267
|
+
} = invokeConfig;
|
|
268
|
+
return {
|
|
269
|
+
...invokeDefValues,
|
|
270
|
+
type: 'xstate.invoke',
|
|
210
271
|
src: resolvedSrc,
|
|
211
|
-
id: resolvedId
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
return _objectSpread2(_objectSpread2({}, invokeDefValues), {}, {
|
|
218
|
-
type: invoke,
|
|
219
|
-
src: resolvedSrc,
|
|
220
|
-
id: resolvedId
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
});
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* The mapping of events to transitions.
|
|
230
|
-
*/
|
|
231
|
-
}, {
|
|
232
|
-
key: "on",
|
|
233
|
-
get: function get() {
|
|
234
|
-
var _this5 = this;
|
|
235
|
-
return memo(this, 'on', function () {
|
|
236
|
-
var transitions = _this5.transitions;
|
|
237
|
-
return transitions.reduce(function (map, transition) {
|
|
238
|
-
map[transition.eventType] = map[transition.eventType] || [];
|
|
239
|
-
map[transition.eventType].push(transition);
|
|
240
|
-
return map;
|
|
241
|
-
}, {});
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
}, {
|
|
245
|
-
key: "after",
|
|
246
|
-
get: function get() {
|
|
247
|
-
var _this6 = this;
|
|
248
|
-
return memo(this, 'delayedTransitions', function () {
|
|
249
|
-
return getDelayedTransitions(_this6);
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
}, {
|
|
253
|
-
key: "initial",
|
|
254
|
-
get: function get() {
|
|
255
|
-
var _this7 = this;
|
|
256
|
-
return memo(this, 'initial', function () {
|
|
257
|
-
return formatInitialTransition(_this7, _this7.config.initial || []);
|
|
258
|
-
});
|
|
259
|
-
}
|
|
272
|
+
id: resolvedId
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
}));
|
|
277
|
+
}
|
|
260
278
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
279
|
+
/**
|
|
280
|
+
* The mapping of events to transitions.
|
|
281
|
+
*/
|
|
282
|
+
get on() {
|
|
283
|
+
return memo(this, 'on', () => {
|
|
284
|
+
const transitions = this.transitions;
|
|
285
|
+
return [...transitions].flatMap(([descriptor, t]) => t.map(t => [descriptor, t])).reduce((map, [descriptor, transition]) => {
|
|
286
|
+
map[descriptor] = map[descriptor] || [];
|
|
287
|
+
map[descriptor].push(transition);
|
|
288
|
+
return map;
|
|
289
|
+
}, {});
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
get after() {
|
|
293
|
+
return memo(this, 'delayedTransitions', () => getDelayedTransitions(this));
|
|
294
|
+
}
|
|
295
|
+
get initial() {
|
|
296
|
+
return memo(this, 'initial', () => formatInitialTransition(this, this.config.initial));
|
|
297
|
+
}
|
|
298
|
+
next(snapshot, event) {
|
|
299
|
+
const eventType = event.type;
|
|
300
|
+
const actions = [];
|
|
301
|
+
let selectedTransition;
|
|
302
|
+
const candidates = memo(this, `candidates-${eventType}`, () => getCandidates(this, eventType));
|
|
303
|
+
for (const candidate of candidates) {
|
|
304
|
+
const {
|
|
305
|
+
guard
|
|
306
|
+
} = candidate;
|
|
307
|
+
const resolvedContext = snapshot.context;
|
|
308
|
+
let guardPassed = false;
|
|
284
309
|
try {
|
|
285
|
-
|
|
286
|
-
var candidate = _step.value;
|
|
287
|
-
var guard = candidate.guard;
|
|
288
|
-
var resolvedContext = state.context;
|
|
289
|
-
var guardPassed = false;
|
|
290
|
-
try {
|
|
291
|
-
guardPassed = !guard || evaluateGuard(guard, resolvedContext, _event, state);
|
|
292
|
-
} catch (err) {
|
|
293
|
-
throw new Error("Unable to evaluate guard '".concat(guard.type, "' in transition for event '").concat(eventName, "' in state node '").concat(this.id, "':\n").concat(err.message));
|
|
294
|
-
}
|
|
295
|
-
if (guardPassed) {
|
|
296
|
-
actions.push.apply(actions, _toConsumableArray(candidate.actions));
|
|
297
|
-
selectedTransition = candidate;
|
|
298
|
-
break;
|
|
299
|
-
}
|
|
300
|
-
}
|
|
310
|
+
guardPassed = !guard || evaluateGuard(guard, resolvedContext, event, snapshot);
|
|
301
311
|
} catch (err) {
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
_iterator.f();
|
|
312
|
+
const guardType = typeof guard === 'string' ? guard : typeof guard === 'object' ? guard.type : undefined;
|
|
313
|
+
throw new Error(`Unable to evaluate guard ${guardType ? `'${guardType}' ` : ''}in transition for event '${eventType}' in state node '${this.id}':\n${err.message}`);
|
|
305
314
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
* The target state value of the history state node, if it exists. This represents the
|
|
311
|
-
* default state value to transition to if no history value exists yet.
|
|
312
|
-
*/
|
|
313
|
-
}, {
|
|
314
|
-
key: "target",
|
|
315
|
-
get: function get() {
|
|
316
|
-
if (this.type === 'history') {
|
|
317
|
-
var historyConfig = this.config;
|
|
318
|
-
return historyConfig.target;
|
|
315
|
+
if (guardPassed) {
|
|
316
|
+
actions.push(...candidate.actions);
|
|
317
|
+
selectedTransition = candidate;
|
|
318
|
+
break;
|
|
319
319
|
}
|
|
320
|
-
return undefined;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
/**
|
|
324
|
-
* All the state node IDs of this state node and its descendant state nodes.
|
|
325
|
-
*/
|
|
326
|
-
}, {
|
|
327
|
-
key: "stateIds",
|
|
328
|
-
get: function get() {
|
|
329
|
-
var _this9 = this;
|
|
330
|
-
var childStateIds = flatten(Object.keys(this.states).map(function (stateKey) {
|
|
331
|
-
return _this9.states[stateKey].stateIds;
|
|
332
|
-
}));
|
|
333
|
-
return [this.id].concat(childStateIds);
|
|
334
320
|
}
|
|
321
|
+
return selectedTransition ? [selectedTransition] : undefined;
|
|
322
|
+
}
|
|
335
323
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
var _iterator2 = _createForOfIteratorHelper(state.events),
|
|
352
|
-
_step2;
|
|
353
|
-
try {
|
|
354
|
-
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
355
|
-
var event = _step2.value;
|
|
356
|
-
events.add("".concat(event));
|
|
357
|
-
}
|
|
358
|
-
} catch (err) {
|
|
359
|
-
_iterator2.e(err);
|
|
360
|
-
} finally {
|
|
361
|
-
_iterator2.f();
|
|
362
|
-
}
|
|
324
|
+
/**
|
|
325
|
+
* All the event types accepted by this state node and its descendants.
|
|
326
|
+
*/
|
|
327
|
+
get events() {
|
|
328
|
+
return memo(this, 'events', () => {
|
|
329
|
+
const {
|
|
330
|
+
states
|
|
331
|
+
} = this;
|
|
332
|
+
const events = new Set(this.ownEvents);
|
|
333
|
+
if (states) {
|
|
334
|
+
for (const stateId of Object.keys(states)) {
|
|
335
|
+
const state = states[stateId];
|
|
336
|
+
if (state.states) {
|
|
337
|
+
for (const event of state.events) {
|
|
338
|
+
events.add(`${event}`);
|
|
363
339
|
}
|
|
364
340
|
}
|
|
365
341
|
}
|
|
366
|
-
|
|
367
|
-
});
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
/**
|
|
371
|
-
* All the events that have transitions directly from this state node.
|
|
372
|
-
*
|
|
373
|
-
* Excludes any inert events.
|
|
374
|
-
*/
|
|
375
|
-
}, {
|
|
376
|
-
key: "ownEvents",
|
|
377
|
-
get: function get() {
|
|
378
|
-
var events = new Set(this.transitions.filter(function (transition) {
|
|
379
|
-
return !(!transition.target && !transition.actions.length && !transition.reenter);
|
|
380
|
-
}).map(function (transition) {
|
|
381
|
-
return transition.eventType;
|
|
382
|
-
}));
|
|
342
|
+
}
|
|
383
343
|
return Array.from(events);
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
return StateNode;
|
|
387
|
-
}();
|
|
344
|
+
});
|
|
345
|
+
}
|
|
388
346
|
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
347
|
+
/**
|
|
348
|
+
* All the events that have transitions directly from this state node.
|
|
349
|
+
*
|
|
350
|
+
* Excludes any inert events.
|
|
351
|
+
*/
|
|
352
|
+
get ownEvents() {
|
|
353
|
+
const events = new Set([...this.transitions.keys()].filter(descriptor => {
|
|
354
|
+
return this.transitions.get(descriptor).some(transition => !(!transition.target && !transition.actions.length && !transition.reenter));
|
|
355
|
+
}));
|
|
356
|
+
return Array.from(events);
|
|
357
|
+
}
|
|
398
358
|
}
|
|
399
|
-
|
|
400
|
-
|
|
359
|
+
|
|
360
|
+
const STATE_IDENTIFIER = '#';
|
|
361
|
+
class StateMachine {
|
|
362
|
+
constructor(
|
|
401
363
|
/**
|
|
402
364
|
* The raw config used to create the machine.
|
|
403
365
|
*/
|
|
404
|
-
config,
|
|
405
|
-
var _this$config$types;
|
|
406
|
-
_classCallCheck(this, StateMachine);
|
|
366
|
+
config, implementations) {
|
|
407
367
|
this.config = config;
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
_defineProperty(this, "__TActorMap", void 0);
|
|
422
|
-
_defineProperty(this, "__TResolvedTypesMeta", void 0);
|
|
368
|
+
/**
|
|
369
|
+
* The machine's own version.
|
|
370
|
+
*/
|
|
371
|
+
this.version = void 0;
|
|
372
|
+
this.implementations = void 0;
|
|
373
|
+
this.__xstatenode = true;
|
|
374
|
+
this.idMap = new Map();
|
|
375
|
+
this.root = void 0;
|
|
376
|
+
this.id = void 0;
|
|
377
|
+
this.states = void 0;
|
|
378
|
+
this.events = void 0;
|
|
379
|
+
/** @deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
|
|
380
|
+
this.__TResolvedTypesMeta = void 0;
|
|
423
381
|
this.id = config.id || '(machine)';
|
|
424
|
-
this.
|
|
425
|
-
|
|
382
|
+
this.implementations = {
|
|
383
|
+
actors: implementations?.actors ?? {},
|
|
384
|
+
actions: implementations?.actions ?? {},
|
|
385
|
+
delays: implementations?.delays ?? {},
|
|
386
|
+
guards: implementations?.guards ?? {}
|
|
387
|
+
};
|
|
426
388
|
this.version = this.config.version;
|
|
427
|
-
this.types = (_this$config$types = this.config.types) !== null && _this$config$types !== void 0 ? _this$config$types : {};
|
|
428
389
|
this.transition = this.transition.bind(this);
|
|
390
|
+
this.getInitialSnapshot = this.getInitialSnapshot.bind(this);
|
|
391
|
+
this.restoreSnapshot = this.restoreSnapshot.bind(this);
|
|
392
|
+
this.start = this.start.bind(this);
|
|
429
393
|
this.root = new StateNode(config, {
|
|
430
394
|
_key: this.id,
|
|
431
395
|
_machine: this
|
|
@@ -444,396 +408,194 @@ var StateMachine = /*#__PURE__*/function () {
|
|
|
444
408
|
*
|
|
445
409
|
* @returns A new `StateMachine` instance with the provided implementations.
|
|
446
410
|
*/
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
}, {
|
|
471
|
-
key: "provide",
|
|
472
|
-
value: function provide(implementations) {
|
|
473
|
-
var _this$options = this.options,
|
|
474
|
-
actions = _this$options.actions,
|
|
475
|
-
guards = _this$options.guards,
|
|
476
|
-
actors = _this$options.actors,
|
|
477
|
-
delays = _this$options.delays;
|
|
478
|
-
return new StateMachine(this.config, {
|
|
479
|
-
actions: _objectSpread2(_objectSpread2({}, actions), implementations.actions),
|
|
480
|
-
guards: _objectSpread2(_objectSpread2({}, guards), implementations.guards),
|
|
481
|
-
actors: _objectSpread2(_objectSpread2({}, actors), implementations.actors),
|
|
482
|
-
delays: _objectSpread2(_objectSpread2({}, delays), implementations.delays)
|
|
483
|
-
});
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
/**
|
|
487
|
-
* Resolves the given `state` to a new `State` instance relative to this machine.
|
|
488
|
-
*
|
|
489
|
-
* This ensures that `.nextEvents` represent the correct values.
|
|
490
|
-
*
|
|
491
|
-
* @param state The state to resolve
|
|
492
|
-
*/
|
|
493
|
-
}, {
|
|
494
|
-
key: "resolveState",
|
|
495
|
-
value: function resolveState(state) {
|
|
496
|
-
var configurationSet = getConfiguration(getStateNodes(this.root, state.value));
|
|
497
|
-
var configuration = Array.from(configurationSet);
|
|
498
|
-
return this.createState(_objectSpread2(_objectSpread2({}, state), {}, {
|
|
499
|
-
value: resolveStateValue(this.root, state.value),
|
|
500
|
-
configuration: configuration,
|
|
501
|
-
done: isInFinalState(configuration)
|
|
502
|
-
}));
|
|
503
|
-
}
|
|
504
|
-
}, {
|
|
505
|
-
key: "resolveStateValue",
|
|
506
|
-
value: function resolveStateValue$1(stateValue) {
|
|
507
|
-
var resolvedStateValue = resolveStateValue(this.root, stateValue);
|
|
508
|
-
var resolvedContext = this.getContext();
|
|
509
|
-
return this.resolveState(State.from(resolvedStateValue, resolvedContext, this));
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
/**
|
|
513
|
-
* Determines the next state given the current `state` and received `event`.
|
|
514
|
-
* Calculates a full macrostep from all microsteps.
|
|
515
|
-
*
|
|
516
|
-
* @param state The current State instance or state value
|
|
517
|
-
* @param event The received event
|
|
518
|
-
*/
|
|
519
|
-
}, {
|
|
520
|
-
key: "transition",
|
|
521
|
-
value: function transition() {
|
|
522
|
-
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.initialState;
|
|
523
|
-
var event = arguments.length > 1 ? arguments[1] : undefined;
|
|
524
|
-
var actorCtx = arguments.length > 2 ? arguments[2] : undefined;
|
|
525
|
-
var currentState = state instanceof State ? state : this.resolveStateValue(state);
|
|
526
|
-
// TODO: handle error events in a better way
|
|
527
|
-
var scxmlEvent = toSCXMLEvent(event);
|
|
528
|
-
if (isSCXMLErrorEvent(scxmlEvent) && !currentState.nextEvents.some(function (nextEvent) {
|
|
529
|
-
return nextEvent === scxmlEvent.name;
|
|
530
|
-
})) {
|
|
531
|
-
throw scxmlEvent.data.data;
|
|
411
|
+
provide(implementations) {
|
|
412
|
+
const {
|
|
413
|
+
actions,
|
|
414
|
+
guards,
|
|
415
|
+
actors,
|
|
416
|
+
delays
|
|
417
|
+
} = this.implementations;
|
|
418
|
+
return new StateMachine(this.config, {
|
|
419
|
+
actions: {
|
|
420
|
+
...actions,
|
|
421
|
+
...implementations.actions
|
|
422
|
+
},
|
|
423
|
+
guards: {
|
|
424
|
+
...guards,
|
|
425
|
+
...implementations.guards
|
|
426
|
+
},
|
|
427
|
+
actors: {
|
|
428
|
+
...actors,
|
|
429
|
+
...implementations.actors
|
|
430
|
+
},
|
|
431
|
+
delays: {
|
|
432
|
+
...delays,
|
|
433
|
+
...implementations.delays
|
|
532
434
|
}
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
resolveState(config) {
|
|
438
|
+
const resolvedStateValue = resolveStateValue(this.root, config.value);
|
|
439
|
+
const nodeSet = getAllStateNodes(getStateNodes(this.root, resolvedStateValue));
|
|
440
|
+
return createMachineSnapshot({
|
|
441
|
+
_nodes: [...nodeSet],
|
|
442
|
+
context: config.context || {},
|
|
443
|
+
children: {},
|
|
444
|
+
status: isInFinalState(nodeSet, this.root) ? 'done' : config.status || 'active',
|
|
445
|
+
output: config.output,
|
|
446
|
+
error: config.error,
|
|
447
|
+
historyValue: config.historyValue
|
|
448
|
+
}, this);
|
|
449
|
+
}
|
|
537
450
|
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.initialState;
|
|
549
|
-
var event = arguments.length > 1 ? arguments[1] : undefined;
|
|
550
|
-
var actorCtx = arguments.length > 2 ? arguments[2] : undefined;
|
|
551
|
-
var scxmlEvent = toSCXMLEvent(event);
|
|
552
|
-
var _macrostep2 = macrostep(state, scxmlEvent, actorCtx),
|
|
553
|
-
microstates = _macrostep2.microstates;
|
|
554
|
-
return microstates;
|
|
555
|
-
}
|
|
556
|
-
}, {
|
|
557
|
-
key: "getTransitionData",
|
|
558
|
-
value: function getTransitionData(state, _event) {
|
|
559
|
-
return transitionNode(this.root, state.value, state, _event) || [];
|
|
560
|
-
}
|
|
451
|
+
/**
|
|
452
|
+
* Determines the next snapshot given the current `snapshot` and received `event`.
|
|
453
|
+
* Calculates a full macrostep from all microsteps.
|
|
454
|
+
*
|
|
455
|
+
* @param snapshot The current snapshot
|
|
456
|
+
* @param event The received event
|
|
457
|
+
*/
|
|
458
|
+
transition(snapshot, event, actorScope) {
|
|
459
|
+
return macrostep(snapshot, event, actorScope).snapshot;
|
|
460
|
+
}
|
|
561
461
|
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
var preInitial = this.resolveState(this.createState({
|
|
576
|
-
value: {},
|
|
577
|
-
// TODO: this is computed in state constructor
|
|
578
|
-
context: context,
|
|
579
|
-
_event: createInitEvent({}),
|
|
580
|
-
actions: [],
|
|
581
|
-
meta: undefined,
|
|
582
|
-
configuration: config,
|
|
583
|
-
transitions: [],
|
|
584
|
-
children: {}
|
|
585
|
-
}));
|
|
586
|
-
preInitial._initial = true;
|
|
587
|
-
(_preInitial$actions = preInitial.actions).unshift.apply(_preInitial$actions, _toConsumableArray(actions));
|
|
588
|
-
if (actorCtx) {
|
|
589
|
-
var _resolveActionsAndCon = resolveActionsAndContext(actions, initEvent, preInitial, actorCtx),
|
|
590
|
-
nextState = _resolveActionsAndCon.nextState;
|
|
591
|
-
preInitial.children = nextState.children;
|
|
592
|
-
preInitial.actions = nextState.actions;
|
|
593
|
-
}
|
|
594
|
-
return preInitial;
|
|
595
|
-
}
|
|
462
|
+
/**
|
|
463
|
+
* Determines the next state given the current `state` and `event`.
|
|
464
|
+
* Calculates a microstep.
|
|
465
|
+
*
|
|
466
|
+
* @param state The current state
|
|
467
|
+
* @param event The received event
|
|
468
|
+
*/
|
|
469
|
+
microstep(snapshot, event, actorScope) {
|
|
470
|
+
return macrostep(snapshot, event, actorScope).microstates;
|
|
471
|
+
}
|
|
472
|
+
getTransitionData(snapshot, event) {
|
|
473
|
+
return transitionNode(this.root, snapshot.value, snapshot, event) || [];
|
|
474
|
+
}
|
|
596
475
|
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
476
|
+
/**
|
|
477
|
+
* The initial state _before_ evaluating any microsteps.
|
|
478
|
+
* This "pre-initial" state is provided to initial actions executed in the initial state.
|
|
479
|
+
*/
|
|
480
|
+
getPreInitialState(actorScope, initEvent, internalQueue) {
|
|
481
|
+
const {
|
|
482
|
+
context
|
|
483
|
+
} = this.config;
|
|
484
|
+
const preInitial = createMachineSnapshot({
|
|
485
|
+
context: typeof context !== 'function' && context ? context : {},
|
|
486
|
+
_nodes: [this.root],
|
|
487
|
+
children: {},
|
|
488
|
+
status: 'active'
|
|
489
|
+
}, this);
|
|
490
|
+
if (typeof context === 'function') {
|
|
491
|
+
const assignment = ({
|
|
492
|
+
spawn,
|
|
493
|
+
event
|
|
494
|
+
}) => context({
|
|
495
|
+
spawn,
|
|
496
|
+
input: event.input
|
|
497
|
+
});
|
|
498
|
+
return resolveActionsAndContext(preInitial, initEvent, actorScope, [assign(assignment)], internalQueue);
|
|
605
499
|
}
|
|
500
|
+
return preInitial;
|
|
501
|
+
}
|
|
606
502
|
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
503
|
+
/**
|
|
504
|
+
* Returns the initial `State` instance, with reference to `self` as an `ActorRef`.
|
|
505
|
+
*/
|
|
506
|
+
getInitialSnapshot(actorScope, input) {
|
|
507
|
+
const initEvent = createInitEvent(input); // TODO: fix;
|
|
508
|
+
const internalQueue = [];
|
|
509
|
+
const preInitialState = this.getPreInitialState(actorScope, initEvent, internalQueue);
|
|
510
|
+
const nextState = microstep([{
|
|
511
|
+
target: [...getInitialStateNodes(this.root)],
|
|
512
|
+
source: this.root,
|
|
513
|
+
reenter: true,
|
|
514
|
+
actions: [],
|
|
515
|
+
eventType: null,
|
|
516
|
+
toJSON: null // TODO: fix
|
|
517
|
+
}], preInitialState, actorScope, initEvent, true, internalQueue);
|
|
518
|
+
const {
|
|
519
|
+
snapshot: macroState
|
|
520
|
+
} = macrostep(nextState, initEvent, actorScope, internalQueue);
|
|
521
|
+
return macroState;
|
|
522
|
+
}
|
|
523
|
+
start(snapshot) {
|
|
524
|
+
Object.values(snapshot.children).forEach(child => {
|
|
525
|
+
if (child.getSnapshot().status === 'active') {
|
|
526
|
+
child.start();
|
|
527
|
+
}
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
getStateNodeById(stateId) {
|
|
531
|
+
const fullPath = stateId.split(STATE_DELIMITER);
|
|
532
|
+
const relativePath = fullPath.slice(1);
|
|
533
|
+
const resolvedStateId = isStateId(fullPath[0]) ? fullPath[0].slice(STATE_IDENTIFIER.length) : fullPath[0];
|
|
534
|
+
const stateNode = this.idMap.get(resolvedStateId);
|
|
535
|
+
if (!stateNode) {
|
|
536
|
+
throw new Error(`Child state node '#${resolvedStateId}' does not exist on machine '${this.id}'`);
|
|
537
|
+
}
|
|
538
|
+
return getStateNodeByPath(stateNode, relativePath);
|
|
539
|
+
}
|
|
540
|
+
get definition() {
|
|
541
|
+
return this.root.definition;
|
|
542
|
+
}
|
|
543
|
+
toJSON() {
|
|
544
|
+
return this.definition;
|
|
545
|
+
}
|
|
546
|
+
getPersistedSnapshot(snapshot, options) {
|
|
547
|
+
return getPersistedSnapshot(snapshot, options);
|
|
548
|
+
}
|
|
549
|
+
restoreSnapshot(snapshot, _actorScope) {
|
|
550
|
+
const children = {};
|
|
551
|
+
const snapshotChildren = snapshot.children;
|
|
552
|
+
Object.keys(snapshotChildren).forEach(actorId => {
|
|
553
|
+
const actorData = snapshotChildren[actorId];
|
|
554
|
+
const childState = actorData.snapshot;
|
|
555
|
+
const src = actorData.src;
|
|
556
|
+
const logic = typeof src === 'string' ? resolveReferencedActor(this, src) : src;
|
|
557
|
+
if (!logic) {
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
const actorRef = createActor(logic, {
|
|
561
|
+
id: actorId,
|
|
562
|
+
parent: _actorScope?.self,
|
|
563
|
+
syncSnapshot: actorData.syncSnapshot,
|
|
564
|
+
snapshot: childState,
|
|
565
|
+
src,
|
|
566
|
+
systemId: actorData.systemId
|
|
629
567
|
});
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
568
|
+
children[actorId] = actorRef;
|
|
569
|
+
});
|
|
570
|
+
const restoredSnapshot = createMachineSnapshot({
|
|
571
|
+
...snapshot,
|
|
572
|
+
children,
|
|
573
|
+
_nodes: Array.from(getAllStateNodes(getStateNodes(this.root, snapshot.value)))
|
|
574
|
+
}, this);
|
|
575
|
+
let seen = new Set();
|
|
576
|
+
function reviveContext(contextPart, children) {
|
|
577
|
+
if (seen.has(contextPart)) {
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
seen.add(contextPart);
|
|
581
|
+
for (let key in contextPart) {
|
|
582
|
+
const value = contextPart[key];
|
|
583
|
+
if (value && typeof value === 'object') {
|
|
584
|
+
if ('xstate$$type' in value && value.xstate$$type === $$ACTOR_TYPE) {
|
|
585
|
+
contextPart[key] = children[value.id];
|
|
586
|
+
continue;
|
|
638
587
|
}
|
|
588
|
+
reviveContext(value, children);
|
|
639
589
|
}
|
|
640
|
-
});
|
|
641
|
-
}
|
|
642
|
-
}, {
|
|
643
|
-
key: "getStateNodeById",
|
|
644
|
-
value: function getStateNodeById(stateId) {
|
|
645
|
-
var fullPath = stateId.split(this.delimiter);
|
|
646
|
-
var relativePath = fullPath.slice(1);
|
|
647
|
-
var resolvedStateId = isStateId(fullPath[0]) ? fullPath[0].slice(STATE_IDENTIFIER.length) : fullPath[0];
|
|
648
|
-
var stateNode = this.idMap.get(resolvedStateId);
|
|
649
|
-
if (!stateNode) {
|
|
650
|
-
throw new Error("Child state node '#".concat(resolvedStateId, "' does not exist on machine '").concat(this.id, "'"));
|
|
651
590
|
}
|
|
652
|
-
return getStateNodeByPath(stateNode, relativePath);
|
|
653
|
-
}
|
|
654
|
-
}, {
|
|
655
|
-
key: "definition",
|
|
656
|
-
get: function get() {
|
|
657
|
-
return _objectSpread2({
|
|
658
|
-
context: this.getContext()
|
|
659
|
-
}, this.root.definition);
|
|
660
|
-
}
|
|
661
|
-
}, {
|
|
662
|
-
key: "toJSON",
|
|
663
|
-
value: function toJSON() {
|
|
664
|
-
return this.definition;
|
|
665
|
-
}
|
|
666
|
-
}, {
|
|
667
|
-
key: "getPersistedState",
|
|
668
|
-
value: function getPersistedState$1(state) {
|
|
669
|
-
return getPersistedState(state);
|
|
670
|
-
}
|
|
671
|
-
}, {
|
|
672
|
-
key: "createState",
|
|
673
|
-
value: function createState(stateConfig) {
|
|
674
|
-
var state = stateConfig instanceof State ? stateConfig : new State(stateConfig, this);
|
|
675
|
-
var _resolveActionsAndCon2 = resolveActionsAndContext(state.actions, state._event, state, undefined),
|
|
676
|
-
resolvedState = _resolveActionsAndCon2.nextState;
|
|
677
|
-
return resolvedState;
|
|
678
|
-
}
|
|
679
|
-
}, {
|
|
680
|
-
key: "getStatus",
|
|
681
|
-
value: function getStatus(state) {
|
|
682
|
-
return state.done ? {
|
|
683
|
-
status: 'done',
|
|
684
|
-
data: state.output
|
|
685
|
-
} : {
|
|
686
|
-
status: 'active'
|
|
687
|
-
};
|
|
688
|
-
}
|
|
689
|
-
}, {
|
|
690
|
-
key: "restoreState",
|
|
691
|
-
value: function restoreState(state, _actorCtx) {
|
|
692
|
-
var _this = this;
|
|
693
|
-
var children = {};
|
|
694
|
-
Object.keys(state.children).forEach(function (actorId) {
|
|
695
|
-
var _resolveReferencedAct, _behavior$restoreStat;
|
|
696
|
-
var actorData = state.children[actorId];
|
|
697
|
-
var childState = actorData.state;
|
|
698
|
-
var src = actorData.src;
|
|
699
|
-
var behavior = src ? (_resolveReferencedAct = resolveReferencedActor(_this.options.actors[src])) === null || _resolveReferencedAct === void 0 ? void 0 : _resolveReferencedAct.src : undefined;
|
|
700
|
-
if (!behavior) {
|
|
701
|
-
return;
|
|
702
|
-
}
|
|
703
|
-
var actorState = (_behavior$restoreStat = behavior.restoreState) === null || _behavior$restoreStat === void 0 ? void 0 : _behavior$restoreStat.call(behavior, childState, _actorCtx);
|
|
704
|
-
var actorRef = interpret(behavior, {
|
|
705
|
-
id: actorId,
|
|
706
|
-
state: actorState
|
|
707
|
-
});
|
|
708
|
-
children[actorId] = actorRef;
|
|
709
|
-
});
|
|
710
|
-
var restoredState = this.createState(new State(_objectSpread2(_objectSpread2({}, state), {}, {
|
|
711
|
-
children: children
|
|
712
|
-
}), this));
|
|
713
|
-
|
|
714
|
-
// TODO: DRY this up
|
|
715
|
-
restoredState.configuration.forEach(function (stateNode) {
|
|
716
|
-
if (stateNode.invoke) {
|
|
717
|
-
stateNode.invoke.forEach(function (invokeConfig) {
|
|
718
|
-
var id = invokeConfig.id,
|
|
719
|
-
src = invokeConfig.src;
|
|
720
|
-
if (children[id]) {
|
|
721
|
-
return;
|
|
722
|
-
}
|
|
723
|
-
var referenced = resolveReferencedActor(_this.options.actors[src]);
|
|
724
|
-
if (referenced) {
|
|
725
|
-
var actorRef = interpret(referenced.src, {
|
|
726
|
-
id: id,
|
|
727
|
-
parent: _actorCtx === null || _actorCtx === void 0 ? void 0 : _actorCtx.self,
|
|
728
|
-
input: 'input' in invokeConfig ? invokeConfig.input : referenced.input
|
|
729
|
-
});
|
|
730
|
-
children[id] = actorRef;
|
|
731
|
-
}
|
|
732
|
-
});
|
|
733
|
-
}
|
|
734
|
-
});
|
|
735
|
-
restoredState.actions = [];
|
|
736
|
-
return restoredState;
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
/**@deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
|
|
740
|
-
}]);
|
|
741
|
-
return StateMachine;
|
|
742
|
-
}();
|
|
743
|
-
|
|
744
|
-
function createMachine(config, implementations) {
|
|
745
|
-
return new StateMachine(config, implementations);
|
|
746
|
-
}
|
|
747
|
-
|
|
748
|
-
function mapState(stateMap, stateId) {
|
|
749
|
-
var foundStateId;
|
|
750
|
-
for (var _i = 0, _Object$keys = Object.keys(stateMap); _i < _Object$keys.length; _i++) {
|
|
751
|
-
var mappedStateId = _Object$keys[_i];
|
|
752
|
-
if (matchesState(mappedStateId, stateId) && (!foundStateId || stateId.length > foundStateId.length)) {
|
|
753
|
-
foundStateId = mappedStateId;
|
|
754
591
|
}
|
|
592
|
+
reviveContext(restoredSnapshot.context, children);
|
|
593
|
+
return restoredSnapshot;
|
|
755
594
|
}
|
|
756
|
-
return stateMap[foundStateId];
|
|
757
595
|
}
|
|
758
596
|
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
_classCallCheck(this, SimulatedClock);
|
|
762
|
-
_defineProperty(this, "timeouts", new Map());
|
|
763
|
-
_defineProperty(this, "_now", 0);
|
|
764
|
-
_defineProperty(this, "_id", 0);
|
|
765
|
-
}
|
|
766
|
-
_createClass(SimulatedClock, [{
|
|
767
|
-
key: "now",
|
|
768
|
-
value: function now() {
|
|
769
|
-
return this._now;
|
|
770
|
-
}
|
|
771
|
-
}, {
|
|
772
|
-
key: "getId",
|
|
773
|
-
value: function getId() {
|
|
774
|
-
return this._id++;
|
|
775
|
-
}
|
|
776
|
-
}, {
|
|
777
|
-
key: "setTimeout",
|
|
778
|
-
value: function setTimeout(fn, timeout) {
|
|
779
|
-
var id = this.getId();
|
|
780
|
-
this.timeouts.set(id, {
|
|
781
|
-
start: this.now(),
|
|
782
|
-
timeout: timeout,
|
|
783
|
-
fn: fn
|
|
784
|
-
});
|
|
785
|
-
return id;
|
|
786
|
-
}
|
|
787
|
-
}, {
|
|
788
|
-
key: "clearTimeout",
|
|
789
|
-
value: function clearTimeout(id) {
|
|
790
|
-
this.timeouts["delete"](id);
|
|
791
|
-
}
|
|
792
|
-
}, {
|
|
793
|
-
key: "set",
|
|
794
|
-
value: function set(time) {
|
|
795
|
-
if (this._now > time) {
|
|
796
|
-
throw new Error('Unable to travel back in time');
|
|
797
|
-
}
|
|
798
|
-
this._now = time;
|
|
799
|
-
this.flushTimeouts();
|
|
800
|
-
}
|
|
801
|
-
}, {
|
|
802
|
-
key: "flushTimeouts",
|
|
803
|
-
value: function flushTimeouts() {
|
|
804
|
-
var _this = this;
|
|
805
|
-
_toConsumableArray(this.timeouts).sort(function (_ref, _ref2) {
|
|
806
|
-
var _ref3 = _slicedToArray(_ref, 2);
|
|
807
|
-
_ref3[0];
|
|
808
|
-
var timeoutA = _ref3[1];
|
|
809
|
-
var _ref4 = _slicedToArray(_ref2, 2);
|
|
810
|
-
_ref4[0];
|
|
811
|
-
var timeoutB = _ref4[1];
|
|
812
|
-
var endA = timeoutA.start + timeoutA.timeout;
|
|
813
|
-
var endB = timeoutB.start + timeoutB.timeout;
|
|
814
|
-
return endB > endA ? -1 : 1;
|
|
815
|
-
}).forEach(function (_ref5) {
|
|
816
|
-
var _ref6 = _slicedToArray(_ref5, 2),
|
|
817
|
-
id = _ref6[0],
|
|
818
|
-
timeout = _ref6[1];
|
|
819
|
-
if (_this.now() - timeout.start >= timeout.timeout) {
|
|
820
|
-
_this.timeouts["delete"](id);
|
|
821
|
-
timeout.fn.call(null);
|
|
822
|
-
}
|
|
823
|
-
});
|
|
824
|
-
}
|
|
825
|
-
}, {
|
|
826
|
-
key: "increment",
|
|
827
|
-
value: function increment(ms) {
|
|
828
|
-
this._now += ms;
|
|
829
|
-
this.flushTimeouts();
|
|
830
|
-
}
|
|
831
|
-
}]);
|
|
832
|
-
return SimulatedClock;
|
|
833
|
-
}();
|
|
834
|
-
|
|
835
|
-
var defaultWaitForOptions = {
|
|
836
|
-
timeout: 10000 // 10 seconds
|
|
597
|
+
const defaultWaitForOptions = {
|
|
598
|
+
timeout: Infinity // much more than 10 seconds
|
|
837
599
|
};
|
|
838
600
|
|
|
839
601
|
/**
|
|
@@ -858,20 +620,20 @@ var defaultWaitForOptions = {
|
|
|
858
620
|
* that matches the condition
|
|
859
621
|
*/
|
|
860
622
|
function waitFor(actorRef, predicate, options) {
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
623
|
+
const resolvedOptions = {
|
|
624
|
+
...defaultWaitForOptions,
|
|
625
|
+
...options
|
|
626
|
+
};
|
|
627
|
+
return new Promise((res, rej) => {
|
|
628
|
+
let done = false;
|
|
629
|
+
const handle = resolvedOptions.timeout === Infinity ? undefined : setTimeout(() => {
|
|
868
630
|
sub.unsubscribe();
|
|
869
|
-
rej(new Error(
|
|
631
|
+
rej(new Error(`Timeout of ${resolvedOptions.timeout} ms exceeded`));
|
|
870
632
|
}, resolvedOptions.timeout);
|
|
871
|
-
|
|
633
|
+
const dispose = () => {
|
|
872
634
|
clearTimeout(handle);
|
|
873
635
|
done = true;
|
|
874
|
-
sub
|
|
636
|
+
sub?.unsubscribe();
|
|
875
637
|
};
|
|
876
638
|
function checkEmitted(emitted) {
|
|
877
639
|
if (predicate(emitted)) {
|
|
@@ -879,18 +641,22 @@ function waitFor(actorRef, predicate, options) {
|
|
|
879
641
|
res(emitted);
|
|
880
642
|
}
|
|
881
643
|
}
|
|
644
|
+
let sub; // avoid TDZ when disposing synchronously
|
|
882
645
|
|
|
883
646
|
// See if the current snapshot already matches the predicate
|
|
884
647
|
checkEmitted(actorRef.getSnapshot());
|
|
885
|
-
|
|
648
|
+
if (done) {
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
sub = actorRef.subscribe({
|
|
886
652
|
next: checkEmitted,
|
|
887
|
-
error:
|
|
653
|
+
error: err => {
|
|
888
654
|
dispose();
|
|
889
655
|
rej(err);
|
|
890
656
|
},
|
|
891
|
-
complete:
|
|
657
|
+
complete: () => {
|
|
892
658
|
dispose();
|
|
893
|
-
rej(new Error(
|
|
659
|
+
rej(new Error(`Actor terminated without satisfying predicate`));
|
|
894
660
|
}
|
|
895
661
|
});
|
|
896
662
|
if (done) {
|
|
@@ -899,4 +665,29 @@ function waitFor(actorRef, predicate, options) {
|
|
|
899
665
|
});
|
|
900
666
|
}
|
|
901
667
|
|
|
902
|
-
|
|
668
|
+
// this is not 100% accurate since we can't make parallel regions required in the result
|
|
669
|
+
// `TTestValue` doesn't encode this information anyhow for us to be able to do that
|
|
670
|
+
// this is fine for most practical use cases anyway though
|
|
671
|
+
function createMachine(config, implementations) {
|
|
672
|
+
return new StateMachine(config, implementations);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
// at the moment we allow extra actors - ones that are not specified by `children`
|
|
676
|
+
// this could be reconsidered in the future
|
|
677
|
+
function setup({
|
|
678
|
+
actors,
|
|
679
|
+
actions,
|
|
680
|
+
guards,
|
|
681
|
+
delays
|
|
682
|
+
}) {
|
|
683
|
+
return {
|
|
684
|
+
createMachine: config => createMachine(config, {
|
|
685
|
+
actors,
|
|
686
|
+
actions,
|
|
687
|
+
guards,
|
|
688
|
+
delays
|
|
689
|
+
})
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
export { SimulatedClock, StateMachine, StateNode, createMachine, setup, waitFor };
|