xstate 5.0.0-beta.44 → 5.0.0-beta.46
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/actions/dist/xstate-actions.cjs.js +2 -2
- package/actions/dist/xstate-actions.development.cjs.js +2 -2
- package/actions/dist/xstate-actions.development.esm.js +2 -2
- package/actions/dist/xstate-actions.esm.js +2 -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 +134 -9
- package/actors/dist/xstate-actors.development.cjs.js +134 -9
- package/actors/dist/xstate-actors.development.esm.js +134 -9
- package/actors/dist/xstate-actors.esm.js +134 -9
- package/actors/dist/xstate-actors.umd.min.js +1 -1
- package/actors/dist/xstate-actors.umd.min.js.map +1 -1
- package/dist/declarations/src/State.d.ts +18 -28
- package/dist/declarations/src/StateMachine.d.ts +11 -29
- package/dist/declarations/src/StateNode.d.ts +8 -6
- package/dist/declarations/src/actions/choose.d.ts +3 -3
- package/dist/declarations/src/actions/pure.d.ts +4 -4
- package/dist/declarations/src/actors/observable.d.ts +80 -4
- package/dist/declarations/src/actors/transition.d.ts +53 -4
- package/dist/declarations/src/createMachine.d.ts +5 -0
- package/dist/declarations/src/guards.d.ts +26 -4
- package/dist/declarations/src/index.d.ts +3 -2
- package/dist/declarations/src/interpreter.d.ts +1 -0
- package/dist/declarations/src/setup.d.ts +35 -0
- package/dist/declarations/src/stateUtils.d.ts +7 -7
- package/dist/declarations/src/types.d.ts +54 -25
- package/dist/declarations/src/utils.d.ts +2 -1
- package/dist/{raise-5854eaca.esm.js → raise-1682abb7.esm.js} +99 -135
- package/dist/{raise-fb6f017b.cjs.js → raise-a1d3d7e9.cjs.js} +100 -136
- package/dist/{raise-ed700d14.development.cjs.js → raise-a9e7e31c.development.cjs.js} +100 -136
- package/dist/{raise-348cc74e.development.esm.js → raise-fa23c2b9.development.esm.js} +99 -135
- package/dist/{send-53e5693c.cjs.js → send-2fa3a204.cjs.js} +24 -28
- package/dist/{send-00466e37.development.cjs.js → send-5b256a89.development.cjs.js} +24 -28
- package/dist/{send-a0193bdb.development.esm.js → send-9acdf858.development.esm.js} +24 -28
- package/dist/{send-b7b4befa.esm.js → send-a237e4e8.esm.js} +24 -28
- package/dist/xstate.cjs.js +102 -92
- package/dist/xstate.cjs.mjs +2 -0
- package/dist/xstate.development.cjs.js +102 -92
- package/dist/xstate.development.cjs.mjs +2 -0
- package/dist/xstate.development.esm.js +97 -89
- package/dist/xstate.esm.js +97 -89
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/guards/dist/xstate-guards.cjs.js +1 -1
- package/guards/dist/xstate-guards.development.cjs.js +1 -1
- package/guards/dist/xstate-guards.development.esm.js +1 -1
- package/guards/dist/xstate-guards.esm.js +1 -1
- package/guards/dist/xstate-guards.umd.min.js +1 -1
- package/guards/dist/xstate-guards.umd.min.js.map +1 -1
- package/package.json +1 -1
- package/dist/declarations/src/Machine.d.ts +0 -5
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var actors_dist_xstateActors = require('../actors/dist/xstate-actors.development.cjs.js');
|
|
6
|
-
var guards_dist_xstateGuards = require('./raise-
|
|
7
|
-
var send = require('./send-
|
|
6
|
+
var guards_dist_xstateGuards = require('./raise-a9e7e31c.development.cjs.js');
|
|
7
|
+
var send = require('./send-5b256a89.development.cjs.js');
|
|
8
8
|
require('../dev/dist/xstate-dev.development.cjs.js');
|
|
9
9
|
|
|
10
10
|
class SimulatedClock {
|
|
@@ -56,6 +56,20 @@ class SimulatedClock {
|
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
+
const cache = new WeakMap();
|
|
60
|
+
function memo(object, key, fn) {
|
|
61
|
+
let memoizedData = cache.get(object);
|
|
62
|
+
if (!memoizedData) {
|
|
63
|
+
memoizedData = {
|
|
64
|
+
[key]: fn()
|
|
65
|
+
};
|
|
66
|
+
cache.set(object, memoizedData);
|
|
67
|
+
} else if (!(key in memoizedData)) {
|
|
68
|
+
memoizedData[key] = fn();
|
|
69
|
+
}
|
|
70
|
+
return memoizedData[key];
|
|
71
|
+
}
|
|
72
|
+
|
|
59
73
|
const EMPTY_OBJECT = {};
|
|
60
74
|
const toSerializableAction = action => {
|
|
61
75
|
if (typeof action === 'string') {
|
|
@@ -76,85 +90,72 @@ const toSerializableAction = action => {
|
|
|
76
90
|
return action;
|
|
77
91
|
};
|
|
78
92
|
class StateNode {
|
|
79
|
-
/**
|
|
80
|
-
* The relative key of the state node, which represents its location in the overall state value.
|
|
81
|
-
*/
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* The unique ID of the state node.
|
|
85
|
-
*/
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* The type of this state node:
|
|
89
|
-
*
|
|
90
|
-
* - `'atomic'` - no child state nodes
|
|
91
|
-
* - `'compound'` - nested child state nodes (XOR)
|
|
92
|
-
* - `'parallel'` - orthogonal nested child state nodes (AND)
|
|
93
|
-
* - `'history'` - history state node
|
|
94
|
-
* - `'final'` - final state node
|
|
95
|
-
*/
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* The string path from the root machine node to this node.
|
|
99
|
-
*/
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* The child state nodes.
|
|
103
|
-
*/
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* The type of history on this state node. Can be:
|
|
107
|
-
*
|
|
108
|
-
* - `'shallow'` - recalls only top-level historical state value
|
|
109
|
-
* - `'deep'` - recalls historical state value at all levels
|
|
110
|
-
*/
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* The action(s) to be executed upon entering the state node.
|
|
114
|
-
*/
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* The action(s) to be executed upon exiting the state node.
|
|
118
|
-
*/
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* The parent state node.
|
|
122
|
-
*/
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* The root machine node.
|
|
126
|
-
*/
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* The meta data associated with this state node, which will be returned in State instances.
|
|
130
|
-
*/
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* The output data sent with the "xstate.done.state._id_" event if this is a final state node.
|
|
134
|
-
*/
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* The order this state node appears. Corresponds to the implicit document order.
|
|
138
|
-
*/
|
|
139
|
-
|
|
140
93
|
constructor(
|
|
141
94
|
/**
|
|
142
95
|
* The raw config used to create the machine.
|
|
143
96
|
*/
|
|
144
97
|
config, options) {
|
|
145
98
|
this.config = config;
|
|
99
|
+
/**
|
|
100
|
+
* The relative key of the state node, which represents its location in the overall state value.
|
|
101
|
+
*/
|
|
146
102
|
this.key = void 0;
|
|
103
|
+
/**
|
|
104
|
+
* The unique ID of the state node.
|
|
105
|
+
*/
|
|
147
106
|
this.id = void 0;
|
|
107
|
+
/**
|
|
108
|
+
* The type of this state node:
|
|
109
|
+
*
|
|
110
|
+
* - `'atomic'` - no child state nodes
|
|
111
|
+
* - `'compound'` - nested child state nodes (XOR)
|
|
112
|
+
* - `'parallel'` - orthogonal nested child state nodes (AND)
|
|
113
|
+
* - `'history'` - history state node
|
|
114
|
+
* - `'final'` - final state node
|
|
115
|
+
*/
|
|
148
116
|
this.type = void 0;
|
|
117
|
+
/**
|
|
118
|
+
* The string path from the root machine node to this node.
|
|
119
|
+
*/
|
|
149
120
|
this.path = void 0;
|
|
121
|
+
/**
|
|
122
|
+
* The child state nodes.
|
|
123
|
+
*/
|
|
150
124
|
this.states = void 0;
|
|
125
|
+
/**
|
|
126
|
+
* The type of history on this state node. Can be:
|
|
127
|
+
*
|
|
128
|
+
* - `'shallow'` - recalls only top-level historical state value
|
|
129
|
+
* - `'deep'` - recalls historical state value at all levels
|
|
130
|
+
*/
|
|
151
131
|
this.history = void 0;
|
|
132
|
+
/**
|
|
133
|
+
* The action(s) to be executed upon entering the state node.
|
|
134
|
+
*/
|
|
152
135
|
this.entry = void 0;
|
|
136
|
+
/**
|
|
137
|
+
* The action(s) to be executed upon exiting the state node.
|
|
138
|
+
*/
|
|
153
139
|
this.exit = void 0;
|
|
140
|
+
/**
|
|
141
|
+
* The parent state node.
|
|
142
|
+
*/
|
|
154
143
|
this.parent = void 0;
|
|
144
|
+
/**
|
|
145
|
+
* The root machine node.
|
|
146
|
+
*/
|
|
155
147
|
this.machine = void 0;
|
|
148
|
+
/**
|
|
149
|
+
* The meta data associated with this state node, which will be returned in State instances.
|
|
150
|
+
*/
|
|
156
151
|
this.meta = void 0;
|
|
152
|
+
/**
|
|
153
|
+
* The output data sent with the "xstate.done.state._id_" event if this is a final state node.
|
|
154
|
+
*/
|
|
157
155
|
this.output = void 0;
|
|
156
|
+
/**
|
|
157
|
+
* The order this state node appears. Corresponds to the implicit document order.
|
|
158
|
+
*/
|
|
158
159
|
this.order = -1;
|
|
159
160
|
this.description = void 0;
|
|
160
161
|
this.tags = [];
|
|
@@ -248,7 +249,7 @@ class StateNode {
|
|
|
248
249
|
* The logic invoked as actors by this state node.
|
|
249
250
|
*/
|
|
250
251
|
get invoke() {
|
|
251
|
-
return
|
|
252
|
+
return memo(this, 'invoke', () => guards_dist_xstateGuards.toArray(this.config.invoke).map((invokeConfig, i) => {
|
|
252
253
|
const {
|
|
253
254
|
src,
|
|
254
255
|
systemId
|
|
@@ -281,7 +282,7 @@ class StateNode {
|
|
|
281
282
|
* The mapping of events to transitions.
|
|
282
283
|
*/
|
|
283
284
|
get on() {
|
|
284
|
-
return
|
|
285
|
+
return memo(this, 'on', () => {
|
|
285
286
|
const transitions = this.transitions;
|
|
286
287
|
return [...transitions].flatMap(([descriptor, t]) => t.map(t => [descriptor, t])).reduce((map, [descriptor, transition]) => {
|
|
287
288
|
map[descriptor] = map[descriptor] || [];
|
|
@@ -291,16 +292,16 @@ class StateNode {
|
|
|
291
292
|
});
|
|
292
293
|
}
|
|
293
294
|
get after() {
|
|
294
|
-
return
|
|
295
|
+
return memo(this, 'delayedTransitions', () => guards_dist_xstateGuards.getDelayedTransitions(this));
|
|
295
296
|
}
|
|
296
297
|
get initial() {
|
|
297
|
-
return
|
|
298
|
+
return memo(this, 'initial', () => guards_dist_xstateGuards.formatInitialTransition(this, this.config.initial));
|
|
298
299
|
}
|
|
299
300
|
next(state, event) {
|
|
300
301
|
const eventType = event.type;
|
|
301
302
|
const actions = [];
|
|
302
303
|
let selectedTransition;
|
|
303
|
-
const candidates =
|
|
304
|
+
const candidates = memo(this, `candidates-${eventType}`, () => guards_dist_xstateGuards.getCandidates(this, eventType));
|
|
304
305
|
for (const candidate of candidates) {
|
|
305
306
|
const {
|
|
306
307
|
guard
|
|
@@ -326,7 +327,7 @@ class StateNode {
|
|
|
326
327
|
* All the event types accepted by this state node and its descendants.
|
|
327
328
|
*/
|
|
328
329
|
get events() {
|
|
329
|
-
return
|
|
330
|
+
return memo(this, 'events', () => {
|
|
330
331
|
const {
|
|
331
332
|
states
|
|
332
333
|
} = this;
|
|
@@ -360,16 +361,15 @@ class StateNode {
|
|
|
360
361
|
|
|
361
362
|
const STATE_IDENTIFIER = '#';
|
|
362
363
|
class StateMachine {
|
|
363
|
-
/**
|
|
364
|
-
* The machine's own version.
|
|
365
|
-
*/
|
|
366
|
-
|
|
367
364
|
constructor(
|
|
368
365
|
/**
|
|
369
366
|
* The raw config used to create the machine.
|
|
370
367
|
*/
|
|
371
368
|
config, implementations) {
|
|
372
369
|
this.config = config;
|
|
370
|
+
/**
|
|
371
|
+
* The machine's own version.
|
|
372
|
+
*/
|
|
373
373
|
this.version = void 0;
|
|
374
374
|
this.implementations = void 0;
|
|
375
375
|
this.types = void 0;
|
|
@@ -379,15 +379,7 @@ class StateMachine {
|
|
|
379
379
|
this.id = void 0;
|
|
380
380
|
this.states = void 0;
|
|
381
381
|
this.events = void 0;
|
|
382
|
-
|
|
383
|
-
this.__TEvent = void 0;
|
|
384
|
-
this.__TActor = void 0;
|
|
385
|
-
this.__TAction = void 0;
|
|
386
|
-
this.__TGuard = void 0;
|
|
387
|
-
this.__TDelay = void 0;
|
|
388
|
-
this.__TTag = void 0;
|
|
389
|
-
this.__TInput = void 0;
|
|
390
|
-
this.__TOutput = void 0;
|
|
382
|
+
/** @deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
|
|
391
383
|
this.__TResolvedTypesMeta = void 0;
|
|
392
384
|
this.id = config.id || '(machine)';
|
|
393
385
|
this.implementations = {
|
|
@@ -451,12 +443,12 @@ class StateMachine {
|
|
|
451
443
|
}
|
|
452
444
|
resolveState(config) {
|
|
453
445
|
const resolvedStateValue = guards_dist_xstateGuards.resolveStateValue(this.root, config.value);
|
|
454
|
-
const
|
|
446
|
+
const nodeSet = guards_dist_xstateGuards.getAllStateNodes(guards_dist_xstateGuards.getStateNodes(this.root, resolvedStateValue));
|
|
455
447
|
return guards_dist_xstateGuards.createMachineSnapshot({
|
|
456
|
-
|
|
448
|
+
_nodes: [...nodeSet],
|
|
457
449
|
context: config.context || {},
|
|
458
450
|
children: {},
|
|
459
|
-
status: guards_dist_xstateGuards.isInFinalState(
|
|
451
|
+
status: guards_dist_xstateGuards.isInFinalState(nodeSet, this.root) ? 'done' : config.status || 'active',
|
|
460
452
|
output: config.output,
|
|
461
453
|
error: config.error,
|
|
462
454
|
historyValue: config.historyValue
|
|
@@ -472,7 +464,7 @@ class StateMachine {
|
|
|
472
464
|
*/
|
|
473
465
|
transition(state, event, actorScope) {
|
|
474
466
|
// TODO: handle error events in a better way
|
|
475
|
-
if (guards_dist_xstateGuards.isErrorActorEvent(event) && !state.
|
|
467
|
+
if (guards_dist_xstateGuards.isErrorActorEvent(event) && !guards_dist_xstateGuards.getAllOwnEventDescriptors(state).some(nextEvent => nextEvent === event.type)) {
|
|
476
468
|
return guards_dist_xstateGuards.cloneMachineSnapshot(state, {
|
|
477
469
|
status: 'error',
|
|
478
470
|
error: event.data
|
|
@@ -508,7 +500,7 @@ class StateMachine {
|
|
|
508
500
|
} = this.config;
|
|
509
501
|
const preInitial = guards_dist_xstateGuards.createMachineSnapshot({
|
|
510
502
|
context: typeof context !== 'function' && context ? context : {},
|
|
511
|
-
|
|
503
|
+
_nodes: [this.root],
|
|
512
504
|
children: {},
|
|
513
505
|
status: 'active'
|
|
514
506
|
}, this);
|
|
@@ -582,11 +574,11 @@ class StateMachine {
|
|
|
582
574
|
if (!logic) {
|
|
583
575
|
return;
|
|
584
576
|
}
|
|
585
|
-
const actorState = logic.restoreState?.(childState, _actorScope);
|
|
586
577
|
const actorRef = guards_dist_xstateGuards.createActor(logic, {
|
|
587
578
|
id: actorId,
|
|
588
579
|
parent: _actorScope?.self,
|
|
589
|
-
|
|
580
|
+
syncSnapshot: actorData.syncSnapshot,
|
|
581
|
+
state: childState,
|
|
590
582
|
src,
|
|
591
583
|
systemId: actorData.systemId
|
|
592
584
|
});
|
|
@@ -595,7 +587,7 @@ class StateMachine {
|
|
|
595
587
|
const restoredSnapshot = guards_dist_xstateGuards.createMachineSnapshot({
|
|
596
588
|
...snapshot,
|
|
597
589
|
children,
|
|
598
|
-
|
|
590
|
+
_nodes: Array.from(guards_dist_xstateGuards.getAllStateNodes(guards_dist_xstateGuards.getStateNodes(this.root, snapshot.value)))
|
|
599
591
|
}, this);
|
|
600
592
|
let seen = new Set();
|
|
601
593
|
function reviveContext(contextPart, children) {
|
|
@@ -617,8 +609,6 @@ class StateMachine {
|
|
|
617
609
|
reviveContext(restoredSnapshot.context, children);
|
|
618
610
|
return restoredSnapshot;
|
|
619
611
|
}
|
|
620
|
-
|
|
621
|
-
/**@deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
|
|
622
612
|
}
|
|
623
613
|
|
|
624
614
|
const defaultWaitForOptions = {
|
|
@@ -699,6 +689,24 @@ function createMachine(config, implementations) {
|
|
|
699
689
|
return new StateMachine(config, implementations);
|
|
700
690
|
}
|
|
701
691
|
|
|
692
|
+
// at the moment we allow extra actors - ones that are not specified by `children`
|
|
693
|
+
// this could be reconsidered in the future
|
|
694
|
+
function setup({
|
|
695
|
+
actors,
|
|
696
|
+
actions,
|
|
697
|
+
guards,
|
|
698
|
+
delays
|
|
699
|
+
}) {
|
|
700
|
+
return {
|
|
701
|
+
createMachine: config => createMachine(config, {
|
|
702
|
+
actors,
|
|
703
|
+
actions,
|
|
704
|
+
guards,
|
|
705
|
+
delays
|
|
706
|
+
})
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
|
|
702
710
|
exports.createEmptyActor = actors_dist_xstateActors.createEmptyActor;
|
|
703
711
|
exports.fromCallback = actors_dist_xstateActors.fromCallback;
|
|
704
712
|
exports.fromEventObservable = actors_dist_xstateActors.fromEventObservable;
|
|
@@ -706,6 +714,7 @@ exports.fromObservable = actors_dist_xstateActors.fromObservable;
|
|
|
706
714
|
exports.fromPromise = actors_dist_xstateActors.fromPromise;
|
|
707
715
|
exports.fromTransition = actors_dist_xstateActors.fromTransition;
|
|
708
716
|
exports.Actor = guards_dist_xstateGuards.Actor;
|
|
717
|
+
exports.__unsafe_getAllOwnEventDescriptors = guards_dist_xstateGuards.getAllOwnEventDescriptors;
|
|
709
718
|
exports.and = guards_dist_xstateGuards.and;
|
|
710
719
|
exports.cancel = guards_dist_xstateGuards.cancel;
|
|
711
720
|
exports.createActor = guards_dist_xstateGuards.createActor;
|
|
@@ -734,4 +743,5 @@ exports.SimulatedClock = SimulatedClock;
|
|
|
734
743
|
exports.StateMachine = StateMachine;
|
|
735
744
|
exports.StateNode = StateNode;
|
|
736
745
|
exports.createMachine = createMachine;
|
|
746
|
+
exports.setup = setup;
|
|
737
747
|
exports.waitFor = waitFor;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export { createEmptyActor, fromCallback, fromEventObservable, fromObservable, fromPromise, fromTransition } from '../actors/dist/xstate-actors.development.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,
|
|
3
|
-
export { D as Actor, J as and, O as cancel, C as createActor,
|
|
4
|
-
import { a as assign } from './send-
|
|
5
|
-
export { S as SpecialTargets, a as assign, c as choose, e as escalate, f as forwardTo, l as log, p as pure, s as sendParent, b as sendTo } from './send-
|
|
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 isErrorActorEvent, o as getAllOwnEventDescriptors, p as cloneMachineSnapshot, q as macrostep, s as transitionNode, u as resolveActionsAndContext, v as createInitEvent, w as microstep, x as getInitialStateNodes, y as isStateId, z as getStateNodeByPath, A as getPersistedState, B as resolveReferencedActor, C as createActor, $ as $$ACTOR_TYPE } from './raise-fa23c2b9.development.esm.js';
|
|
3
|
+
export { D as Actor, o as __unsafe_getAllOwnEventDescriptors, J as and, O as cancel, C as createActor, j as getStateNodes, E as interpret, F as isMachineSnapshot, G as matchesState, K as not, L as or, H as pathToStateValue, P as raise, R as spawn, M as stateIn, Q as stop, I as toObserver } from './raise-fa23c2b9.development.esm.js';
|
|
4
|
+
import { a as assign } from './send-9acdf858.development.esm.js';
|
|
5
|
+
export { S as SpecialTargets, a as assign, c as choose, e as escalate, f as forwardTo, l as log, p as pure, s as sendParent, b as sendTo } from './send-9acdf858.development.esm.js';
|
|
6
6
|
import '../dev/dist/xstate-dev.development.esm.js';
|
|
7
7
|
|
|
8
8
|
class SimulatedClock {
|
|
@@ -54,6 +54,20 @@ class SimulatedClock {
|
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
|
|
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
|
+
}
|
|
70
|
+
|
|
57
71
|
const EMPTY_OBJECT = {};
|
|
58
72
|
const toSerializableAction = action => {
|
|
59
73
|
if (typeof action === 'string') {
|
|
@@ -74,85 +88,72 @@ const toSerializableAction = action => {
|
|
|
74
88
|
return action;
|
|
75
89
|
};
|
|
76
90
|
class StateNode {
|
|
77
|
-
/**
|
|
78
|
-
* The relative key of the state node, which represents its location in the overall state value.
|
|
79
|
-
*/
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* The unique ID of the state node.
|
|
83
|
-
*/
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* The type of this state node:
|
|
87
|
-
*
|
|
88
|
-
* - `'atomic'` - no child state nodes
|
|
89
|
-
* - `'compound'` - nested child state nodes (XOR)
|
|
90
|
-
* - `'parallel'` - orthogonal nested child state nodes (AND)
|
|
91
|
-
* - `'history'` - history state node
|
|
92
|
-
* - `'final'` - final state node
|
|
93
|
-
*/
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* The string path from the root machine node to this node.
|
|
97
|
-
*/
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* The child state nodes.
|
|
101
|
-
*/
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* The type of history on this state node. Can be:
|
|
105
|
-
*
|
|
106
|
-
* - `'shallow'` - recalls only top-level historical state value
|
|
107
|
-
* - `'deep'` - recalls historical state value at all levels
|
|
108
|
-
*/
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* The action(s) to be executed upon entering the state node.
|
|
112
|
-
*/
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* The action(s) to be executed upon exiting the state node.
|
|
116
|
-
*/
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* The parent state node.
|
|
120
|
-
*/
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* The root machine node.
|
|
124
|
-
*/
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* The meta data associated with this state node, which will be returned in State instances.
|
|
128
|
-
*/
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* The output data sent with the "xstate.done.state._id_" event if this is a final state node.
|
|
132
|
-
*/
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* The order this state node appears. Corresponds to the implicit document order.
|
|
136
|
-
*/
|
|
137
|
-
|
|
138
91
|
constructor(
|
|
139
92
|
/**
|
|
140
93
|
* The raw config used to create the machine.
|
|
141
94
|
*/
|
|
142
95
|
config, options) {
|
|
143
96
|
this.config = config;
|
|
97
|
+
/**
|
|
98
|
+
* The relative key of the state node, which represents its location in the overall state value.
|
|
99
|
+
*/
|
|
144
100
|
this.key = void 0;
|
|
101
|
+
/**
|
|
102
|
+
* The unique ID of the state node.
|
|
103
|
+
*/
|
|
145
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
|
+
*/
|
|
146
114
|
this.type = void 0;
|
|
115
|
+
/**
|
|
116
|
+
* The string path from the root machine node to this node.
|
|
117
|
+
*/
|
|
147
118
|
this.path = void 0;
|
|
119
|
+
/**
|
|
120
|
+
* The child state nodes.
|
|
121
|
+
*/
|
|
148
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
|
+
*/
|
|
149
129
|
this.history = void 0;
|
|
130
|
+
/**
|
|
131
|
+
* The action(s) to be executed upon entering the state node.
|
|
132
|
+
*/
|
|
150
133
|
this.entry = void 0;
|
|
134
|
+
/**
|
|
135
|
+
* The action(s) to be executed upon exiting the state node.
|
|
136
|
+
*/
|
|
151
137
|
this.exit = void 0;
|
|
138
|
+
/**
|
|
139
|
+
* The parent state node.
|
|
140
|
+
*/
|
|
152
141
|
this.parent = void 0;
|
|
142
|
+
/**
|
|
143
|
+
* The root machine node.
|
|
144
|
+
*/
|
|
153
145
|
this.machine = void 0;
|
|
146
|
+
/**
|
|
147
|
+
* The meta data associated with this state node, which will be returned in State instances.
|
|
148
|
+
*/
|
|
154
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
|
+
*/
|
|
155
153
|
this.output = void 0;
|
|
154
|
+
/**
|
|
155
|
+
* The order this state node appears. Corresponds to the implicit document order.
|
|
156
|
+
*/
|
|
156
157
|
this.order = -1;
|
|
157
158
|
this.description = void 0;
|
|
158
159
|
this.tags = [];
|
|
@@ -358,16 +359,15 @@ class StateNode {
|
|
|
358
359
|
|
|
359
360
|
const STATE_IDENTIFIER = '#';
|
|
360
361
|
class StateMachine {
|
|
361
|
-
/**
|
|
362
|
-
* The machine's own version.
|
|
363
|
-
*/
|
|
364
|
-
|
|
365
362
|
constructor(
|
|
366
363
|
/**
|
|
367
364
|
* The raw config used to create the machine.
|
|
368
365
|
*/
|
|
369
366
|
config, implementations) {
|
|
370
367
|
this.config = config;
|
|
368
|
+
/**
|
|
369
|
+
* The machine's own version.
|
|
370
|
+
*/
|
|
371
371
|
this.version = void 0;
|
|
372
372
|
this.implementations = void 0;
|
|
373
373
|
this.types = void 0;
|
|
@@ -377,15 +377,7 @@ class StateMachine {
|
|
|
377
377
|
this.id = void 0;
|
|
378
378
|
this.states = void 0;
|
|
379
379
|
this.events = void 0;
|
|
380
|
-
|
|
381
|
-
this.__TEvent = void 0;
|
|
382
|
-
this.__TActor = void 0;
|
|
383
|
-
this.__TAction = void 0;
|
|
384
|
-
this.__TGuard = void 0;
|
|
385
|
-
this.__TDelay = void 0;
|
|
386
|
-
this.__TTag = void 0;
|
|
387
|
-
this.__TInput = void 0;
|
|
388
|
-
this.__TOutput = void 0;
|
|
380
|
+
/** @deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
|
|
389
381
|
this.__TResolvedTypesMeta = void 0;
|
|
390
382
|
this.id = config.id || '(machine)';
|
|
391
383
|
this.implementations = {
|
|
@@ -449,12 +441,12 @@ class StateMachine {
|
|
|
449
441
|
}
|
|
450
442
|
resolveState(config) {
|
|
451
443
|
const resolvedStateValue = resolveStateValue(this.root, config.value);
|
|
452
|
-
const
|
|
444
|
+
const nodeSet = getAllStateNodes(getStateNodes(this.root, resolvedStateValue));
|
|
453
445
|
return createMachineSnapshot({
|
|
454
|
-
|
|
446
|
+
_nodes: [...nodeSet],
|
|
455
447
|
context: config.context || {},
|
|
456
448
|
children: {},
|
|
457
|
-
status: isInFinalState(
|
|
449
|
+
status: isInFinalState(nodeSet, this.root) ? 'done' : config.status || 'active',
|
|
458
450
|
output: config.output,
|
|
459
451
|
error: config.error,
|
|
460
452
|
historyValue: config.historyValue
|
|
@@ -470,7 +462,7 @@ class StateMachine {
|
|
|
470
462
|
*/
|
|
471
463
|
transition(state, event, actorScope) {
|
|
472
464
|
// TODO: handle error events in a better way
|
|
473
|
-
if (isErrorActorEvent(event) && !state.
|
|
465
|
+
if (isErrorActorEvent(event) && !getAllOwnEventDescriptors(state).some(nextEvent => nextEvent === event.type)) {
|
|
474
466
|
return cloneMachineSnapshot(state, {
|
|
475
467
|
status: 'error',
|
|
476
468
|
error: event.data
|
|
@@ -506,7 +498,7 @@ class StateMachine {
|
|
|
506
498
|
} = this.config;
|
|
507
499
|
const preInitial = createMachineSnapshot({
|
|
508
500
|
context: typeof context !== 'function' && context ? context : {},
|
|
509
|
-
|
|
501
|
+
_nodes: [this.root],
|
|
510
502
|
children: {},
|
|
511
503
|
status: 'active'
|
|
512
504
|
}, this);
|
|
@@ -580,11 +572,11 @@ class StateMachine {
|
|
|
580
572
|
if (!logic) {
|
|
581
573
|
return;
|
|
582
574
|
}
|
|
583
|
-
const actorState = logic.restoreState?.(childState, _actorScope);
|
|
584
575
|
const actorRef = createActor(logic, {
|
|
585
576
|
id: actorId,
|
|
586
577
|
parent: _actorScope?.self,
|
|
587
|
-
|
|
578
|
+
syncSnapshot: actorData.syncSnapshot,
|
|
579
|
+
state: childState,
|
|
588
580
|
src,
|
|
589
581
|
systemId: actorData.systemId
|
|
590
582
|
});
|
|
@@ -593,7 +585,7 @@ class StateMachine {
|
|
|
593
585
|
const restoredSnapshot = createMachineSnapshot({
|
|
594
586
|
...snapshot,
|
|
595
587
|
children,
|
|
596
|
-
|
|
588
|
+
_nodes: Array.from(getAllStateNodes(getStateNodes(this.root, snapshot.value)))
|
|
597
589
|
}, this);
|
|
598
590
|
let seen = new Set();
|
|
599
591
|
function reviveContext(contextPart, children) {
|
|
@@ -615,8 +607,6 @@ class StateMachine {
|
|
|
615
607
|
reviveContext(restoredSnapshot.context, children);
|
|
616
608
|
return restoredSnapshot;
|
|
617
609
|
}
|
|
618
|
-
|
|
619
|
-
/**@deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
|
|
620
610
|
}
|
|
621
611
|
|
|
622
612
|
const defaultWaitForOptions = {
|
|
@@ -697,4 +687,22 @@ function createMachine(config, implementations) {
|
|
|
697
687
|
return new StateMachine(config, implementations);
|
|
698
688
|
}
|
|
699
689
|
|
|
700
|
-
|
|
690
|
+
// at the moment we allow extra actors - ones that are not specified by `children`
|
|
691
|
+
// this could be reconsidered in the future
|
|
692
|
+
function setup({
|
|
693
|
+
actors,
|
|
694
|
+
actions,
|
|
695
|
+
guards,
|
|
696
|
+
delays
|
|
697
|
+
}) {
|
|
698
|
+
return {
|
|
699
|
+
createMachine: config => createMachine(config, {
|
|
700
|
+
actors,
|
|
701
|
+
actions,
|
|
702
|
+
guards,
|
|
703
|
+
delays
|
|
704
|
+
})
|
|
705
|
+
};
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
export { SimulatedClock, StateMachine, StateNode, createMachine, setup, waitFor };
|