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