@xstate/graph
Advanced tools
Comparing version 2.0.0-beta.3 to 2.0.0-beta.4
@@ -27,2 +27,2 @@ import { EventObject, AnyStateMachine, AnyState, StateFrom, EventFrom, AnyActorLogic, SnapshotFrom, EventFromLogic } from 'xstate'; | ||
export declare function resolveTraversalOptions<TLogic extends AnyActorLogic, TState extends SnapshotFrom<TLogic>, TEvent extends EventFromLogic<TLogic>>(logic: AnyActorLogic, traversalOptions?: TraversalOptions<TState, TEvent>, defaultOptions?: TraversalOptions<TState, TEvent>): TraversalConfig<TState, TEvent>; | ||
export declare function joinPaths<TState, TEvent extends EventObject>(path1: StatePath<TState, TEvent>, path2: StatePath<TState, TEvent>): StatePath<TState, TEvent>; | ||
export declare function joinPaths<TState, TEvent extends EventObject>(headPath: StatePath<TState, TEvent>, tailPath: StatePath<TState, TEvent>): StatePath<TState, TEvent>; |
@@ -72,9 +72,9 @@ import { EventObject, StateValue, StateNode, TransitionDefinition } from 'xstate'; | ||
/** | ||
* The current state before taking the event. | ||
* The event that resulted in the current state | ||
*/ | ||
state: TState; | ||
event: TEvent; | ||
/** | ||
* The event to be taken from the specified state. | ||
* The current state after taking the event. | ||
*/ | ||
event: TEvent; | ||
state: TState; | ||
} | ||
@@ -81,0 +81,0 @@ export type Steps<TState, TEvent extends EventObject> = Array<Step<TState, TEvent>>; |
@@ -155,11 +155,12 @@ 'use strict'; | ||
} | ||
function joinPaths(path1, path2) { | ||
const secondPathSource = path2.steps[0]?.state ?? path2.state; | ||
if (secondPathSource !== path1.state) { | ||
function joinPaths(headPath, tailPath) { | ||
const secondPathSource = tailPath.steps[0].state; | ||
if (secondPathSource !== headPath.state) { | ||
throw new Error(`Paths cannot be joined`); | ||
} | ||
return { | ||
state: path2.state, | ||
steps: path1.steps.concat(path2.steps), | ||
weight: path1.weight + path2.weight | ||
state: tailPath.state, | ||
// e.g. [A, B, C] + [C, D, E] = [A, B, C, D, E] | ||
steps: headPath.steps.concat(tailPath.steps.slice(1)), | ||
weight: headPath.weight + tailPath.weight | ||
}; | ||
@@ -232,2 +233,33 @@ } | ||
// TODO: rewrite parts of the algorithm leading to this to make this function obsolete | ||
function alterPath(path) { | ||
let steps = []; | ||
if (!path.steps.length) { | ||
steps = [{ | ||
state: path.state, | ||
event: { | ||
type: 'xstate.init' | ||
} | ||
}]; | ||
} else { | ||
for (let i = 0; i < path.steps.length; i++) { | ||
const step = path.steps[i]; | ||
steps.push({ | ||
state: step.state, | ||
event: i === 0 ? { | ||
type: 'xstate.init' | ||
} : path.steps[i - 1].event | ||
}); | ||
} | ||
steps.push({ | ||
state: path.state, | ||
event: path.steps[path.steps.length - 1].event | ||
}); | ||
} | ||
return { | ||
...path, | ||
steps | ||
}; | ||
} | ||
function getSimplePaths(logic, options) { | ||
@@ -246,3 +278,3 @@ const resolvedOptions = resolveTraversalOptions(logic, options); | ||
}; | ||
const path = []; | ||
const steps = []; | ||
const pathMap = {}; | ||
@@ -262,4 +294,4 @@ function util(fromStateSerial, toStateSerial) { | ||
state: fromState, | ||
weight: path.length, | ||
steps: [...path] | ||
weight: steps.length, | ||
steps: [...steps] | ||
}; | ||
@@ -281,3 +313,3 @@ toStatePlan.paths.push(path2); | ||
visitCtx.edges.add(serializedEvent); | ||
path.push({ | ||
steps.push({ | ||
state: stateMap.get(fromStateSerial), | ||
@@ -290,3 +322,3 @@ event: subEvent | ||
} | ||
path.pop(); | ||
steps.pop(); | ||
visitCtx.vertices.delete(fromStateSerial); | ||
@@ -301,5 +333,5 @@ } | ||
if (resolvedOptions.toState) { | ||
return simplePaths.filter(path => resolvedOptions.toState(path.state)); | ||
return simplePaths.filter(path => resolvedOptions.toState(path.state)).map(alterPath); | ||
} | ||
return simplePaths; | ||
return simplePaths.map(alterPath); | ||
} | ||
@@ -373,8 +405,9 @@ | ||
const state = stateMap.get(stateSerial); | ||
const steps = !fromState ? [] : statePlanMap[fromState].paths[0].steps.concat({ | ||
state: stateMap.get(fromState), | ||
event: fromEvent | ||
}); | ||
paths.push({ | ||
state, | ||
steps: !fromState ? [] : statePlanMap[fromState].paths[0].steps.concat({ | ||
state: stateMap.get(fromState), | ||
event: fromEvent | ||
}), | ||
steps, | ||
weight | ||
@@ -386,6 +419,3 @@ }); | ||
state, | ||
steps: !fromState ? [] : statePlanMap[fromState].paths[0].steps.concat({ | ||
state: stateMap.get(fromState), | ||
event: fromEvent | ||
}), | ||
steps, | ||
weight | ||
@@ -396,5 +426,5 @@ }] | ||
if (resolvedOptions.toState) { | ||
return paths.filter(path => resolvedOptions.toState(path.state)); | ||
return paths.filter(path => resolvedOptions.toState(path.state)).map(alterPath); | ||
} | ||
return paths; | ||
return paths.map(alterPath); | ||
} | ||
@@ -450,7 +480,7 @@ | ||
} | ||
return [{ | ||
return [alterPath({ | ||
state, | ||
steps, | ||
weight: steps.length | ||
}]; | ||
})]; | ||
} | ||
@@ -457,0 +487,0 @@ |
@@ -151,11 +151,12 @@ import { StateMachine } from 'xstate'; | ||
} | ||
function joinPaths(path1, path2) { | ||
const secondPathSource = path2.steps[0]?.state ?? path2.state; | ||
if (secondPathSource !== path1.state) { | ||
function joinPaths(headPath, tailPath) { | ||
const secondPathSource = tailPath.steps[0].state; | ||
if (secondPathSource !== headPath.state) { | ||
throw new Error(`Paths cannot be joined`); | ||
} | ||
return { | ||
state: path2.state, | ||
steps: path1.steps.concat(path2.steps), | ||
weight: path1.weight + path2.weight | ||
state: tailPath.state, | ||
// e.g. [A, B, C] + [C, D, E] = [A, B, C, D, E] | ||
steps: headPath.steps.concat(tailPath.steps.slice(1)), | ||
weight: headPath.weight + tailPath.weight | ||
}; | ||
@@ -228,2 +229,33 @@ } | ||
// TODO: rewrite parts of the algorithm leading to this to make this function obsolete | ||
function alterPath(path) { | ||
let steps = []; | ||
if (!path.steps.length) { | ||
steps = [{ | ||
state: path.state, | ||
event: { | ||
type: 'xstate.init' | ||
} | ||
}]; | ||
} else { | ||
for (let i = 0; i < path.steps.length; i++) { | ||
const step = path.steps[i]; | ||
steps.push({ | ||
state: step.state, | ||
event: i === 0 ? { | ||
type: 'xstate.init' | ||
} : path.steps[i - 1].event | ||
}); | ||
} | ||
steps.push({ | ||
state: path.state, | ||
event: path.steps[path.steps.length - 1].event | ||
}); | ||
} | ||
return { | ||
...path, | ||
steps | ||
}; | ||
} | ||
function getSimplePaths(logic, options) { | ||
@@ -242,3 +274,3 @@ const resolvedOptions = resolveTraversalOptions(logic, options); | ||
}; | ||
const path = []; | ||
const steps = []; | ||
const pathMap = {}; | ||
@@ -258,4 +290,4 @@ function util(fromStateSerial, toStateSerial) { | ||
state: fromState, | ||
weight: path.length, | ||
steps: [...path] | ||
weight: steps.length, | ||
steps: [...steps] | ||
}; | ||
@@ -277,3 +309,3 @@ toStatePlan.paths.push(path2); | ||
visitCtx.edges.add(serializedEvent); | ||
path.push({ | ||
steps.push({ | ||
state: stateMap.get(fromStateSerial), | ||
@@ -286,3 +318,3 @@ event: subEvent | ||
} | ||
path.pop(); | ||
steps.pop(); | ||
visitCtx.vertices.delete(fromStateSerial); | ||
@@ -297,5 +329,5 @@ } | ||
if (resolvedOptions.toState) { | ||
return simplePaths.filter(path => resolvedOptions.toState(path.state)); | ||
return simplePaths.filter(path => resolvedOptions.toState(path.state)).map(alterPath); | ||
} | ||
return simplePaths; | ||
return simplePaths.map(alterPath); | ||
} | ||
@@ -369,8 +401,9 @@ | ||
const state = stateMap.get(stateSerial); | ||
const steps = !fromState ? [] : statePlanMap[fromState].paths[0].steps.concat({ | ||
state: stateMap.get(fromState), | ||
event: fromEvent | ||
}); | ||
paths.push({ | ||
state, | ||
steps: !fromState ? [] : statePlanMap[fromState].paths[0].steps.concat({ | ||
state: stateMap.get(fromState), | ||
event: fromEvent | ||
}), | ||
steps, | ||
weight | ||
@@ -382,6 +415,3 @@ }); | ||
state, | ||
steps: !fromState ? [] : statePlanMap[fromState].paths[0].steps.concat({ | ||
state: stateMap.get(fromState), | ||
event: fromEvent | ||
}), | ||
steps, | ||
weight | ||
@@ -392,5 +422,5 @@ }] | ||
if (resolvedOptions.toState) { | ||
return paths.filter(path => resolvedOptions.toState(path.state)); | ||
return paths.filter(path => resolvedOptions.toState(path.state)).map(alterPath); | ||
} | ||
return paths; | ||
return paths.map(alterPath); | ||
} | ||
@@ -446,9 +476,9 @@ | ||
} | ||
return [{ | ||
return [alterPath({ | ||
state, | ||
steps, | ||
weight: steps.length | ||
}]; | ||
})]; | ||
} | ||
export { getAdjacencyMap, getPathsFromEvents, getShortestPaths, getSimplePaths, getStateNodes, joinPaths, serializeEvent, serializeMachineState as serializeState, toDirectedGraph }; |
{ | ||
"name": "@xstate/graph", | ||
"version": "2.0.0-beta.3", | ||
"version": "2.0.0-beta.4", | ||
"description": "XState graph utilities", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
40101
1125
0