xstate 6.0.0-alpha.7 → 6.0.0-alpha.71
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 +206 -67
- package/bin/agent.js +347 -0
- package/bin/xstate.js +52 -0
- package/dist/createActor-2136a8ad.development.cjs.js +7110 -0
- package/dist/createActor-22a0534b.esm.js +6783 -0
- package/dist/createActor-286a0dbb.development.esm.js +6999 -0
- package/dist/createActor-f4978450.cjs.js +6893 -0
- package/dist/declarations/src/SimulatedClock.d.ts +2 -1
- package/dist/declarations/src/State.d.ts +66 -5
- package/dist/declarations/src/StateMachine.d.ts +135 -30
- package/dist/declarations/src/StateNode.d.ts +21 -16
- package/dist/declarations/src/actions.d.ts +11 -6
- package/dist/declarations/src/actors/callback.d.ts +11 -2
- package/dist/declarations/src/actors/index.d.ts +6 -5
- package/dist/declarations/src/actors/listener.d.ts +8 -3
- package/dist/declarations/src/actors/logic.d.ts +42 -52
- package/dist/declarations/src/actors/observable.d.ts +15 -2
- package/dist/declarations/src/actors/promise.d.ts +90 -22
- package/dist/declarations/src/actors/subscription.d.ts +9 -3
- package/dist/declarations/src/assert.d.ts +1 -0
- package/dist/declarations/src/base.types.d.ts +102 -0
- package/dist/declarations/src/createActor.d.ts +104 -27
- package/dist/declarations/src/createMachine.d.ts +37 -16
- package/dist/declarations/src/delay.d.ts +8 -0
- package/dist/declarations/src/durable/index.d.ts +222 -0
- package/dist/declarations/src/effectDescriptor.d.ts +95 -0
- package/dist/declarations/src/eventTypes.d.ts +9 -0
- package/dist/declarations/src/fsm/index.d.ts +2 -0
- package/dist/declarations/src/fsm.d.ts +169 -0
- package/dist/declarations/src/graph/adjacency.d.ts +6 -3
- package/dist/declarations/src/graph/graph.d.ts +6 -3
- package/dist/declarations/src/graph/index.d.ts +2 -3
- package/dist/declarations/src/graph/pathFromEvents.d.ts +5 -2
- package/dist/declarations/src/graph/shortestPaths.d.ts +5 -3
- package/dist/declarations/src/graph/simplePaths.d.ts +5 -3
- package/dist/declarations/src/graph/types.d.ts +113 -59
- package/dist/declarations/src/hotSwap.d.ts +18 -0
- package/dist/declarations/src/index.d.ts +27 -19
- package/dist/declarations/src/inspection.d.ts +24 -1
- package/dist/declarations/src/machineFromJSON.d.ts +171 -0
- package/dist/declarations/src/machineVersion.types.d.ts +53 -0
- package/dist/declarations/src/machineVersions.d.ts +105 -0
- package/dist/declarations/src/mapState.d.ts +2 -0
- package/dist/declarations/src/runtimeHelpers.d.ts +43 -0
- package/dist/declarations/src/schema.types.d.ts +31 -4
- package/dist/declarations/src/serialize.d.ts +39 -27
- package/dist/declarations/src/setup.d.ts +1091 -146
- package/dist/declarations/src/spawn.d.ts +6 -4
- package/dist/declarations/src/stateUtils.d.ts +57 -14
- package/dist/declarations/src/system.d.ts +178 -17
- package/dist/declarations/src/systemTransition.d.ts +108 -0
- package/dist/declarations/src/toPromise.d.ts +1 -0
- package/dist/declarations/src/transition.d.ts +48 -10
- package/dist/declarations/src/transitionActions.d.ts +22 -4
- package/dist/declarations/src/typeUtils.d.ts +52 -0
- package/dist/declarations/src/types.d.ts +842 -297
- package/dist/declarations/src/types.v6.d.ts +543 -101
- package/dist/declarations/src/utils.d.ts +39 -8
- package/dist/declarations/src/validation/index.d.ts +49 -0
- package/dist/declarations/src/validation.types.d.ts +29 -0
- package/dist/declarations/src/waitFor.d.ts +1 -0
- package/dist/effectDescriptor-2ae7e5e3.esm.js +343 -0
- package/dist/effectDescriptor-86f04325.development.esm.js +343 -0
- package/dist/effectDescriptor-a6247f92.cjs.js +351 -0
- package/dist/effectDescriptor-e41e1627.development.cjs.js +351 -0
- package/dist/index-31b3c878.cjs.js +1078 -0
- package/dist/index-5ce9cbf9.development.cjs.js +1078 -0
- package/dist/index-6923de25.development.esm.js +1069 -0
- package/dist/index-74b77771.esm.js +1069 -0
- package/dist/schema.types-0ecd17b8.esm.js +71 -0
- package/dist/schema.types-15a68ddc.cjs.js +74 -0
- package/dist/schema.types-1c16f035.development.cjs.js +74 -0
- package/dist/schema.types-1cd4652e.development.esm.js +71 -0
- package/dist/xstate-actors.cjs.js +5 -6
- package/dist/xstate-actors.cjs.mjs +2 -4
- package/dist/xstate-actors.development.cjs.js +5 -6
- package/dist/xstate-actors.development.cjs.mjs +2 -4
- package/dist/xstate-actors.development.esm.js +2 -1
- package/dist/xstate-actors.esm.js +2 -1
- package/dist/xstate-actors.umd.min.js +1 -1
- package/dist/xstate-actors.umd.min.js.map +1 -1
- package/dist/xstate-durable.cjs.d.mts +2 -0
- package/dist/xstate-durable.cjs.d.ts +2 -0
- package/dist/xstate-durable.cjs.js +513 -0
- package/dist/xstate-durable.cjs.mjs +5 -0
- package/dist/xstate-durable.development.cjs.js +513 -0
- package/dist/xstate-durable.development.cjs.mjs +5 -0
- package/dist/xstate-durable.development.esm.js +507 -0
- package/dist/xstate-durable.esm.js +507 -0
- package/dist/xstate-durable.umd.min.js +2 -0
- package/dist/xstate-durable.umd.min.js.map +1 -0
- package/dist/xstate-fsm.cjs.d.mts +2 -0
- package/dist/xstate-fsm.cjs.d.ts +2 -0
- package/dist/xstate-fsm.cjs.js +111 -0
- package/dist/xstate-fsm.cjs.mjs +5 -0
- package/dist/xstate-fsm.development.cjs.js +111 -0
- package/dist/xstate-fsm.development.cjs.mjs +5 -0
- package/dist/xstate-fsm.development.esm.js +105 -0
- package/dist/xstate-fsm.esm.js +105 -0
- package/dist/xstate-fsm.umd.min.js +2 -0
- package/dist/xstate-fsm.umd.min.js.map +1 -0
- package/dist/xstate-graph.cjs.js +172 -457
- package/dist/xstate-graph.cjs.mjs +2 -5
- package/dist/xstate-graph.development.cjs.js +172 -457
- package/dist/xstate-graph.development.cjs.mjs +2 -5
- package/dist/xstate-graph.development.esm.js +170 -451
- package/dist/xstate-graph.esm.js +170 -451
- package/dist/xstate-graph.umd.min.js +1 -1
- package/dist/xstate-graph.umd.min.js.map +1 -1
- package/dist/xstate-validation.cjs.d.mts +2 -0
- package/dist/xstate-validation.cjs.d.ts +2 -0
- package/dist/xstate-validation.cjs.js +330 -0
- package/dist/xstate-validation.cjs.mjs +5 -0
- package/dist/xstate-validation.development.cjs.js +330 -0
- package/dist/xstate-validation.development.cjs.mjs +5 -0
- package/dist/xstate-validation.development.esm.js +324 -0
- package/dist/xstate-validation.esm.js +324 -0
- package/dist/xstate-validation.umd.min.js +2 -0
- package/dist/xstate-validation.umd.min.js.map +1 -0
- package/dist/xstate.cjs.js +4099 -1534
- package/dist/xstate.cjs.mjs +22 -13
- package/dist/xstate.development.cjs.js +4251 -1479
- package/dist/xstate.development.cjs.mjs +22 -13
- package/dist/xstate.development.esm.js +4212 -1449
- package/dist/xstate.esm.js +3996 -1440
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/docs/actions.md +75 -0
- package/docs/actor-logic.md +250 -0
- package/docs/actors.md +64 -0
- package/docs/agents.md +132 -0
- package/docs/api-stability.json +3672 -0
- package/docs/async-logic.md +75 -0
- package/docs/async-requests.md +157 -0
- package/docs/backend-workflows.md +121 -0
- package/docs/cancellation.md +42 -0
- package/docs/cheatsheet.md +561 -0
- package/docs/choice-states.md +109 -0
- package/docs/choose-xstate.md +47 -0
- package/docs/configuration.md +161 -0
- package/docs/context.md +67 -0
- package/docs/create-actor.md +157 -0
- package/docs/data-and-effects.md +48 -0
- package/docs/delays.md +81 -0
- package/docs/durable-execution.md +510 -0
- package/docs/emitted-events.md +146 -0
- package/docs/examples.md +75 -0
- package/docs/faq.md +60 -0
- package/docs/final-states.md +149 -0
- package/docs/forms-and-wizards.md +166 -0
- package/docs/frameworks.md +69 -0
- package/docs/fsm.md +164 -0
- package/docs/glossary.md +68 -0
- package/docs/guards.md +137 -0
- package/docs/history-states.md +159 -0
- package/docs/how-xstate-runs-machines.md +84 -0
- package/docs/index.md +73 -0
- package/docs/input-output.md +69 -0
- package/docs/inspect-actor-systems.md +116 -0
- package/docs/inspection.md +73 -0
- package/docs/internal-events.md +156 -0
- package/docs/invoke.md +354 -0
- package/docs/lifecycle-and-errors.md +48 -0
- package/docs/listen-and-subscribe.md +126 -0
- package/docs/model-based-testing.md +321 -0
- package/docs/modeling.md +40 -0
- package/docs/parallel-states.md +182 -0
- package/docs/persist-and-restore-actors.md +115 -0
- package/docs/persistence.md +325 -0
- package/docs/quick-start.md +55 -0
- package/docs/retries-and-timeouts.md +122 -0
- package/docs/route-states.md +104 -0
- package/docs/scxml.md +91 -0
- package/docs/selectors.md +99 -0
- package/docs/serialization.md +127 -0
- package/docs/setup-and-provide.md +271 -0
- package/docs/snapshots.md +134 -0
- package/docs/spawn.md +220 -0
- package/docs/state-input.md +162 -0
- package/docs/statecharts.md +54 -0
- package/docs/states-events-transitions.md +82 -0
- package/docs/states.md +168 -0
- package/docs/system-transitions.md +170 -0
- package/docs/systems.md +166 -0
- package/docs/testing.md +150 -0
- package/docs/timeouts.md +125 -0
- package/docs/transition-functions.md +89 -0
- package/docs/transitions.md +187 -0
- package/docs/troubleshooting.md +48 -0
- package/docs/tutorial.md +481 -0
- package/docs/typescript.md +211 -0
- package/docs/typestates.md +110 -0
- package/docs/utilities.md +39 -0
- package/docs/why-state-machines.md +59 -0
- package/docs/xstate-v5-to-v6.md +1771 -0
- package/docs/your-first-machine.md +79 -0
- package/durable/package.json +8 -0
- package/fsm/package.json +8 -0
- package/package.json +56 -10
- package/validation/package.json +8 -0
- package/actions/package.json +0 -8
- package/dev/package.json +0 -8
- package/dist/StateMachine-1ff90553.esm.js +0 -729
- package/dist/StateMachine-2b259bff.development.esm.js +0 -746
- package/dist/StateMachine-6673bf76.cjs.js +0 -732
- package/dist/StateMachine-68fe6077.development.cjs.js +0 -749
- package/dist/declarations/src/createMachineFromConfig.d.ts +0 -177
- package/dist/declarations/src/getNextSnapshot.d.ts +0 -37
- package/dist/declarations/src/graph/TestModel.d.ts +0 -70
- package/dist/declarations/src/graph/pathGenerators.d.ts +0 -4
- package/dist/index-87499be0.cjs.js +0 -4415
- package/dist/index-9594f050.development.esm.js +0 -4399
- package/dist/index-aaf74d09.esm.js +0 -4360
- package/dist/index-b063a967.development.cjs.js +0 -4454
- package/guards/package.json +0 -8
- package/invoke/package.json +0 -4
|
@@ -2,384 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var dist_xstateActors = require('./index-
|
|
6
|
-
var
|
|
7
|
-
|
|
8
|
-
function simpleStringify(value) {
|
|
9
|
-
return JSON.stringify(value);
|
|
10
|
-
}
|
|
11
|
-
function formatPathTestResult(path, testPathResult, options) {
|
|
12
|
-
const resolvedOptions = {
|
|
13
|
-
formatColor: (_color, string) => string,
|
|
14
|
-
serializeState: simpleStringify,
|
|
15
|
-
serializeEvent: simpleStringify,
|
|
16
|
-
...options
|
|
17
|
-
};
|
|
18
|
-
const {
|
|
19
|
-
formatColor,
|
|
20
|
-
serializeState,
|
|
21
|
-
serializeEvent
|
|
22
|
-
} = resolvedOptions;
|
|
23
|
-
const {
|
|
24
|
-
state
|
|
25
|
-
} = path;
|
|
26
|
-
const targetStateString = serializeState(state, path.steps.length ? path.steps[path.steps.length - 1].event : undefined);
|
|
27
|
-
let errMessage = '';
|
|
28
|
-
let hasFailed = false;
|
|
29
|
-
errMessage += '\nPath:\n' + testPathResult.steps.map((s, i, steps) => {
|
|
30
|
-
const stateString = serializeState(s.step.state, i > 0 ? steps[i - 1].step.event : undefined);
|
|
31
|
-
const eventString = serializeEvent(s.step.event);
|
|
32
|
-
const stateResult = `\tState: ${hasFailed ? formatColor('gray', stateString) : s.state.error ? (hasFailed = true, formatColor('redBright', stateString)) : formatColor('greenBright', stateString)}`;
|
|
33
|
-
const eventResult = `\tEvent: ${hasFailed ? formatColor('gray', eventString) : s.event.error ? (hasFailed = true, formatColor('red', eventString)) : formatColor('green', eventString)}`;
|
|
34
|
-
return [stateResult, eventResult].join('\n');
|
|
35
|
-
}).concat(`\tState: ${hasFailed ? formatColor('gray', targetStateString) : testPathResult.state.error ? formatColor('red', targetStateString) : formatColor('green', targetStateString)}`).join('\n\n');
|
|
36
|
-
return errMessage;
|
|
37
|
-
}
|
|
38
|
-
function getDescription(snapshot) {
|
|
39
|
-
const contextString = !Object.keys(snapshot.context).length ? '' : `(${JSON.stringify(snapshot.context)})`;
|
|
40
|
-
const stateStrings = snapshot._nodes.filter(sn => sn.type === 'atomic' || sn.type === 'final').map(({
|
|
41
|
-
id,
|
|
42
|
-
path
|
|
43
|
-
}) => {
|
|
44
|
-
const meta = snapshot.getMeta()[id];
|
|
45
|
-
if (!meta) {
|
|
46
|
-
return `"${path.join('.')}"`;
|
|
47
|
-
}
|
|
48
|
-
const {
|
|
49
|
-
description
|
|
50
|
-
} = meta;
|
|
51
|
-
if (typeof description === 'function') {
|
|
52
|
-
return description(snapshot);
|
|
53
|
-
}
|
|
54
|
-
return description ? `"${description}"` : JSON.stringify(snapshot.value);
|
|
55
|
-
});
|
|
56
|
-
return `state${stateStrings.length === 1 ? '' : 's'} ` + stateStrings.join(', ') + ` ${contextString}`.trim();
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Deduplicates your paths so that A -> B is not executed separately to A -> B
|
|
61
|
-
* -> C
|
|
62
|
-
*/
|
|
63
|
-
const deduplicatePaths = (paths, serializeEvent = simpleStringify) => {
|
|
64
|
-
/** Put all paths on the same level so we can dedup them */
|
|
65
|
-
const allPathsWithEventSequence = [];
|
|
66
|
-
paths.forEach(path => {
|
|
67
|
-
allPathsWithEventSequence.push({
|
|
68
|
-
path,
|
|
69
|
-
eventSequence: path.steps.map(step => serializeEvent(step.event))
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
// Sort by path length, descending
|
|
74
|
-
allPathsWithEventSequence.sort((a, z) => z.path.steps.length - a.path.steps.length);
|
|
75
|
-
const superpathsWithEventSequence = [];
|
|
76
|
-
|
|
77
|
-
/** Filter out the paths that are subpaths of superpaths */
|
|
78
|
-
pathLoop: for (const pathWithEventSequence of allPathsWithEventSequence) {
|
|
79
|
-
// Check each existing superpath to see if the path is a subpath of it
|
|
80
|
-
superpathLoop: for (const superpathWithEventSequence of superpathsWithEventSequence) {
|
|
81
|
-
// eslint-disable-next-line @typescript-eslint/no-for-in-array
|
|
82
|
-
for (const i in pathWithEventSequence.eventSequence) {
|
|
83
|
-
// Check event sequence to determine if path is subpath, e.g.:
|
|
84
|
-
//
|
|
85
|
-
// This will short-circuit the check
|
|
86
|
-
// ['a', 'b', 'c', 'd'] (superpath)
|
|
87
|
-
// ['a', 'b', 'x'] (path)
|
|
88
|
-
//
|
|
89
|
-
// This will not short-circuit; path is subpath
|
|
90
|
-
// ['a', 'b', 'c', 'd'] (superpath)
|
|
91
|
-
// ['a', 'b', 'c'] (path)
|
|
92
|
-
if (pathWithEventSequence.eventSequence[i] !== superpathWithEventSequence.eventSequence[i]) {
|
|
93
|
-
// If the path is different from the superpath,
|
|
94
|
-
// continue to the next superpath
|
|
95
|
-
continue superpathLoop;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// If we reached here, path is subpath of superpath
|
|
100
|
-
// Continue & do not add path to superpaths
|
|
101
|
-
continue pathLoop;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// If we reached here, path is not a subpath of any existing superpaths
|
|
105
|
-
// So add it to the superpaths
|
|
106
|
-
superpathsWithEventSequence.push(pathWithEventSequence);
|
|
107
|
-
}
|
|
108
|
-
return superpathsWithEventSequence.map(path => path.path);
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
const createShortestPathsGen = () => (logic, defaultOptions) => {
|
|
112
|
-
const paths = getShortestPaths(logic, defaultOptions);
|
|
113
|
-
return paths;
|
|
114
|
-
};
|
|
115
|
-
const createSimplePathsGen = () => (logic, defaultOptions) => {
|
|
116
|
-
const paths = getSimplePaths(logic, defaultOptions);
|
|
117
|
-
return paths;
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
const validateState = _state => {
|
|
121
|
-
// TODO
|
|
122
|
-
};
|
|
123
|
-
const validateMachine = machine => {
|
|
124
|
-
validateState(machine.root);
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Creates a test model that represents an abstract model of a system under test
|
|
129
|
-
* (SUT).
|
|
130
|
-
*
|
|
131
|
-
* The test model is used to generate test paths, which are used to verify that
|
|
132
|
-
* states in the model are reachable in the SUT.
|
|
133
|
-
*/
|
|
134
|
-
class TestModel {
|
|
135
|
-
getDefaultOptions() {
|
|
136
|
-
return {
|
|
137
|
-
serializeState: state => simpleStringify(state),
|
|
138
|
-
serializeEvent: event => simpleStringify(event),
|
|
139
|
-
// For non-state-machine test models, we cannot identify
|
|
140
|
-
// separate transitions, so just use event type
|
|
141
|
-
serializeTransition: (state, event) => `${simpleStringify(state)}|${event?.type}`,
|
|
142
|
-
events: [],
|
|
143
|
-
stateMatcher: (_, stateKey) => stateKey === '*',
|
|
144
|
-
logger: {
|
|
145
|
-
log: console.log.bind(console),
|
|
146
|
-
error: console.error.bind(console)
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
constructor(testLogic, options) {
|
|
151
|
-
this.testLogic = testLogic;
|
|
152
|
-
this.options = void 0;
|
|
153
|
-
this.defaultTraversalOptions = void 0;
|
|
154
|
-
this._toTestPath = statePath => {
|
|
155
|
-
function formatEvent(event) {
|
|
156
|
-
const {
|
|
157
|
-
type,
|
|
158
|
-
...other
|
|
159
|
-
} = event;
|
|
160
|
-
const propertyString = Object.keys(other).length ? ` (${JSON.stringify(other)})` : '';
|
|
161
|
-
return `${type}${propertyString}`;
|
|
162
|
-
}
|
|
163
|
-
const eventsString = statePath.steps.map(s => formatEvent(s.event)).join(' → ');
|
|
164
|
-
return {
|
|
165
|
-
...statePath,
|
|
166
|
-
test: params => this.testPath(statePath, params),
|
|
167
|
-
description: dist_xstateActors.isMachineSnapshot(statePath.state) ? `Reaches ${getDescription(statePath.state).trim()}: ${eventsString}` : JSON.stringify(statePath.state)
|
|
168
|
-
};
|
|
169
|
-
};
|
|
170
|
-
this.options = {
|
|
171
|
-
...this.getDefaultOptions(),
|
|
172
|
-
...options
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
getPaths(pathGenerator, options) {
|
|
176
|
-
const allowDuplicatePaths = options?.allowDuplicatePaths ?? false;
|
|
177
|
-
const paths = pathGenerator(this.testLogic, this._resolveOptions(options));
|
|
178
|
-
return (allowDuplicatePaths ? paths : deduplicatePaths(paths)).map(this._toTestPath);
|
|
179
|
-
}
|
|
180
|
-
getShortestPaths(options) {
|
|
181
|
-
return this.getPaths(createShortestPathsGen(), options);
|
|
182
|
-
}
|
|
183
|
-
getShortestPathsFrom(paths, options) {
|
|
184
|
-
const resultPaths = [];
|
|
185
|
-
for (const path of paths) {
|
|
186
|
-
const shortestPaths = this.getShortestPaths({
|
|
187
|
-
...options,
|
|
188
|
-
fromState: path.state
|
|
189
|
-
});
|
|
190
|
-
for (const shortestPath of shortestPaths) {
|
|
191
|
-
resultPaths.push(this._toTestPath(joinPaths(path, shortestPath)));
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
return resultPaths;
|
|
195
|
-
}
|
|
196
|
-
getSimplePaths(options) {
|
|
197
|
-
return this.getPaths(createSimplePathsGen(), options);
|
|
198
|
-
}
|
|
199
|
-
getSimplePathsFrom(paths, options) {
|
|
200
|
-
const resultPaths = [];
|
|
201
|
-
for (const path of paths) {
|
|
202
|
-
const shortestPaths = this.getSimplePaths({
|
|
203
|
-
...options,
|
|
204
|
-
fromState: path.state
|
|
205
|
-
});
|
|
206
|
-
for (const shortestPath of shortestPaths) {
|
|
207
|
-
resultPaths.push(this._toTestPath(joinPaths(path, shortestPath)));
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
return resultPaths;
|
|
211
|
-
}
|
|
212
|
-
getPathsFromEvents(events, options) {
|
|
213
|
-
const paths = getPathsFromEvents(this.testLogic, events, options);
|
|
214
|
-
return paths.map(this._toTestPath);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* An array of adjacencies, which are objects that represent each `state` with
|
|
219
|
-
* the `nextState` given the `event`.
|
|
220
|
-
*/
|
|
221
|
-
getAdjacencyMap() {
|
|
222
|
-
const adjMap = getAdjacencyMap(this.testLogic, this.options);
|
|
223
|
-
return adjMap;
|
|
224
|
-
}
|
|
225
|
-
async testPath(path, params, options) {
|
|
226
|
-
const testPathResult = {
|
|
227
|
-
steps: [],
|
|
228
|
-
state: {
|
|
229
|
-
error: null
|
|
230
|
-
}
|
|
231
|
-
};
|
|
232
|
-
try {
|
|
233
|
-
for (const step of path.steps) {
|
|
234
|
-
const testStepResult = {
|
|
235
|
-
step,
|
|
236
|
-
state: {
|
|
237
|
-
error: null
|
|
238
|
-
},
|
|
239
|
-
event: {
|
|
240
|
-
error: null
|
|
241
|
-
}
|
|
242
|
-
};
|
|
243
|
-
testPathResult.steps.push(testStepResult);
|
|
244
|
-
try {
|
|
245
|
-
await this.testTransition(params, step);
|
|
246
|
-
} catch (err) {
|
|
247
|
-
testStepResult.event.error = err;
|
|
248
|
-
throw err;
|
|
249
|
-
}
|
|
250
|
-
try {
|
|
251
|
-
await this.testState(params, step.state, options);
|
|
252
|
-
} catch (err) {
|
|
253
|
-
testStepResult.state.error = err;
|
|
254
|
-
throw err;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
} catch (err) {
|
|
258
|
-
// TODO: make option
|
|
259
|
-
err.message += formatPathTestResult(path, testPathResult, this.options);
|
|
260
|
-
throw err;
|
|
261
|
-
}
|
|
262
|
-
return testPathResult;
|
|
263
|
-
}
|
|
264
|
-
async testState(params, state, options) {
|
|
265
|
-
const resolvedOptions = this._resolveOptions(options);
|
|
266
|
-
const stateTestKeys = this._getStateTestKeys(params, state, resolvedOptions);
|
|
267
|
-
for (const stateTestKey of stateTestKeys) {
|
|
268
|
-
await params.states?.[stateTestKey](state);
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
_getStateTestKeys(params, state, resolvedOptions) {
|
|
272
|
-
const states = params.states || {};
|
|
273
|
-
const stateTestKeys = Object.keys(states).filter(stateKey => {
|
|
274
|
-
return resolvedOptions.stateMatcher(state, stateKey);
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
// Fallthrough state tests
|
|
278
|
-
if (!stateTestKeys.length && '*' in states) {
|
|
279
|
-
stateTestKeys.push('*');
|
|
280
|
-
}
|
|
281
|
-
return stateTestKeys;
|
|
282
|
-
}
|
|
283
|
-
_getEventExec(params, step) {
|
|
284
|
-
const eventExec = params.events?.[step.event.type];
|
|
285
|
-
return eventExec;
|
|
286
|
-
}
|
|
287
|
-
async testTransition(params, step) {
|
|
288
|
-
const eventExec = this._getEventExec(params, step);
|
|
289
|
-
await eventExec?.(step);
|
|
290
|
-
}
|
|
291
|
-
_resolveOptions(options) {
|
|
292
|
-
return {
|
|
293
|
-
...this.defaultTraversalOptions,
|
|
294
|
-
...this.options,
|
|
295
|
-
...options
|
|
296
|
-
};
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
function stateValuesEqual(a, b) {
|
|
300
|
-
if (a === b) {
|
|
301
|
-
return true;
|
|
302
|
-
}
|
|
303
|
-
if (a === undefined || b === undefined) {
|
|
304
|
-
return false;
|
|
305
|
-
}
|
|
306
|
-
if (typeof a === 'string' || typeof b === 'string') {
|
|
307
|
-
return a === b;
|
|
308
|
-
}
|
|
309
|
-
const aKeys = Object.keys(a);
|
|
310
|
-
const bKeys = Object.keys(b);
|
|
311
|
-
return aKeys.length === bKeys.length && aKeys.every(key => stateValuesEqual(a[key], b[key]));
|
|
312
|
-
}
|
|
313
|
-
function serializeMachineTransition(snapshot, event, previousSnapshot, {
|
|
314
|
-
serializeEvent
|
|
315
|
-
}) {
|
|
316
|
-
// TODO: the stateValuesEqual check here is very likely not exactly correct
|
|
317
|
-
// but I'm not sure what the correct check is and what this is trying to do
|
|
318
|
-
if (!event || previousSnapshot && stateValuesEqual(previousSnapshot.value, snapshot.value)) {
|
|
319
|
-
return '';
|
|
320
|
-
}
|
|
321
|
-
const prevStateString = previousSnapshot ? ` from ${simpleStringify(previousSnapshot.value)}` : '';
|
|
322
|
-
return ` via ${serializeEvent(event)}${prevStateString}`;
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
/**
|
|
326
|
-
* Creates a test model that represents an abstract model of a system under test
|
|
327
|
-
* (SUT).
|
|
328
|
-
*
|
|
329
|
-
* The test model is used to generate test paths, which are used to verify that
|
|
330
|
-
* states in the `machine` are reachable in the SUT.
|
|
331
|
-
*
|
|
332
|
-
* @example
|
|
333
|
-
*
|
|
334
|
-
* ```js
|
|
335
|
-
* const toggleModel = createModel(toggleMachine).withEvents({
|
|
336
|
-
* TOGGLE: {
|
|
337
|
-
* exec: async (page) => {
|
|
338
|
-
* await page.click('input');
|
|
339
|
-
* }
|
|
340
|
-
* }
|
|
341
|
-
* });
|
|
342
|
-
* ```
|
|
343
|
-
*
|
|
344
|
-
* @param machine The state machine used to represent the abstract model.
|
|
345
|
-
* @param options Options for the created test model:
|
|
346
|
-
*
|
|
347
|
-
* - `events`: an object mapping string event types (e.g., `SUBMIT`) to an event
|
|
348
|
-
* test config (e.g., `{exec: () => {...}, cases: [...]}`)
|
|
349
|
-
*/
|
|
350
|
-
function createTestModel(machine, options) {
|
|
351
|
-
validateMachine(machine);
|
|
352
|
-
const serializeEvent = options?.serializeEvent ?? simpleStringify;
|
|
353
|
-
const serializeTransition = options?.serializeTransition ?? serializeMachineTransition;
|
|
354
|
-
const {
|
|
355
|
-
events: getEvents,
|
|
356
|
-
...otherOptions
|
|
357
|
-
} = options ?? {};
|
|
358
|
-
const testModel = new TestModel(machine, {
|
|
359
|
-
serializeState: (state, event, prevState) => {
|
|
360
|
-
// Only consider the `state` if `serializeTransition()` is opted out (empty string)
|
|
361
|
-
return `${serializeSnapshot(state)}${serializeTransition(state, event, prevState, {
|
|
362
|
-
serializeEvent
|
|
363
|
-
})}`;
|
|
364
|
-
},
|
|
365
|
-
stateMatcher: (state, key) => {
|
|
366
|
-
return key.startsWith('#') ? state._nodes.includes(machine.getStateNodeById(key)) : state.matches(key);
|
|
367
|
-
},
|
|
368
|
-
events: state => {
|
|
369
|
-
const events = typeof getEvents === 'function' ? getEvents(state) : getEvents ?? [];
|
|
370
|
-
return dist_xstateActors.getAllOwnEventDescriptors(state).flatMap(eventType => {
|
|
371
|
-
if (events.some(e => e.type === eventType)) {
|
|
372
|
-
return events.filter(e => e.type === eventType);
|
|
373
|
-
}
|
|
374
|
-
return [{
|
|
375
|
-
type: eventType
|
|
376
|
-
}]; // TODO: fix types
|
|
377
|
-
});
|
|
378
|
-
},
|
|
379
|
-
...otherOptions
|
|
380
|
-
});
|
|
381
|
-
return testModel;
|
|
382
|
-
}
|
|
5
|
+
var dist_xstateActors = require('./index-5ce9cbf9.development.cjs.js');
|
|
6
|
+
var createActor = require('./createActor-2136a8ad.development.cjs.js');
|
|
383
7
|
|
|
384
8
|
function createMockActorScope() {
|
|
385
9
|
const emptyActor = dist_xstateActors.createEmptyActor();
|
|
@@ -400,15 +24,17 @@ function createMockActorScope() {
|
|
|
400
24
|
/**
|
|
401
25
|
* Returns all state nodes of the given `node`.
|
|
402
26
|
*
|
|
27
|
+
* @public
|
|
403
28
|
* @param stateNode State node to recursively get child state nodes from
|
|
29
|
+
* @public
|
|
404
30
|
*/
|
|
405
|
-
function
|
|
31
|
+
function getDescendantStateNodes(stateNode) {
|
|
406
32
|
const {
|
|
407
33
|
states
|
|
408
34
|
} = stateNode;
|
|
409
35
|
const nodes = Object.keys(states).reduce((accNodes, stateKey) => {
|
|
410
36
|
const childStateNode = states[stateKey];
|
|
411
|
-
const childStateNodes =
|
|
37
|
+
const childStateNodes = getDescendantStateNodes(childStateNode);
|
|
412
38
|
accNodes.push(childStateNode, ...childStateNodes);
|
|
413
39
|
return accNodes;
|
|
414
40
|
}, []);
|
|
@@ -423,55 +49,87 @@ function getChildren(stateNode) {
|
|
|
423
49
|
});
|
|
424
50
|
return children;
|
|
425
51
|
}
|
|
52
|
+
|
|
53
|
+
/** @public */
|
|
426
54
|
function serializeSnapshot(snapshot) {
|
|
427
55
|
const {
|
|
428
56
|
value,
|
|
429
57
|
context
|
|
430
58
|
} = snapshot;
|
|
59
|
+
const machineSnapshot = createActor.isMachineSnapshot(snapshot) ? snapshot : undefined;
|
|
60
|
+
const inputs = machineSnapshot?.getInputs();
|
|
61
|
+
const history = machineSnapshot?.historyValue;
|
|
431
62
|
return JSON.stringify({
|
|
432
63
|
value,
|
|
433
|
-
context: Object.keys(context ?? {}).length ? context : undefined
|
|
64
|
+
context: Object.keys(context ?? {}).length ? context : undefined,
|
|
65
|
+
// Equal values and root contexts can still make different decisions when
|
|
66
|
+
// their state inputs or remembered history differ. Empty records remain
|
|
67
|
+
// omitted to preserve the keys of machines that do not use these features.
|
|
68
|
+
stateInputs: inputs && Object.keys(inputs).length ? inputs : undefined,
|
|
69
|
+
historyValue: history && Object.keys(history).length ? Object.fromEntries(Object.entries(history).map(([id, nodes]) => [id, nodes.map(node => node.id)])) : undefined
|
|
434
70
|
});
|
|
435
71
|
}
|
|
436
72
|
function serializeEvent(event) {
|
|
437
73
|
return JSON.stringify(event);
|
|
438
74
|
}
|
|
75
|
+
function resolveEventCases(cases, state, defaults) {
|
|
76
|
+
const events = [];
|
|
77
|
+
const sources = cases;
|
|
78
|
+
const entries = Object.entries(sources).filter(([, source]) => source !== undefined);
|
|
79
|
+
const applicable = type => !defaults || defaults.some(event => createActor.matchesEventDescriptor(type, event.type));
|
|
80
|
+
for (const [type, source] of entries) {
|
|
81
|
+
if (!applicable(type)) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const samples = typeof source === 'function' ? source(state) : source;
|
|
85
|
+
if (!Array.isArray(samples) || !samples.length) {
|
|
86
|
+
throw new Error(`Event cases for "${type}" must be a non-empty array`);
|
|
87
|
+
}
|
|
88
|
+
for (const payload of samples) {
|
|
89
|
+
const event = {
|
|
90
|
+
...payload,
|
|
91
|
+
type
|
|
92
|
+
};
|
|
93
|
+
if (!defaults || defaults.some(({
|
|
94
|
+
type: descriptor,
|
|
95
|
+
...pattern
|
|
96
|
+
}) => createActor.matchesEventDescriptor(type, descriptor) && createActor.matchesEvent(event, pattern))) {
|
|
97
|
+
events.push(event);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return [...events, ...(defaults?.filter(event => !entries.some(([type]) => type === event.type)) ?? [])];
|
|
102
|
+
}
|
|
439
103
|
function createDefaultMachineOptions(machine, options) {
|
|
440
|
-
const {
|
|
441
|
-
events: getEvents,
|
|
442
|
-
...otherOptions
|
|
443
|
-
} = options ?? {};
|
|
444
104
|
const traversalOptions = {
|
|
445
105
|
serializeState: serializeSnapshot,
|
|
446
106
|
serializeEvent,
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
const matchingEvents = events.filter(ev => ev.type === type);
|
|
451
|
-
if (matchingEvents.length) {
|
|
452
|
-
return matchingEvents;
|
|
453
|
-
}
|
|
454
|
-
return [{
|
|
455
|
-
type
|
|
456
|
-
}];
|
|
457
|
-
});
|
|
458
|
-
},
|
|
459
|
-
fromState: machine.getInitialSnapshot(createMockActorScope(), options?.input),
|
|
460
|
-
...otherOptions
|
|
107
|
+
...options,
|
|
108
|
+
events: state => createActor.getAllOwnEvents(state),
|
|
109
|
+
fromState: options?.fromState ?? machine.getInitialSnapshot(createMockActorScope(), options?.input)
|
|
461
110
|
};
|
|
462
111
|
return traversalOptions;
|
|
463
112
|
}
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
serializeState: state => JSON.stringify(state),
|
|
467
|
-
serializeEvent
|
|
468
|
-
};
|
|
469
|
-
}
|
|
113
|
+
|
|
114
|
+
/** @public */
|
|
470
115
|
function toDirectedGraph(stateMachine) {
|
|
471
|
-
const stateNode = stateMachine
|
|
116
|
+
const stateNode = isMachineLogic(stateMachine) ? stateMachine.root : stateMachine; // TODO: accept only machines
|
|
472
117
|
|
|
473
|
-
const
|
|
474
|
-
|
|
118
|
+
const diagnostics = [];
|
|
119
|
+
const transitions = [...[...stateNode.transitions.values()].flat(), ...(stateNode.always ?? [])];
|
|
120
|
+
const edges = transitions.flatMap((t, transitionIndex) => {
|
|
121
|
+
if (t.to) {
|
|
122
|
+
diagnostics.push({
|
|
123
|
+
code: 'dynamic-target',
|
|
124
|
+
stateId: stateNode.id,
|
|
125
|
+
eventType: t.eventType,
|
|
126
|
+
transitionId: `${stateNode.id}:${transitionIndex}`,
|
|
127
|
+
message: 'Transition function destinations require execution or source analysis.'
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
// A transition function computes its target when it runs, so there is
|
|
131
|
+
// no static edge to draw; a targetless transition stays in its state.
|
|
132
|
+
const targets = t.target ?? (t.to ? [] : [stateNode]);
|
|
475
133
|
return targets.map((target, targetIndex) => {
|
|
476
134
|
const edge = {
|
|
477
135
|
id: `${stateNode.id}:${transitionIndex}:${targetIndex}`,
|
|
@@ -498,11 +156,17 @@ function toDirectedGraph(stateMachine) {
|
|
|
498
156
|
return edge;
|
|
499
157
|
});
|
|
500
158
|
});
|
|
159
|
+
const children = getChildren(stateNode).map(toDirectedGraph);
|
|
160
|
+
diagnostics.push(...children.flatMap(child => child.analysis.diagnostics));
|
|
501
161
|
const graph = {
|
|
502
162
|
id: stateNode.id,
|
|
503
163
|
stateNode: stateNode,
|
|
504
|
-
children
|
|
164
|
+
children,
|
|
505
165
|
edges,
|
|
166
|
+
analysis: {
|
|
167
|
+
transitionTargetsComplete: diagnostics.length === 0,
|
|
168
|
+
diagnostics
|
|
169
|
+
},
|
|
506
170
|
toJSON: () => {
|
|
507
171
|
const {
|
|
508
172
|
id,
|
|
@@ -512,34 +176,44 @@ function toDirectedGraph(stateMachine) {
|
|
|
512
176
|
return {
|
|
513
177
|
id,
|
|
514
178
|
children,
|
|
515
|
-
edges: graphEdges
|
|
179
|
+
edges: graphEdges,
|
|
180
|
+
analysis: graph.analysis
|
|
516
181
|
};
|
|
517
182
|
}
|
|
518
183
|
};
|
|
519
184
|
return graph;
|
|
520
185
|
}
|
|
521
186
|
function isMachineLogic(logic) {
|
|
522
|
-
|
|
187
|
+
if (!logic || typeof logic !== 'object') {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
const machine = logic;
|
|
191
|
+
const root = machine.root;
|
|
192
|
+
return !!root && typeof root === 'object' && typeof root.id === 'string' && typeof root.states === 'object' && typeof root.transitions?.values === 'function' && typeof machine.getStateNodeById === 'function' && typeof machine.resolveState === 'function' && typeof machine.getTransitionData === 'function';
|
|
523
193
|
}
|
|
524
194
|
function resolveTraversalOptions(logic, traversalOptions, defaultOptions) {
|
|
525
195
|
const resolvedDefaultOptions = defaultOptions ?? (isMachineLogic(logic) ? createDefaultMachineOptions(logic, traversalOptions) : undefined);
|
|
526
196
|
const serializeState = traversalOptions?.serializeState ?? resolvedDefaultOptions?.serializeState ?? (state => JSON.stringify(state));
|
|
197
|
+
const eventSource = traversalOptions?.events ?? resolvedDefaultOptions?.events ?? [];
|
|
527
198
|
const traversalConfig = {
|
|
528
199
|
serializeState,
|
|
529
200
|
serializeEvent,
|
|
530
|
-
events: [],
|
|
531
201
|
filterEvents: undefined,
|
|
532
202
|
limit: Infinity,
|
|
533
|
-
fromState: undefined,
|
|
534
203
|
toState: undefined,
|
|
535
204
|
// Traversal should not continue past the `toState` predicate
|
|
536
205
|
// since the target state has already been reached at that point
|
|
537
206
|
stopWhen: traversalOptions?.toState,
|
|
538
207
|
...resolvedDefaultOptions,
|
|
539
|
-
...traversalOptions
|
|
208
|
+
...traversalOptions,
|
|
209
|
+
// Synthetic completion/delay events are supported by the machine runtime.
|
|
210
|
+
events: typeof eventSource === 'function' || Array.isArray(eventSource) ? eventSource : state => resolveEventCases(eventSource, state, createActor.isMachineSnapshot(state) ? createActor.getAllOwnEvents(state) : undefined),
|
|
211
|
+
fromState: traversalOptions?.fromState ?? resolvedDefaultOptions?.fromState
|
|
540
212
|
};
|
|
541
213
|
return traversalConfig;
|
|
542
214
|
}
|
|
215
|
+
|
|
216
|
+
/** @public */
|
|
543
217
|
function joinPaths(headPath, tailPath) {
|
|
544
218
|
const secondPathSource = tailPath.steps[0].state;
|
|
545
219
|
if (secondPathSource !== headPath.state) {
|
|
@@ -553,9 +227,18 @@ function joinPaths(headPath, tailPath) {
|
|
|
553
227
|
};
|
|
554
228
|
}
|
|
555
229
|
|
|
230
|
+
function createTraversalError(state, event, cause) {
|
|
231
|
+
return new Error(`Unable to evaluate graph transition from state ${state} with event ${event}. If the transition needs event payloads, provide representative events using the "events" option.`, {
|
|
232
|
+
cause
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
556
236
|
function resolveTransitionSnapshot(result) {
|
|
557
237
|
return Array.isArray(result) ? result[0] : result;
|
|
558
238
|
}
|
|
239
|
+
|
|
240
|
+
/** @public */
|
|
241
|
+
|
|
559
242
|
function getAdjacencyMap(logic, options) {
|
|
560
243
|
|
|
561
244
|
const {
|
|
@@ -573,21 +256,25 @@ function getAdjacencyMap(logic, options) {
|
|
|
573
256
|
const actorScope = createMockActorScope();
|
|
574
257
|
const fromState = customFromState ?? logic.getInitialSnapshot(actorScope,
|
|
575
258
|
// TODO: fix this
|
|
576
|
-
options
|
|
577
|
-
const adj =
|
|
259
|
+
options?.input);
|
|
260
|
+
const adj = Object.create(null);
|
|
578
261
|
let iterations = 0;
|
|
579
262
|
const queue = [{
|
|
580
263
|
nextState: fromState,
|
|
581
264
|
event: undefined,
|
|
582
265
|
prevState: undefined
|
|
583
266
|
}];
|
|
584
|
-
|
|
585
|
-
while (queue.length) {
|
|
267
|
+
for (let head = 0; head < queue.length; head++) {
|
|
586
268
|
const {
|
|
587
269
|
nextState: state,
|
|
588
270
|
event,
|
|
589
271
|
prevState
|
|
590
|
-
} = queue
|
|
272
|
+
} = queue[head];
|
|
273
|
+
// Release processed entries without shifting the remaining frontier.
|
|
274
|
+
if (head > 4096 && head * 2 > queue.length) {
|
|
275
|
+
queue.splice(0, head + 1);
|
|
276
|
+
head = -1;
|
|
277
|
+
}
|
|
591
278
|
if (iterations++ > limit) {
|
|
592
279
|
throw new Error('Traversal limit exceeded');
|
|
593
280
|
}
|
|
@@ -595,10 +282,9 @@ function getAdjacencyMap(logic, options) {
|
|
|
595
282
|
if (adj[serializedState]) {
|
|
596
283
|
continue;
|
|
597
284
|
}
|
|
598
|
-
stateMap.set(serializedState, state);
|
|
599
285
|
adj[serializedState] = {
|
|
600
286
|
state,
|
|
601
|
-
transitions:
|
|
287
|
+
transitions: Object.create(null)
|
|
602
288
|
};
|
|
603
289
|
if (stopWhen && stopWhen(state)) {
|
|
604
290
|
continue;
|
|
@@ -608,8 +294,14 @@ function getAdjacencyMap(logic, options) {
|
|
|
608
294
|
if (filterEvents && !filterEvents(state, nextEvent)) {
|
|
609
295
|
continue;
|
|
610
296
|
}
|
|
611
|
-
const
|
|
612
|
-
|
|
297
|
+
const serializedEvent = serializeEvent(nextEvent);
|
|
298
|
+
let nextSnapshot;
|
|
299
|
+
try {
|
|
300
|
+
nextSnapshot = resolveTransitionSnapshot(transition(state, nextEvent, actorScope));
|
|
301
|
+
} catch (cause) {
|
|
302
|
+
throw createTraversalError(serializedState, serializedEvent, cause);
|
|
303
|
+
}
|
|
304
|
+
adj[serializedState].transitions[serializedEvent] = {
|
|
613
305
|
event: nextEvent,
|
|
614
306
|
state: nextSnapshot
|
|
615
307
|
};
|
|
@@ -622,6 +314,8 @@ function getAdjacencyMap(logic, options) {
|
|
|
622
314
|
}
|
|
623
315
|
return adj;
|
|
624
316
|
}
|
|
317
|
+
|
|
318
|
+
/** @public */
|
|
625
319
|
function adjacencyMapToArray(adjMap) {
|
|
626
320
|
const adjList = [];
|
|
627
321
|
for (const adjValue of Object.values(adjMap)) {
|
|
@@ -643,7 +337,7 @@ function alterPath(path) {
|
|
|
643
337
|
steps = [{
|
|
644
338
|
state: path.state,
|
|
645
339
|
event: {
|
|
646
|
-
type:
|
|
340
|
+
type: createActor.XSTATE_INIT
|
|
647
341
|
}
|
|
648
342
|
}];
|
|
649
343
|
} else {
|
|
@@ -652,7 +346,7 @@ function alterPath(path) {
|
|
|
652
346
|
steps.push({
|
|
653
347
|
state: step.state,
|
|
654
348
|
event: i === 0 ? {
|
|
655
|
-
type:
|
|
349
|
+
type: createActor.XSTATE_INIT
|
|
656
350
|
} : path.steps[i - 1].event
|
|
657
351
|
});
|
|
658
352
|
}
|
|
@@ -667,46 +361,60 @@ function alterPath(path) {
|
|
|
667
361
|
};
|
|
668
362
|
}
|
|
669
363
|
|
|
670
|
-
|
|
671
|
-
return !!value && value instanceof StateMachine.StateMachine;
|
|
672
|
-
}
|
|
364
|
+
/** @public */
|
|
673
365
|
function getPathsFromEvents(logic, events, options) {
|
|
674
366
|
const resolvedOptions = resolveTraversalOptions(logic, {
|
|
675
367
|
events,
|
|
676
368
|
...options
|
|
677
|
-
}
|
|
369
|
+
});
|
|
678
370
|
const actorScope = createMockActorScope();
|
|
679
371
|
const fromState = resolvedOptions.fromState ?? logic.getInitialSnapshot(actorScope,
|
|
680
372
|
// TODO: fix this
|
|
681
373
|
options?.input);
|
|
682
374
|
const {
|
|
683
375
|
serializeState,
|
|
684
|
-
serializeEvent
|
|
376
|
+
serializeEvent,
|
|
377
|
+
filterEvents,
|
|
378
|
+
stopWhen,
|
|
379
|
+
limit
|
|
685
380
|
} = resolvedOptions;
|
|
686
|
-
const adjacency = getAdjacencyMap(logic, resolvedOptions);
|
|
687
|
-
const stateMap = new Map();
|
|
688
381
|
const steps = [];
|
|
689
|
-
const serializedFromState = serializeState(fromState, undefined, undefined);
|
|
690
|
-
stateMap.set(serializedFromState, fromState);
|
|
691
|
-
let stateSerial = serializedFromState;
|
|
692
382
|
let state = fromState;
|
|
383
|
+
let stateSerial = serializeState(state, undefined, undefined);
|
|
693
384
|
for (const event of events) {
|
|
385
|
+
if (steps.length >= limit) {
|
|
386
|
+
throw new Error('Traversal limit exceeded');
|
|
387
|
+
}
|
|
388
|
+
const eventSerial = serializeEvent(event);
|
|
389
|
+
let nextEvent = event;
|
|
390
|
+
if (options?.events !== undefined) {
|
|
391
|
+
const candidates = typeof options.events === 'function' ? options.events(state) : options.events;
|
|
392
|
+
nextEvent = undefined;
|
|
393
|
+
for (let index = candidates.length - 1; index >= 0; index--) {
|
|
394
|
+
const candidate = candidates[index];
|
|
395
|
+
if ((!filterEvents || filterEvents(state, candidate)) && serializeEvent(candidate) === eventSerial) {
|
|
396
|
+
nextEvent = candidate;
|
|
397
|
+
break;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
} else if (filterEvents && !filterEvents(state, event)) {
|
|
401
|
+
nextEvent = undefined;
|
|
402
|
+
}
|
|
403
|
+
if (!nextEvent || stopWhen?.(state)) {
|
|
404
|
+
throw new Error(`Invalid transition from ${stateSerial} with ${eventSerial}`);
|
|
405
|
+
}
|
|
694
406
|
steps.push({
|
|
695
|
-
state
|
|
407
|
+
state,
|
|
696
408
|
event
|
|
697
409
|
});
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
state
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
if (!nextState) {
|
|
704
|
-
throw new Error(`Invalid transition from ${stateSerial} with ${eventSerial}`);
|
|
410
|
+
let result;
|
|
411
|
+
try {
|
|
412
|
+
result = logic.transition(state, nextEvent, actorScope);
|
|
413
|
+
} catch (cause) {
|
|
414
|
+
throw createTraversalError(stateSerial, eventSerial, cause);
|
|
705
415
|
}
|
|
706
|
-
const
|
|
707
|
-
|
|
708
|
-
stateMap.set(nextStateSerial, nextState);
|
|
709
|
-
stateSerial = nextStateSerial;
|
|
416
|
+
const nextState = Array.isArray(result) ? result[0] : result;
|
|
417
|
+
stateSerial = serializeState(nextState, event, state);
|
|
710
418
|
state = nextState;
|
|
711
419
|
}
|
|
712
420
|
|
|
@@ -722,11 +430,16 @@ function getPathsFromEvents(logic, events, options) {
|
|
|
722
430
|
})];
|
|
723
431
|
}
|
|
724
432
|
|
|
433
|
+
/** @public */
|
|
434
|
+
|
|
725
435
|
function getShortestPaths(logic, options) {
|
|
726
436
|
const resolvedOptions = resolveTraversalOptions(logic, options);
|
|
727
437
|
const serializeState = resolvedOptions.serializeState;
|
|
728
438
|
const fromState = resolvedOptions.fromState ?? logic.getInitialSnapshot(createMockActorScope(), options?.input);
|
|
729
|
-
const adjacency = getAdjacencyMap(logic,
|
|
439
|
+
const adjacency = getAdjacencyMap(logic, {
|
|
440
|
+
...resolvedOptions,
|
|
441
|
+
fromState
|
|
442
|
+
});
|
|
730
443
|
|
|
731
444
|
// weight, state, event
|
|
732
445
|
const weightMap = new Map();
|
|
@@ -778,7 +491,7 @@ function getShortestPaths(logic, options) {
|
|
|
778
491
|
visited.add(serializedState);
|
|
779
492
|
unvisited.delete(serializedState);
|
|
780
493
|
}
|
|
781
|
-
const statePlanMap =
|
|
494
|
+
const statePlanMap = Object.create(null);
|
|
782
495
|
const paths = [];
|
|
783
496
|
weightMap.forEach(({
|
|
784
497
|
weight,
|
|
@@ -786,7 +499,7 @@ function getShortestPaths(logic, options) {
|
|
|
786
499
|
event: fromEvent
|
|
787
500
|
}, stateSerial) => {
|
|
788
501
|
const state = stateMap.get(stateSerial);
|
|
789
|
-
const steps =
|
|
502
|
+
const steps = fromState === undefined ? [] : statePlanMap[fromState].paths[0].steps.concat({
|
|
790
503
|
state: stateMap.get(fromState),
|
|
791
504
|
event: fromEvent
|
|
792
505
|
});
|
|
@@ -810,19 +523,24 @@ function getShortestPaths(logic, options) {
|
|
|
810
523
|
return paths.map(alterPath);
|
|
811
524
|
}
|
|
812
525
|
|
|
526
|
+
/** @public */
|
|
527
|
+
|
|
813
528
|
function getSimplePaths(logic, options) {
|
|
814
529
|
const resolvedOptions = resolveTraversalOptions(logic, options);
|
|
815
530
|
const actorScope = createMockActorScope();
|
|
816
531
|
const fromState = resolvedOptions.fromState ?? logic.getInitialSnapshot(actorScope, options?.input);
|
|
817
532
|
const serializeState = resolvedOptions.serializeState;
|
|
818
|
-
const adjacency = getAdjacencyMap(logic,
|
|
533
|
+
const adjacency = getAdjacencyMap(logic, {
|
|
534
|
+
...resolvedOptions,
|
|
535
|
+
fromState
|
|
536
|
+
});
|
|
819
537
|
const stateMap = new Map();
|
|
820
538
|
const visitCtx = {
|
|
821
539
|
vertices: new Set(),
|
|
822
540
|
edges: new Set()
|
|
823
541
|
};
|
|
824
542
|
const steps = [];
|
|
825
|
-
const pathMap =
|
|
543
|
+
const pathMap = Object.create(null);
|
|
826
544
|
function util(fromStateSerial, toStateSerial) {
|
|
827
545
|
const fromState = stateMap.get(fromStateSerial);
|
|
828
546
|
visitCtx.vertices.add(fromStateSerial);
|
|
@@ -880,16 +598,13 @@ function getSimplePaths(logic, options) {
|
|
|
880
598
|
return simplePaths.map(alterPath);
|
|
881
599
|
}
|
|
882
600
|
|
|
883
|
-
exports.
|
|
601
|
+
exports.getAllOwnEvents = createActor.getAllOwnEvents;
|
|
884
602
|
exports.adjacencyMapToArray = adjacencyMapToArray;
|
|
885
|
-
exports.createShortestPathsGen = createShortestPathsGen;
|
|
886
|
-
exports.createSimplePathsGen = createSimplePathsGen;
|
|
887
|
-
exports.createTestModel = createTestModel;
|
|
888
603
|
exports.getAdjacencyMap = getAdjacencyMap;
|
|
604
|
+
exports.getDescendantStateNodes = getDescendantStateNodes;
|
|
889
605
|
exports.getPathsFromEvents = getPathsFromEvents;
|
|
890
606
|
exports.getShortestPaths = getShortestPaths;
|
|
891
607
|
exports.getSimplePaths = getSimplePaths;
|
|
892
|
-
exports.getStateNodes = getStateNodes;
|
|
893
608
|
exports.joinPaths = joinPaths;
|
|
894
609
|
exports.serializeSnapshot = serializeSnapshot;
|
|
895
610
|
exports.toDirectedGraph = toDirectedGraph;
|