xstate 3.2.1 → 3.3.3
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/.vscode/launch.json +15 -13
- package/README.md +37 -9
- package/dist/xstate.js +1 -1
- package/dist/xstate.utils.js +1 -1
- package/es/Machine.d.ts +2 -2
- package/es/Machine.js +2 -2
- package/es/State.d.ts +8 -7
- package/es/State.js +3 -2
- package/es/StateNode.d.ts +50 -13
- package/es/StateNode.js +617 -412
- package/es/graph.d.ts +9 -6
- package/es/graph.js +31 -24
- package/es/patterns.js +1 -1
- package/es/scxml.d.ts +2 -1
- package/es/scxml.js +33 -10
- package/es/types.d.ts +38 -7
- package/es/utils.d.ts +14 -1
- package/es/utils.js +33 -5
- package/lib/Machine.d.ts +2 -2
- package/lib/Machine.js +2 -2
- package/lib/State.d.ts +8 -7
- package/lib/State.js +3 -2
- package/lib/StateNode.d.ts +50 -13
- package/lib/StateNode.js +616 -411
- package/lib/graph.d.ts +9 -6
- package/lib/graph.js +30 -22
- package/lib/patterns.js +1 -1
- package/lib/scxml.d.ts +2 -1
- package/lib/scxml.js +33 -10
- package/lib/types.d.ts +38 -7
- package/lib/utils.d.ts +14 -1
- package/lib/utils.js +35 -5
- package/package.json +3 -3
- package/src/Machine.ts +5 -3
- package/src/State.ts +10 -2
- package/src/StateNode.ts +966 -590
- package/src/graph.ts +60 -31
- package/src/scxml.ts +80 -49
- package/src/types.ts +48 -7
- package/src/utils.ts +52 -7
- package/test/actions.test.ts +24 -1
- package/test/activities.test.ts +165 -0
- package/test/deep.test.ts +14 -16
- package/test/deterministic.test.ts +26 -5
- package/test/examples/6.17.test.ts +64 -0
- package/test/fixtures/id.ts +1 -1
- package/test/graph.test.ts +39 -16
- package/test/guards.test.ts +172 -15
- package/test/history.test.ts +193 -58
- package/test/invalid.test.ts +48 -0
- package/test/multiple.test.ts +12 -18
- package/test/parallel.test.ts +472 -1
- package/test/scxml.test.ts +13 -4
- package/test/stateIn.test.ts +1 -1
- package/test/transient.test.ts +183 -1
package/es/graph.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { StateNode } from './index';
|
|
2
2
|
import { Machine, Edge, PathMap, PathItem, PathsItem, PathsMap, AdjacencyMap } from './types';
|
|
3
3
|
export declare function getNodes(node: StateNode): StateNode[];
|
|
4
|
-
export declare function
|
|
5
|
-
export declare function
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export declare function
|
|
9
|
-
export declare function
|
|
4
|
+
export declare function getEventEdges(node: StateNode, event: string): Edge[];
|
|
5
|
+
export declare function getEdges(node: StateNode, options?: {
|
|
6
|
+
deep: boolean;
|
|
7
|
+
}): Edge[];
|
|
8
|
+
export declare function getAdjacencyMap(node: Machine, extendedState?: any): AdjacencyMap;
|
|
9
|
+
export declare function getShortestPaths(machine: Machine, extendedState?: any): PathMap;
|
|
10
|
+
export declare function getShortestPathsAsArray(machine: Machine, extendedState?: any): PathItem[];
|
|
11
|
+
export declare function getSimplePaths(machine: Machine, extendedState?: any): PathsMap;
|
|
12
|
+
export declare function getSimplePathsAsArray(machine: Machine, extendedState?: any): PathsItem[];
|
package/es/graph.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { toStateValue, getActionType } from './utils';
|
|
1
|
+
import { toStateValue, getActionType, flatMap } from './utils';
|
|
2
2
|
var EMPTY_MAP = {};
|
|
3
3
|
export function getNodes(node) {
|
|
4
4
|
var states = node.states;
|
|
@@ -10,21 +10,28 @@ export function getNodes(node) {
|
|
|
10
10
|
}, []);
|
|
11
11
|
return nodes;
|
|
12
12
|
}
|
|
13
|
-
function getEventEdges(node, event) {
|
|
13
|
+
export function getEventEdges(node, event) {
|
|
14
14
|
var transitions = node.on[event];
|
|
15
|
-
return transitions.map(function (transition) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
15
|
+
return flatMap(transitions.map(function (transition) {
|
|
16
|
+
var targets = [].concat(transition.target);
|
|
17
|
+
return targets.map(function (target) {
|
|
18
|
+
var targetNode = node.getRelativeStateNodes(target, undefined, false)[0];
|
|
19
|
+
return {
|
|
20
|
+
source: node,
|
|
21
|
+
target: targetNode,
|
|
22
|
+
event: event,
|
|
23
|
+
actions: transition.actions
|
|
24
|
+
? transition.actions.map(getActionType)
|
|
25
|
+
: [],
|
|
26
|
+
cond: transition.cond
|
|
27
|
+
};
|
|
28
|
+
});
|
|
29
|
+
}));
|
|
24
30
|
}
|
|
25
|
-
export function getEdges(node) {
|
|
31
|
+
export function getEdges(node, options) {
|
|
32
|
+
var _a = (options || {}).deep, deep = _a === void 0 ? true : _a;
|
|
26
33
|
var edges = [];
|
|
27
|
-
if (node.states) {
|
|
34
|
+
if (node.states && deep) {
|
|
28
35
|
Object.keys(node.states).forEach(function (stateKey) {
|
|
29
36
|
edges.push.apply(edges, getEdges(node.states[stateKey]));
|
|
30
37
|
});
|
|
@@ -34,7 +41,7 @@ export function getEdges(node) {
|
|
|
34
41
|
});
|
|
35
42
|
return edges;
|
|
36
43
|
}
|
|
37
|
-
export function getAdjacencyMap(node) {
|
|
44
|
+
export function getAdjacencyMap(node, extendedState) {
|
|
38
45
|
var adjacency = {};
|
|
39
46
|
var events = node.events;
|
|
40
47
|
function findAdjacencies(stateValue) {
|
|
@@ -45,7 +52,7 @@ export function getAdjacencyMap(node) {
|
|
|
45
52
|
adjacency[stateKey] = {};
|
|
46
53
|
for (var _i = 0, events_1 = events; _i < events_1.length; _i++) {
|
|
47
54
|
var event_1 = events_1[_i];
|
|
48
|
-
var nextState = node.transition(stateValue, event_1);
|
|
55
|
+
var nextState = node.transition(stateValue, event_1, extendedState);
|
|
49
56
|
adjacency[stateKey][event_1] = { state: nextState.value };
|
|
50
57
|
findAdjacencies(nextState.value);
|
|
51
58
|
}
|
|
@@ -53,11 +60,12 @@ export function getAdjacencyMap(node) {
|
|
|
53
60
|
findAdjacencies(node.initialState.value);
|
|
54
61
|
return adjacency;
|
|
55
62
|
}
|
|
56
|
-
export function getShortestPaths(machine) {
|
|
63
|
+
export function getShortestPaths(machine, extendedState) {
|
|
64
|
+
var _a;
|
|
57
65
|
if (!machine.states) {
|
|
58
66
|
return EMPTY_MAP;
|
|
59
67
|
}
|
|
60
|
-
var adjacency = getAdjacencyMap(machine);
|
|
68
|
+
var adjacency = getAdjacencyMap(machine, extendedState);
|
|
61
69
|
var initialStateId = JSON.stringify(machine.initialState.value);
|
|
62
70
|
var pathMap = (_a = {},
|
|
63
71
|
_a[initialStateId] = [],
|
|
@@ -97,20 +105,19 @@ export function getShortestPaths(machine) {
|
|
|
97
105
|
}
|
|
98
106
|
util(machine.initialState.value);
|
|
99
107
|
return pathMap;
|
|
100
|
-
var _a;
|
|
101
108
|
}
|
|
102
|
-
export function getShortestPathsAsArray(machine) {
|
|
103
|
-
var result = getShortestPaths(machine);
|
|
109
|
+
export function getShortestPathsAsArray(machine, extendedState) {
|
|
110
|
+
var result = getShortestPaths(machine, extendedState);
|
|
104
111
|
return Object.keys(result).map(function (key) { return ({
|
|
105
112
|
state: JSON.parse(key),
|
|
106
113
|
path: result[key]
|
|
107
114
|
}); });
|
|
108
115
|
}
|
|
109
|
-
export function getSimplePaths(machine) {
|
|
116
|
+
export function getSimplePaths(machine, extendedState) {
|
|
110
117
|
if (!machine.states) {
|
|
111
118
|
return EMPTY_MAP;
|
|
112
119
|
}
|
|
113
|
-
var adjacency = getAdjacencyMap(machine);
|
|
120
|
+
var adjacency = getAdjacencyMap(machine, extendedState);
|
|
114
121
|
var visited = new Set();
|
|
115
122
|
var path = [];
|
|
116
123
|
var paths = {};
|
|
@@ -143,8 +150,8 @@ export function getSimplePaths(machine) {
|
|
|
143
150
|
});
|
|
144
151
|
return paths;
|
|
145
152
|
}
|
|
146
|
-
export function getSimplePathsAsArray(machine) {
|
|
147
|
-
var result = getSimplePaths(machine);
|
|
153
|
+
export function getSimplePathsAsArray(machine, extendedState) {
|
|
154
|
+
var result = getSimplePaths(machine, extendedState);
|
|
148
155
|
return Object.keys(result).map(function (key) { return ({
|
|
149
156
|
state: JSON.parse(key),
|
|
150
157
|
paths: result[key]
|
package/es/patterns.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export function toggle(onState, offState, eventType) {
|
|
2
|
+
var _a, _b, _c;
|
|
2
3
|
return _a = {},
|
|
3
4
|
_a[onState] = {
|
|
4
5
|
on: (_b = {}, _b[eventType] = offState, _b)
|
|
@@ -7,5 +8,4 @@ export function toggle(onState, offState, eventType) {
|
|
|
7
8
|
on: (_c = {}, _c[eventType] = onState, _c)
|
|
8
9
|
},
|
|
9
10
|
_a;
|
|
10
|
-
var _a, _b, _c;
|
|
11
11
|
}
|
package/es/scxml.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Machine, EventObject } from './types';
|
|
2
2
|
export declare function fromMachine(machine: Machine): string;
|
|
3
3
|
export interface ScxmlToMachineOptions {
|
|
4
|
-
evalCond: (expr: string) =>
|
|
4
|
+
evalCond: (expr: string) => // tslint:disable-next-line:ban-types
|
|
5
|
+
((extState: any, event: EventObject) => boolean) | Function;
|
|
5
6
|
delimiter?: string;
|
|
6
7
|
}
|
|
7
8
|
export declare function toMachine(xml: string, options: ScxmlToMachineOptions): any;
|
package/es/scxml.js
CHANGED
|
@@ -73,10 +73,9 @@ function stateNodeToSCXML(stateNode) {
|
|
|
73
73
|
return {
|
|
74
74
|
type: 'element',
|
|
75
75
|
name: 'transition',
|
|
76
|
-
attributes: __assign({}, event ? { event: event } : undefined, { target: stateNode.parent.
|
|
77
|
-
.id }, targetTransition.cond
|
|
76
|
+
attributes: __assign({}, (event ? { event: event } : undefined), { target: stateNode.parent.getRelativeStateNodes(targetTransition.target)[0].id }, (targetTransition.cond
|
|
78
77
|
? { cond: targetTransition.cond.toString() }
|
|
79
|
-
: undefined),
|
|
78
|
+
: undefined)),
|
|
80
79
|
elements: targetTransition.actions
|
|
81
80
|
? targetTransition.actions.map(function (action) { return ({
|
|
82
81
|
type: 'element',
|
|
@@ -94,9 +93,10 @@ function stateNodeToSCXML(stateNode) {
|
|
|
94
93
|
return {
|
|
95
94
|
type: 'element',
|
|
96
95
|
name: 'transition',
|
|
97
|
-
attributes: __assign({}, event ? { event: event } : undefined, { target: stateNode.parent.
|
|
96
|
+
attributes: __assign({}, (event ? { event: event } : undefined), { target: stateNode.parent.getRelativeStateNodes(target)[0]
|
|
97
|
+
.id }, (targetTransition.cond
|
|
98
98
|
? { cond: targetTransition.cond.toString() }
|
|
99
|
-
: undefined),
|
|
99
|
+
: undefined)),
|
|
100
100
|
elements: targetTransition.actions
|
|
101
101
|
? targetTransition.actions.map(function (action) { return ({
|
|
102
102
|
type: 'element',
|
|
@@ -169,8 +169,31 @@ function toConfig(nodeJson, id, options) {
|
|
|
169
169
|
var initial = parallel ? undefined : nodeJson.attributes.initial;
|
|
170
170
|
var states;
|
|
171
171
|
var on;
|
|
172
|
+
var elements = nodeJson.elements;
|
|
173
|
+
switch (nodeJson.name) {
|
|
174
|
+
case 'history': {
|
|
175
|
+
if (!elements) {
|
|
176
|
+
return {
|
|
177
|
+
id: id,
|
|
178
|
+
history: nodeJson.attributes.type || 'shallow'
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
var transitionElement = elements.filter(function (element) { return element.name === 'transition'; })[0];
|
|
182
|
+
return {
|
|
183
|
+
id: id,
|
|
184
|
+
history: nodeJson.attributes.type || 'shallow',
|
|
185
|
+
target: "#" + transitionElement.attributes.target
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
default:
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
172
191
|
if (nodeJson.elements) {
|
|
173
|
-
var stateElements = nodeJson.elements.filter(function (element) {
|
|
192
|
+
var stateElements = nodeJson.elements.filter(function (element) {
|
|
193
|
+
return element.name === 'state' ||
|
|
194
|
+
element.name === 'parallel' ||
|
|
195
|
+
element.name === 'history';
|
|
196
|
+
});
|
|
174
197
|
var transitionElements = nodeJson.elements.filter(function (element) { return element.name === 'transition'; });
|
|
175
198
|
var onEntryElement = nodeJson.elements.find(function (element) { return element.name === 'onentry'; });
|
|
176
199
|
var onExitElement = nodeJson.elements.find(function (element) { return element.name === 'onexit'; });
|
|
@@ -182,11 +205,11 @@ function toConfig(nodeJson, id, options) {
|
|
|
182
205
|
}
|
|
183
206
|
states = indexedRecord(stateElements, function (item) { return "" + item.attributes.id; });
|
|
184
207
|
on = mapValues(indexedAggregateRecord(transitionElements, function (item) { return item.attributes.event || ''; }), function (values) {
|
|
185
|
-
return values.map(function (value) { return (__assign({ target: "#" + value.attributes.target }, value.elements ? executableContent(value.elements) : undefined, value.attributes.cond
|
|
208
|
+
return values.map(function (value) { return (__assign({ target: "#" + value.attributes.target }, (value.elements ? executableContent(value.elements) : undefined), (value.attributes.cond
|
|
186
209
|
? {
|
|
187
210
|
cond: evalCond(value.attributes.cond)
|
|
188
211
|
}
|
|
189
|
-
: undefined)); });
|
|
212
|
+
: undefined))); });
|
|
190
213
|
});
|
|
191
214
|
var onEntry = onEntryElement
|
|
192
215
|
? onEntryElement.elements.map(function (element) {
|
|
@@ -208,13 +231,13 @@ function toConfig(nodeJson, id, options) {
|
|
|
208
231
|
}
|
|
209
232
|
})
|
|
210
233
|
: undefined;
|
|
211
|
-
return __assign({ id: id, delimiter: options.delimiter }, initial ? { initial: initial } : undefined, parallel ? { parallel: parallel } : undefined, stateElements.length
|
|
234
|
+
return __assign({ id: id, delimiter: options.delimiter }, (initial ? { initial: initial } : undefined), (parallel ? { parallel: parallel } : undefined), (stateElements.length
|
|
212
235
|
? {
|
|
213
236
|
states: mapValues(states, function (state, key) {
|
|
214
237
|
return toConfig(state, key, options);
|
|
215
238
|
})
|
|
216
239
|
}
|
|
217
|
-
: undefined, transitionElements.length ? { on: on } : undefined, onEntry ? { onEntry: onEntry } : undefined, onExit ? { onExit: onExit } : undefined);
|
|
240
|
+
: undefined), (transitionElements.length ? { on: on } : undefined), (onEntry ? { onEntry: onEntry } : undefined), (onExit ? { onExit: onExit } : undefined));
|
|
218
241
|
}
|
|
219
242
|
return { id: id };
|
|
220
243
|
}
|
package/es/types.d.ts
CHANGED
|
@@ -19,11 +19,17 @@ export interface StateValueMap {
|
|
|
19
19
|
[key: string]: StateValue;
|
|
20
20
|
}
|
|
21
21
|
export declare type StateValue = string | StateValueMap;
|
|
22
|
-
export
|
|
22
|
+
export interface HistoryValue {
|
|
23
|
+
states: Record<string, HistoryValue | undefined>;
|
|
24
|
+
current: StateValue | undefined;
|
|
25
|
+
}
|
|
26
|
+
export declare type ConditionPredicate = (extendedState: any, event: EventObject, microstepState: StateValue) => boolean;
|
|
27
|
+
export declare type Condition = string | ConditionPredicate;
|
|
23
28
|
export interface TransitionConfig {
|
|
24
|
-
cond?:
|
|
29
|
+
cond?: Condition;
|
|
25
30
|
actions?: Action[];
|
|
26
31
|
in?: StateValue;
|
|
32
|
+
internal?: boolean;
|
|
27
33
|
}
|
|
28
34
|
export interface TargetTransitionConfig extends TransitionConfig {
|
|
29
35
|
target: string | string[];
|
|
@@ -35,6 +41,12 @@ export interface StateNodeConfig {
|
|
|
35
41
|
key?: string;
|
|
36
42
|
initial?: string | undefined;
|
|
37
43
|
parallel?: boolean | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* Indicates whether the state node is a history state node, and what
|
|
46
|
+
* type of history:
|
|
47
|
+
* shallow, deep, true (shallow), false (none), undefined (none)
|
|
48
|
+
*/
|
|
49
|
+
history?: 'shallow' | 'deep' | boolean | undefined;
|
|
38
50
|
states?: Record<string, SimpleOrCompoundStateNodeConfig> | undefined;
|
|
39
51
|
on?: Record<string, Transition | undefined>;
|
|
40
52
|
onEntry?: Action | Action[];
|
|
@@ -51,15 +63,24 @@ export interface SimpleStateNodeConfig extends StateNodeConfig {
|
|
|
51
63
|
parallel?: false | undefined;
|
|
52
64
|
states?: undefined;
|
|
53
65
|
}
|
|
66
|
+
export interface HistoryStateNodeConfig extends SimpleStateNodeConfig {
|
|
67
|
+
history: 'shallow' | 'deep' | true;
|
|
68
|
+
target: StateValue | undefined;
|
|
69
|
+
}
|
|
54
70
|
export interface CompoundStateNodeConfig extends StateNodeConfig {
|
|
55
71
|
initial?: string;
|
|
56
72
|
parallel?: boolean;
|
|
57
73
|
states: Record<string, SimpleOrCompoundStateNodeConfig>;
|
|
74
|
+
history?: false | undefined;
|
|
58
75
|
}
|
|
59
76
|
export declare type SimpleOrCompoundStateNodeConfig = CompoundStateNodeConfig | SimpleStateNodeConfig;
|
|
77
|
+
export interface MachineOptions {
|
|
78
|
+
guards: Record<string, ConditionPredicate>;
|
|
79
|
+
}
|
|
60
80
|
export interface MachineConfig extends CompoundStateNodeConfig {
|
|
61
81
|
key?: string;
|
|
62
82
|
strict?: boolean;
|
|
83
|
+
history?: false | undefined;
|
|
63
84
|
}
|
|
64
85
|
export interface StandardMachineConfig extends MachineConfig {
|
|
65
86
|
initial: string;
|
|
@@ -68,7 +89,6 @@ export interface StandardMachineConfig extends MachineConfig {
|
|
|
68
89
|
export interface ParallelMachineConfig extends MachineConfig {
|
|
69
90
|
initial?: undefined;
|
|
70
91
|
parallel: true;
|
|
71
|
-
states: Record<string, CompoundStateNodeConfig>;
|
|
72
92
|
}
|
|
73
93
|
export interface EntryExitEffectMap {
|
|
74
94
|
entry: Action[];
|
|
@@ -79,6 +99,8 @@ export interface StateNode {
|
|
|
79
99
|
id: string;
|
|
80
100
|
initial: string | undefined;
|
|
81
101
|
parallel: boolean;
|
|
102
|
+
transient: boolean;
|
|
103
|
+
history: false | 'shallow' | 'deep';
|
|
82
104
|
states: Record<string, StateNode>;
|
|
83
105
|
on?: Record<string, Transition>;
|
|
84
106
|
onEntry?: Action | Action[];
|
|
@@ -88,6 +110,7 @@ export interface StateNode {
|
|
|
88
110
|
}
|
|
89
111
|
export interface ComplexStateNode extends StateNode {
|
|
90
112
|
initial: string;
|
|
113
|
+
history: false;
|
|
91
114
|
}
|
|
92
115
|
export interface LeafStateNode extends StateNode {
|
|
93
116
|
initial: never;
|
|
@@ -95,6 +118,10 @@ export interface LeafStateNode extends StateNode {
|
|
|
95
118
|
states: never;
|
|
96
119
|
parent: StateNode;
|
|
97
120
|
}
|
|
121
|
+
export interface HistoryStateNode extends StateNode {
|
|
122
|
+
history: 'shallow' | 'deep';
|
|
123
|
+
target: StateValue | undefined;
|
|
124
|
+
}
|
|
98
125
|
export interface Machine extends StateNode {
|
|
99
126
|
id: string;
|
|
100
127
|
initial: string | undefined;
|
|
@@ -116,15 +143,19 @@ export interface ActionMap {
|
|
|
116
143
|
actions: Action[];
|
|
117
144
|
onExit: Action[];
|
|
118
145
|
}
|
|
146
|
+
export interface EntryExitStates {
|
|
147
|
+
entry: Set<StateNode>;
|
|
148
|
+
exit: Set<StateNode>;
|
|
149
|
+
}
|
|
119
150
|
export interface ActivityMap {
|
|
120
151
|
[activityKey: string]: boolean;
|
|
121
152
|
}
|
|
122
153
|
export declare type MaybeStateValueActionsTuple = [StateValue | undefined, ActionMap, ActivityMap | undefined];
|
|
123
154
|
export interface StateTransition {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
155
|
+
value: StateValue | undefined;
|
|
156
|
+
entryExitStates: EntryExitStates | undefined;
|
|
157
|
+
actions: Action[];
|
|
158
|
+
paths: string[][];
|
|
128
159
|
}
|
|
129
160
|
export interface TransitionData {
|
|
130
161
|
value: StateValue | undefined;
|
package/es/utils.d.ts
CHANGED
|
@@ -12,10 +12,23 @@ export declare function mapValues<T, P>(collection: {
|
|
|
12
12
|
}) => P): {
|
|
13
13
|
[key: string]: P;
|
|
14
14
|
};
|
|
15
|
+
export declare function mapFilterValues<T, P>(collection: {
|
|
16
|
+
[key: string]: T;
|
|
17
|
+
}, iteratee: (item: T, key: string, collection: {
|
|
18
|
+
[key: string]: T;
|
|
19
|
+
}) => P, predicate: (item: T) => boolean): {
|
|
20
|
+
[key: string]: P;
|
|
21
|
+
};
|
|
15
22
|
/**
|
|
16
23
|
* Retrieves a value at the given path.
|
|
17
24
|
* @param props The deep path to the prop of the desired value
|
|
18
25
|
*/
|
|
19
|
-
export declare const path: (props: string[]) => any;
|
|
26
|
+
export declare const path: <T extends Record<string, any>>(props: string[]) => any;
|
|
27
|
+
/**
|
|
28
|
+
* Retrieves a value at the given path via the nested accessor prop.
|
|
29
|
+
* @param props The deep path to the prop of the desired value
|
|
30
|
+
*/
|
|
31
|
+
export declare function nestedPath<T extends Record<string, any>>(props: string[], accessorProp: keyof T): (object: T) => T;
|
|
20
32
|
export declare const toStatePaths: (stateValue: StateValue) => string[][];
|
|
21
33
|
export declare const pathsToStateValue: (paths: string[][]) => StateValue;
|
|
34
|
+
export declare const flatMap: <T>(array: T[][]) => T[];
|
package/es/utils.js
CHANGED
|
@@ -13,7 +13,9 @@ export function getActionType(action) {
|
|
|
13
13
|
try {
|
|
14
14
|
return typeof action === 'string' || typeof action === 'number'
|
|
15
15
|
? "" + action
|
|
16
|
-
: typeof action === 'function'
|
|
16
|
+
: typeof action === 'function'
|
|
17
|
+
? action.name
|
|
18
|
+
: action.type;
|
|
17
19
|
}
|
|
18
20
|
catch (e) {
|
|
19
21
|
throw new Error('Events must be strings or objects with a string event.type property.');
|
|
@@ -64,6 +66,17 @@ export function mapValues(collection, iteratee) {
|
|
|
64
66
|
});
|
|
65
67
|
return result;
|
|
66
68
|
}
|
|
69
|
+
export function mapFilterValues(collection, iteratee, predicate) {
|
|
70
|
+
var result = {};
|
|
71
|
+
Object.keys(collection).forEach(function (key) {
|
|
72
|
+
var item = collection[key];
|
|
73
|
+
if (!predicate(item)) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
result[key] = iteratee(item, key, collection);
|
|
77
|
+
});
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
67
80
|
/**
|
|
68
81
|
* Retrieves a value at the given path.
|
|
69
82
|
* @param props The deep path to the prop of the desired value
|
|
@@ -76,17 +89,29 @@ export var path = function (props) { return function (object) {
|
|
|
76
89
|
}
|
|
77
90
|
return result;
|
|
78
91
|
}; };
|
|
92
|
+
/**
|
|
93
|
+
* Retrieves a value at the given path via the nested accessor prop.
|
|
94
|
+
* @param props The deep path to the prop of the desired value
|
|
95
|
+
*/
|
|
96
|
+
export function nestedPath(props, accessorProp) {
|
|
97
|
+
return function (object) {
|
|
98
|
+
var result = object;
|
|
99
|
+
for (var _i = 0, props_2 = props; _i < props_2.length; _i++) {
|
|
100
|
+
var prop = props_2[_i];
|
|
101
|
+
result = result[accessorProp][prop];
|
|
102
|
+
}
|
|
103
|
+
return result;
|
|
104
|
+
};
|
|
105
|
+
}
|
|
79
106
|
export var toStatePaths = function (stateValue) {
|
|
80
107
|
if (typeof stateValue === 'string') {
|
|
81
108
|
return [[stateValue]];
|
|
82
109
|
}
|
|
83
|
-
var result = Object.keys(stateValue)
|
|
84
|
-
.map(function (key) {
|
|
110
|
+
var result = flatMap(Object.keys(stateValue).map(function (key) {
|
|
85
111
|
return toStatePaths(stateValue[key]).map(function (subPath) {
|
|
86
112
|
return [key].concat(subPath);
|
|
87
113
|
});
|
|
88
|
-
})
|
|
89
|
-
.reduce(function (a, b) { return a.concat(b); }, []);
|
|
114
|
+
}));
|
|
90
115
|
return result;
|
|
91
116
|
};
|
|
92
117
|
export var pathsToStateValue = function (paths) {
|
|
@@ -110,3 +135,6 @@ export var pathsToStateValue = function (paths) {
|
|
|
110
135
|
}
|
|
111
136
|
return result;
|
|
112
137
|
};
|
|
138
|
+
export var flatMap = function (array) {
|
|
139
|
+
return array.reduce(function (a, b) { return a.concat(b); }, []);
|
|
140
|
+
};
|
package/lib/Machine.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { StandardMachine, ParallelMachine, MachineConfig, ParallelMachineConfig } from './types';
|
|
2
|
-
export declare function Machine(config: MachineConfig | ParallelMachineConfig): StandardMachine | ParallelMachine;
|
|
1
|
+
import { StandardMachine, ParallelMachine, MachineConfig, ParallelMachineConfig, MachineOptions } from './types';
|
|
2
|
+
export declare function Machine(config: MachineConfig | ParallelMachineConfig, options?: MachineOptions): StandardMachine | ParallelMachine;
|
package/lib/Machine.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
var StateNode_1 = require("./StateNode");
|
|
4
|
-
function Machine(config) {
|
|
5
|
-
return new StateNode_1.StateNode(config);
|
|
4
|
+
function Machine(config, options) {
|
|
5
|
+
return new StateNode_1.StateNode(config, options);
|
|
6
6
|
}
|
|
7
7
|
exports.Machine = Machine;
|
package/lib/State.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { StateValue, ActivityMap, EventObject, Action, StateInterface } from './types';
|
|
1
|
+
import { StateValue, ActivityMap, EventObject, Action, StateInterface, HistoryValue } from './types';
|
|
2
2
|
export declare class State implements StateInterface {
|
|
3
3
|
value: StateValue;
|
|
4
|
-
|
|
4
|
+
historyValue?: HistoryValue | undefined;
|
|
5
|
+
history?: State | undefined;
|
|
5
6
|
actions: Action[];
|
|
6
7
|
activities: ActivityMap;
|
|
7
8
|
data: Record<string, any>;
|
|
@@ -11,10 +12,10 @@ export declare class State implements StateInterface {
|
|
|
11
12
|
events: EventObject[];
|
|
12
13
|
static from(stateValue: State | StateValue): State;
|
|
13
14
|
static inert(stateValue: State | StateValue): State;
|
|
14
|
-
constructor(value: StateValue, history?: State | undefined, actions?: Action[], activities?: ActivityMap, data?: Record<string, any>,
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
constructor(value: StateValue, historyValue?: HistoryValue | undefined, history?: State | undefined, actions?: Action[], activities?: ActivityMap, data?: Record<string, any>,
|
|
16
|
+
/**
|
|
17
|
+
* Internal event queue
|
|
18
|
+
*/
|
|
19
|
+
events?: EventObject[]);
|
|
19
20
|
toString(): string | undefined;
|
|
20
21
|
}
|
package/lib/State.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
var constants_1 = require("./constants");
|
|
4
4
|
var State = /** @class */ (function () {
|
|
5
|
-
function State(value, history, actions, activities, data,
|
|
5
|
+
function State(value, historyValue, history, actions, activities, data,
|
|
6
6
|
/**
|
|
7
7
|
* Internal event queue
|
|
8
8
|
*/
|
|
@@ -12,6 +12,7 @@ var State = /** @class */ (function () {
|
|
|
12
12
|
if (data === void 0) { data = {}; }
|
|
13
13
|
if (events === void 0) { events = []; }
|
|
14
14
|
this.value = value;
|
|
15
|
+
this.historyValue = historyValue;
|
|
15
16
|
this.history = history;
|
|
16
17
|
this.actions = actions;
|
|
17
18
|
this.activities = activities;
|
|
@@ -29,7 +30,7 @@ var State = /** @class */ (function () {
|
|
|
29
30
|
if (!stateValue.actions.length) {
|
|
30
31
|
return stateValue;
|
|
31
32
|
}
|
|
32
|
-
return new State(stateValue.value, stateValue.history, []);
|
|
33
|
+
return new State(stateValue.value, stateValue.historyValue, stateValue.history, [], stateValue.activities);
|
|
33
34
|
}
|
|
34
35
|
return State.from(stateValue);
|
|
35
36
|
};
|
package/lib/StateNode.d.ts
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
import { Event, StateValue, Action, StandardMachine, ParallelMachine, SimpleOrCompoundStateNodeConfig,
|
|
1
|
+
import { Event, StateValue, Action, StandardMachine, ParallelMachine, SimpleOrCompoundStateNodeConfig, ParallelMachineConfig, EventType, StandardMachineConfig, Activity, ConditionalTransitionConfig, StateTransition, MachineOptions, HistoryValue } from './types';
|
|
2
2
|
import { State } from './State';
|
|
3
|
-
declare class StateNode
|
|
3
|
+
declare class StateNode {
|
|
4
4
|
config: SimpleOrCompoundStateNodeConfig | StandardMachineConfig | ParallelMachineConfig;
|
|
5
|
+
options: MachineOptions;
|
|
5
6
|
key: string;
|
|
6
7
|
id: string;
|
|
7
8
|
path: string[];
|
|
8
9
|
initial?: string;
|
|
9
10
|
parallel?: boolean;
|
|
11
|
+
transient: boolean;
|
|
10
12
|
states: Record<string, StateNode>;
|
|
13
|
+
history: false | 'shallow' | 'deep';
|
|
11
14
|
on: Record<string, ConditionalTransitionConfig>;
|
|
12
|
-
onEntry
|
|
13
|
-
onExit
|
|
15
|
+
onEntry: Action[];
|
|
16
|
+
onExit: Action[];
|
|
14
17
|
activities?: Activity[];
|
|
15
18
|
strict: boolean;
|
|
16
19
|
parent?: StateNode;
|
|
@@ -19,24 +22,58 @@ declare class StateNode implements StateNodeConfig {
|
|
|
19
22
|
delimiter: string;
|
|
20
23
|
private __cache;
|
|
21
24
|
private idMap;
|
|
22
|
-
constructor(config: SimpleOrCompoundStateNodeConfig | StandardMachineConfig | ParallelMachineConfig);
|
|
25
|
+
constructor(config: SimpleOrCompoundStateNodeConfig | StandardMachineConfig | ParallelMachineConfig, options?: MachineOptions);
|
|
23
26
|
getStateNodes(state: StateValue | State): StateNode[];
|
|
24
27
|
handles(event: Event): boolean;
|
|
28
|
+
private _transitionLeafNode;
|
|
29
|
+
private _transitionHierarchicalNode;
|
|
30
|
+
private _transitionOrthogonalNode;
|
|
31
|
+
_transition(stateValue: StateValue, state: State, event: Event, extendedState?: any): StateTransition;
|
|
32
|
+
private _next;
|
|
33
|
+
private _getEntryExitStates;
|
|
34
|
+
private _evaluateCond;
|
|
35
|
+
private _getActions;
|
|
36
|
+
private _getActivities;
|
|
25
37
|
transition(state: StateValue | State, event: Event, extendedState?: any): State;
|
|
26
|
-
private
|
|
38
|
+
private ensureValidPaths;
|
|
27
39
|
getStateNode(stateKey: string): StateNode;
|
|
28
40
|
getStateNodeById(stateId: string): StateNode;
|
|
29
|
-
|
|
30
|
-
private
|
|
31
|
-
private next(event, fullState, history?, extendedState?);
|
|
41
|
+
getStateNodeByPath(statePath: string | string[]): StateNode;
|
|
42
|
+
private resolve;
|
|
32
43
|
private readonly resolvedStateValue;
|
|
33
|
-
private getResolvedPath
|
|
44
|
+
private getResolvedPath;
|
|
34
45
|
private readonly initialStateValue;
|
|
35
46
|
readonly initialState: State;
|
|
47
|
+
readonly target: StateValue | undefined;
|
|
36
48
|
getStates(stateValue: StateValue): StateNode[];
|
|
37
|
-
|
|
49
|
+
/**
|
|
50
|
+
* Returns the leaf nodes from a state path relative to this state node.
|
|
51
|
+
*
|
|
52
|
+
* @param relativeStateId The relative state path to retrieve the state nodes
|
|
53
|
+
* @param history The previous state to retrieve history
|
|
54
|
+
* @param resolve Whether state nodes should resolve to initial child state nodes
|
|
55
|
+
*/
|
|
56
|
+
getRelativeStateNodes(relativeStateId: string | string[], historyValue?: HistoryValue, resolve?: boolean): StateNode[];
|
|
57
|
+
readonly initialStateNodes: StateNode[];
|
|
58
|
+
/**
|
|
59
|
+
* Retrieves state nodes from a relative path to this state node.
|
|
60
|
+
*
|
|
61
|
+
* @param relativePath The relative path from this state node
|
|
62
|
+
* @param historyValue
|
|
63
|
+
*/
|
|
64
|
+
getFromRelativePath(relativePath: string[], historyValue?: HistoryValue): StateNode[];
|
|
65
|
+
static updateHistoryValue(hist: HistoryValue, stateValue: StateValue): HistoryValue;
|
|
66
|
+
historyValue(relativeStateValue?: StateValue | undefined): HistoryValue | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* Resolves to the historical value(s) of the parent state node,
|
|
69
|
+
* represented by state nodes.
|
|
70
|
+
*
|
|
71
|
+
* @param historyValue
|
|
72
|
+
*/
|
|
73
|
+
private resolveHistory;
|
|
38
74
|
readonly events: EventType[];
|
|
39
|
-
private
|
|
75
|
+
private formatTransition;
|
|
76
|
+
private formatTransitions;
|
|
40
77
|
}
|
|
41
|
-
export declare function Machine(config:
|
|
78
|
+
export declare function Machine<T extends StandardMachineConfig | ParallelMachineConfig>(config: T, options?: MachineOptions): T extends ParallelMachineConfig ? ParallelMachine : T extends StandardMachineConfig ? StandardMachine : never;
|
|
42
79
|
export { StateNode };
|