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/lib/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/lib/graph.js
CHANGED
|
@@ -15,19 +15,27 @@ function getNodes(node) {
|
|
|
15
15
|
exports.getNodes = getNodes;
|
|
16
16
|
function getEventEdges(node, event) {
|
|
17
17
|
var transitions = node.on[event];
|
|
18
|
-
return transitions.map(function (transition) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
18
|
+
return utils_1.flatMap(transitions.map(function (transition) {
|
|
19
|
+
var targets = [].concat(transition.target);
|
|
20
|
+
return targets.map(function (target) {
|
|
21
|
+
var targetNode = node.getRelativeStateNodes(target, undefined, false)[0];
|
|
22
|
+
return {
|
|
23
|
+
source: node,
|
|
24
|
+
target: targetNode,
|
|
25
|
+
event: event,
|
|
26
|
+
actions: transition.actions
|
|
27
|
+
? transition.actions.map(utils_1.getActionType)
|
|
28
|
+
: [],
|
|
29
|
+
cond: transition.cond
|
|
30
|
+
};
|
|
31
|
+
});
|
|
32
|
+
}));
|
|
27
33
|
}
|
|
28
|
-
|
|
34
|
+
exports.getEventEdges = getEventEdges;
|
|
35
|
+
function getEdges(node, options) {
|
|
36
|
+
var _a = (options || {}).deep, deep = _a === void 0 ? true : _a;
|
|
29
37
|
var edges = [];
|
|
30
|
-
if (node.states) {
|
|
38
|
+
if (node.states && deep) {
|
|
31
39
|
Object.keys(node.states).forEach(function (stateKey) {
|
|
32
40
|
edges.push.apply(edges, getEdges(node.states[stateKey]));
|
|
33
41
|
});
|
|
@@ -38,7 +46,7 @@ function getEdges(node) {
|
|
|
38
46
|
return edges;
|
|
39
47
|
}
|
|
40
48
|
exports.getEdges = getEdges;
|
|
41
|
-
function getAdjacencyMap(node) {
|
|
49
|
+
function getAdjacencyMap(node, extendedState) {
|
|
42
50
|
var adjacency = {};
|
|
43
51
|
var events = node.events;
|
|
44
52
|
function findAdjacencies(stateValue) {
|
|
@@ -49,7 +57,7 @@ function getAdjacencyMap(node) {
|
|
|
49
57
|
adjacency[stateKey] = {};
|
|
50
58
|
for (var _i = 0, events_1 = events; _i < events_1.length; _i++) {
|
|
51
59
|
var event_1 = events_1[_i];
|
|
52
|
-
var nextState = node.transition(stateValue, event_1);
|
|
60
|
+
var nextState = node.transition(stateValue, event_1, extendedState);
|
|
53
61
|
adjacency[stateKey][event_1] = { state: nextState.value };
|
|
54
62
|
findAdjacencies(nextState.value);
|
|
55
63
|
}
|
|
@@ -58,11 +66,12 @@ function getAdjacencyMap(node) {
|
|
|
58
66
|
return adjacency;
|
|
59
67
|
}
|
|
60
68
|
exports.getAdjacencyMap = getAdjacencyMap;
|
|
61
|
-
function getShortestPaths(machine) {
|
|
69
|
+
function getShortestPaths(machine, extendedState) {
|
|
70
|
+
var _a;
|
|
62
71
|
if (!machine.states) {
|
|
63
72
|
return EMPTY_MAP;
|
|
64
73
|
}
|
|
65
|
-
var adjacency = getAdjacencyMap(machine);
|
|
74
|
+
var adjacency = getAdjacencyMap(machine, extendedState);
|
|
66
75
|
var initialStateId = JSON.stringify(machine.initialState.value);
|
|
67
76
|
var pathMap = (_a = {},
|
|
68
77
|
_a[initialStateId] = [],
|
|
@@ -102,22 +111,21 @@ function getShortestPaths(machine) {
|
|
|
102
111
|
}
|
|
103
112
|
util(machine.initialState.value);
|
|
104
113
|
return pathMap;
|
|
105
|
-
var _a;
|
|
106
114
|
}
|
|
107
115
|
exports.getShortestPaths = getShortestPaths;
|
|
108
|
-
function getShortestPathsAsArray(machine) {
|
|
109
|
-
var result = getShortestPaths(machine);
|
|
116
|
+
function getShortestPathsAsArray(machine, extendedState) {
|
|
117
|
+
var result = getShortestPaths(machine, extendedState);
|
|
110
118
|
return Object.keys(result).map(function (key) { return ({
|
|
111
119
|
state: JSON.parse(key),
|
|
112
120
|
path: result[key]
|
|
113
121
|
}); });
|
|
114
122
|
}
|
|
115
123
|
exports.getShortestPathsAsArray = getShortestPathsAsArray;
|
|
116
|
-
function getSimplePaths(machine) {
|
|
124
|
+
function getSimplePaths(machine, extendedState) {
|
|
117
125
|
if (!machine.states) {
|
|
118
126
|
return EMPTY_MAP;
|
|
119
127
|
}
|
|
120
|
-
var adjacency = getAdjacencyMap(machine);
|
|
128
|
+
var adjacency = getAdjacencyMap(machine, extendedState);
|
|
121
129
|
var visited = new Set();
|
|
122
130
|
var path = [];
|
|
123
131
|
var paths = {};
|
|
@@ -151,8 +159,8 @@ function getSimplePaths(machine) {
|
|
|
151
159
|
return paths;
|
|
152
160
|
}
|
|
153
161
|
exports.getSimplePaths = getSimplePaths;
|
|
154
|
-
function getSimplePathsAsArray(machine) {
|
|
155
|
-
var result = getSimplePaths(machine);
|
|
162
|
+
function getSimplePathsAsArray(machine, extendedState) {
|
|
163
|
+
var result = getSimplePaths(machine, extendedState);
|
|
156
164
|
return Object.keys(result).map(function (key) { return ({
|
|
157
165
|
state: JSON.parse(key),
|
|
158
166
|
paths: result[key]
|
package/lib/patterns.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
function toggle(onState, offState, eventType) {
|
|
4
|
+
var _a, _b, _c;
|
|
4
5
|
return _a = {},
|
|
5
6
|
_a[onState] = {
|
|
6
7
|
on: (_b = {}, _b[eventType] = offState, _b)
|
|
@@ -9,6 +10,5 @@ function toggle(onState, offState, eventType) {
|
|
|
9
10
|
on: (_c = {}, _c[eventType] = onState, _c)
|
|
10
11
|
},
|
|
11
12
|
_a;
|
|
12
|
-
var _a, _b, _c;
|
|
13
13
|
}
|
|
14
14
|
exports.toggle = toggle;
|
package/lib/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/lib/scxml.js
CHANGED
|
@@ -75,10 +75,9 @@ function stateNodeToSCXML(stateNode) {
|
|
|
75
75
|
return {
|
|
76
76
|
type: 'element',
|
|
77
77
|
name: 'transition',
|
|
78
|
-
attributes: __assign({}, event ? { event: event } : undefined, { target: stateNode.parent.
|
|
79
|
-
.id }, targetTransition.cond
|
|
78
|
+
attributes: __assign({}, (event ? { event: event } : undefined), { target: stateNode.parent.getRelativeStateNodes(targetTransition.target)[0].id }, (targetTransition.cond
|
|
80
79
|
? { cond: targetTransition.cond.toString() }
|
|
81
|
-
: undefined),
|
|
80
|
+
: undefined)),
|
|
82
81
|
elements: targetTransition.actions
|
|
83
82
|
? targetTransition.actions.map(function (action) { return ({
|
|
84
83
|
type: 'element',
|
|
@@ -96,9 +95,10 @@ function stateNodeToSCXML(stateNode) {
|
|
|
96
95
|
return {
|
|
97
96
|
type: 'element',
|
|
98
97
|
name: 'transition',
|
|
99
|
-
attributes: __assign({}, event ? { event: event } : undefined, { target: stateNode.parent.
|
|
98
|
+
attributes: __assign({}, (event ? { event: event } : undefined), { target: stateNode.parent.getRelativeStateNodes(target)[0]
|
|
99
|
+
.id }, (targetTransition.cond
|
|
100
100
|
? { cond: targetTransition.cond.toString() }
|
|
101
|
-
: undefined),
|
|
101
|
+
: undefined)),
|
|
102
102
|
elements: targetTransition.actions
|
|
103
103
|
? targetTransition.actions.map(function (action) { return ({
|
|
104
104
|
type: 'element',
|
|
@@ -172,8 +172,31 @@ function toConfig(nodeJson, id, options) {
|
|
|
172
172
|
var initial = parallel ? undefined : nodeJson.attributes.initial;
|
|
173
173
|
var states;
|
|
174
174
|
var on;
|
|
175
|
+
var elements = nodeJson.elements;
|
|
176
|
+
switch (nodeJson.name) {
|
|
177
|
+
case 'history': {
|
|
178
|
+
if (!elements) {
|
|
179
|
+
return {
|
|
180
|
+
id: id,
|
|
181
|
+
history: nodeJson.attributes.type || 'shallow'
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
var transitionElement = elements.filter(function (element) { return element.name === 'transition'; })[0];
|
|
185
|
+
return {
|
|
186
|
+
id: id,
|
|
187
|
+
history: nodeJson.attributes.type || 'shallow',
|
|
188
|
+
target: "#" + transitionElement.attributes.target
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
default:
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
175
194
|
if (nodeJson.elements) {
|
|
176
|
-
var stateElements = nodeJson.elements.filter(function (element) {
|
|
195
|
+
var stateElements = nodeJson.elements.filter(function (element) {
|
|
196
|
+
return element.name === 'state' ||
|
|
197
|
+
element.name === 'parallel' ||
|
|
198
|
+
element.name === 'history';
|
|
199
|
+
});
|
|
177
200
|
var transitionElements = nodeJson.elements.filter(function (element) { return element.name === 'transition'; });
|
|
178
201
|
var onEntryElement = nodeJson.elements.find(function (element) { return element.name === 'onentry'; });
|
|
179
202
|
var onExitElement = nodeJson.elements.find(function (element) { return element.name === 'onexit'; });
|
|
@@ -185,11 +208,11 @@ function toConfig(nodeJson, id, options) {
|
|
|
185
208
|
}
|
|
186
209
|
states = indexedRecord(stateElements, function (item) { return "" + item.attributes.id; });
|
|
187
210
|
on = utils_1.mapValues(indexedAggregateRecord(transitionElements, function (item) { return item.attributes.event || ''; }), function (values) {
|
|
188
|
-
return values.map(function (value) { return (__assign({ target: "#" + value.attributes.target }, value.elements ? executableContent(value.elements) : undefined, value.attributes.cond
|
|
211
|
+
return values.map(function (value) { return (__assign({ target: "#" + value.attributes.target }, (value.elements ? executableContent(value.elements) : undefined), (value.attributes.cond
|
|
189
212
|
? {
|
|
190
213
|
cond: evalCond(value.attributes.cond)
|
|
191
214
|
}
|
|
192
|
-
: undefined)); });
|
|
215
|
+
: undefined))); });
|
|
193
216
|
});
|
|
194
217
|
var onEntry = onEntryElement
|
|
195
218
|
? onEntryElement.elements.map(function (element) {
|
|
@@ -211,13 +234,13 @@ function toConfig(nodeJson, id, options) {
|
|
|
211
234
|
}
|
|
212
235
|
})
|
|
213
236
|
: undefined;
|
|
214
|
-
return __assign({ id: id, delimiter: options.delimiter }, initial ? { initial: initial } : undefined, parallel ? { parallel: parallel } : undefined, stateElements.length
|
|
237
|
+
return __assign({ id: id, delimiter: options.delimiter }, (initial ? { initial: initial } : undefined), (parallel ? { parallel: parallel } : undefined), (stateElements.length
|
|
215
238
|
? {
|
|
216
239
|
states: utils_1.mapValues(states, function (state, key) {
|
|
217
240
|
return toConfig(state, key, options);
|
|
218
241
|
})
|
|
219
242
|
}
|
|
220
|
-
: undefined, transitionElements.length ? { on: on } : undefined, onEntry ? { onEntry: onEntry } : undefined, onExit ? { onExit: onExit } : undefined);
|
|
243
|
+
: undefined), (transitionElements.length ? { on: on } : undefined), (onEntry ? { onEntry: onEntry } : undefined), (onExit ? { onExit: onExit } : undefined));
|
|
221
244
|
}
|
|
222
245
|
return { id: id };
|
|
223
246
|
}
|
package/lib/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/lib/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/lib/utils.js
CHANGED
|
@@ -16,7 +16,9 @@ function getActionType(action) {
|
|
|
16
16
|
try {
|
|
17
17
|
return typeof action === 'string' || typeof action === 'number'
|
|
18
18
|
? "" + action
|
|
19
|
-
: typeof action === 'function'
|
|
19
|
+
: typeof action === 'function'
|
|
20
|
+
? action.name
|
|
21
|
+
: action.type;
|
|
20
22
|
}
|
|
21
23
|
catch (e) {
|
|
22
24
|
throw new Error('Events must be strings or objects with a string event.type property.');
|
|
@@ -72,6 +74,18 @@ function mapValues(collection, iteratee) {
|
|
|
72
74
|
return result;
|
|
73
75
|
}
|
|
74
76
|
exports.mapValues = mapValues;
|
|
77
|
+
function mapFilterValues(collection, iteratee, predicate) {
|
|
78
|
+
var result = {};
|
|
79
|
+
Object.keys(collection).forEach(function (key) {
|
|
80
|
+
var item = collection[key];
|
|
81
|
+
if (!predicate(item)) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
result[key] = iteratee(item, key, collection);
|
|
85
|
+
});
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
exports.mapFilterValues = mapFilterValues;
|
|
75
89
|
/**
|
|
76
90
|
* Retrieves a value at the given path.
|
|
77
91
|
* @param props The deep path to the prop of the desired value
|
|
@@ -84,17 +98,30 @@ exports.path = function (props) { return function (object) {
|
|
|
84
98
|
}
|
|
85
99
|
return result;
|
|
86
100
|
}; };
|
|
101
|
+
/**
|
|
102
|
+
* Retrieves a value at the given path via the nested accessor prop.
|
|
103
|
+
* @param props The deep path to the prop of the desired value
|
|
104
|
+
*/
|
|
105
|
+
function nestedPath(props, accessorProp) {
|
|
106
|
+
return function (object) {
|
|
107
|
+
var result = object;
|
|
108
|
+
for (var _i = 0, props_2 = props; _i < props_2.length; _i++) {
|
|
109
|
+
var prop = props_2[_i];
|
|
110
|
+
result = result[accessorProp][prop];
|
|
111
|
+
}
|
|
112
|
+
return result;
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
exports.nestedPath = nestedPath;
|
|
87
116
|
exports.toStatePaths = function (stateValue) {
|
|
88
117
|
if (typeof stateValue === 'string') {
|
|
89
118
|
return [[stateValue]];
|
|
90
119
|
}
|
|
91
|
-
var result = Object.keys(stateValue)
|
|
92
|
-
.map(function (key) {
|
|
120
|
+
var result = exports.flatMap(Object.keys(stateValue).map(function (key) {
|
|
93
121
|
return exports.toStatePaths(stateValue[key]).map(function (subPath) {
|
|
94
122
|
return [key].concat(subPath);
|
|
95
123
|
});
|
|
96
|
-
})
|
|
97
|
-
.reduce(function (a, b) { return a.concat(b); }, []);
|
|
124
|
+
}));
|
|
98
125
|
return result;
|
|
99
126
|
};
|
|
100
127
|
exports.pathsToStateValue = function (paths) {
|
|
@@ -118,3 +145,6 @@ exports.pathsToStateValue = function (paths) {
|
|
|
118
145
|
}
|
|
119
146
|
return result;
|
|
120
147
|
};
|
|
148
|
+
exports.flatMap = function (array) {
|
|
149
|
+
return array.reduce(function (a, b) { return a.concat(b); }, []);
|
|
150
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xstate",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.3",
|
|
4
4
|
"description": "Simple JavaScript Finite State Machines and Statecharts",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "es/index.js",
|
|
@@ -36,11 +36,11 @@
|
|
|
36
36
|
"@types/node": "^8.9.4",
|
|
37
37
|
"chai": "^4.1.2",
|
|
38
38
|
"mocha": "^3.5.3",
|
|
39
|
-
"prettier": "^1.
|
|
39
|
+
"prettier": "^1.13.5",
|
|
40
40
|
"scxml-test-framework": "^1.0.2",
|
|
41
41
|
"ts-node": "^3.3.0",
|
|
42
42
|
"tslint": "^5.7.0",
|
|
43
|
-
"typescript": "^2.
|
|
43
|
+
"typescript": "^2.9.2",
|
|
44
44
|
"webpack": "^3.5.6",
|
|
45
45
|
"xml-js": "^1.6.2"
|
|
46
46
|
},
|
package/src/Machine.ts
CHANGED
|
@@ -3,12 +3,14 @@ import {
|
|
|
3
3
|
StandardMachine,
|
|
4
4
|
ParallelMachine,
|
|
5
5
|
MachineConfig,
|
|
6
|
-
ParallelMachineConfig
|
|
6
|
+
ParallelMachineConfig,
|
|
7
|
+
MachineOptions
|
|
7
8
|
} from './types';
|
|
8
9
|
import { StateNode } from './StateNode';
|
|
9
10
|
|
|
10
11
|
export function Machine(
|
|
11
|
-
config: MachineConfig | ParallelMachineConfig
|
|
12
|
+
config: MachineConfig | ParallelMachineConfig,
|
|
13
|
+
options?: MachineOptions
|
|
12
14
|
): StandardMachine | ParallelMachine {
|
|
13
|
-
return new StateNode(config) as StandardMachine | ParallelMachine;
|
|
15
|
+
return new StateNode(config, options) as StandardMachine | ParallelMachine;
|
|
14
16
|
}
|
package/src/State.ts
CHANGED
|
@@ -3,7 +3,8 @@ import {
|
|
|
3
3
|
ActivityMap,
|
|
4
4
|
EventObject,
|
|
5
5
|
Action,
|
|
6
|
-
StateInterface
|
|
6
|
+
StateInterface,
|
|
7
|
+
HistoryValue
|
|
7
8
|
} from './types';
|
|
8
9
|
import { STATE_DELIMITER, EMPTY_ACTIVITY_MAP } from './constants';
|
|
9
10
|
|
|
@@ -20,7 +21,13 @@ export class State implements StateInterface {
|
|
|
20
21
|
if (!stateValue.actions.length) {
|
|
21
22
|
return stateValue;
|
|
22
23
|
}
|
|
23
|
-
return new State(
|
|
24
|
+
return new State(
|
|
25
|
+
stateValue.value,
|
|
26
|
+
stateValue.historyValue,
|
|
27
|
+
stateValue.history,
|
|
28
|
+
[],
|
|
29
|
+
stateValue.activities
|
|
30
|
+
);
|
|
24
31
|
}
|
|
25
32
|
|
|
26
33
|
return State.from(stateValue);
|
|
@@ -28,6 +35,7 @@ export class State implements StateInterface {
|
|
|
28
35
|
|
|
29
36
|
constructor(
|
|
30
37
|
public value: StateValue,
|
|
38
|
+
public historyValue?: HistoryValue | undefined,
|
|
31
39
|
public history?: State,
|
|
32
40
|
public actions: Action[] = [],
|
|
33
41
|
public activities: ActivityMap = EMPTY_ACTIVITY_MAP,
|