Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@xstate/graph

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@xstate/graph - npm Package Compare versions

Comparing version 0.2.1 to 1.0.0-rc1

lib/graph.d.ts.map

49

lib/graph.d.ts

@@ -1,34 +0,8 @@

import { StateNode, State } from 'xstate';
import { Edge, AdjacencyMap, DefaultContext, ValueAdjacencyMap, EventObject, StateMachine } from 'xstate/lib/types';
export interface PathsItem<TContext, TEvent extends EventObject> {
state: State<TContext, TEvent>;
paths: Array<Array<Segment<TContext, TEvent>>>;
}
export interface PathsMap<TContext, TEvent extends EventObject> {
[key: string]: PathsItem<TContext, TEvent>;
}
export interface Segment<TContext, TEvent extends EventObject> {
/**
* From state.
*/
state: State<TContext, TEvent>;
/**
* Event from state.
*/
event: TEvent;
}
export interface PathItem<TContext, TEvent extends EventObject> {
state: State<TContext, TEvent>;
path: Array<Segment<TContext, TEvent>>;
weight?: number;
}
export interface PathMap<TContext, TEvent extends EventObject> {
[key: string]: PathItem<TContext, TEvent>;
}
export declare function getNodes(node: StateNode): StateNode[];
export declare function getEventEdges<TContext = DefaultContext, TEvent extends EventObject = EventObject>(node: StateNode<TContext>, event: string): Array<Edge<TContext, TEvent>>;
export declare function getEdges<TContext = DefaultContext, TEvent extends EventObject = EventObject>(node: StateNode<TContext>, options?: {
depth: null | number;
}): Array<Edge<TContext, TEvent>>;
export declare function adjacencyMap<TContext = DefaultContext>(node: StateNode<TContext>, context?: TContext): AdjacencyMap;
import { StateNode, State, DefaultContext, EventObject, StateMachine } from 'xstate';
import { StatePathsMap, StatePaths, AdjacencyMap } from './types';
/**
* Returns all state nodes of the given `node`.
* @param stateNode State node to recursively get child state nodes from
*/
export declare function getStateNodes(stateNode: StateNode): StateNode[];
export declare function serializeState<TContext>(state: State<TContext>): string;

@@ -47,5 +21,6 @@ export declare function serializeEvent<TEvent extends EventObject>(event: TEvent): string;

}
export declare function getValueAdjacencyMap<TContext = DefaultContext, TEvent extends EventObject = EventObject>(node: StateNode<TContext, any, TEvent>, options?: Partial<ValueAdjMapOptions<TContext, TEvent>>): ValueAdjacencyMap<TContext, TEvent>;
export declare function getShortestPaths<TContext = DefaultContext, TEvent extends EventObject = EventObject>(machine: StateMachine<TContext, any, TEvent>, options?: Partial<ValueAdjMapOptions<TContext, TEvent>>): PathMap<TContext, TEvent>;
export declare function getSimplePaths<TContext = DefaultContext, TEvent extends EventObject = EventObject>(machine: StateMachine<TContext, any, TEvent>, options?: Partial<ValueAdjMapOptions<TContext, TEvent>>): PathsMap<TContext, TEvent>;
export declare function getSimplePathsAsArray<TContext = DefaultContext, TEvent extends EventObject = EventObject>(machine: StateNode<TContext, any, TEvent>, options?: ValueAdjMapOptions<TContext, TEvent>): Array<PathsItem<TContext, TEvent>>;
export declare function getAdjacencyMap<TContext = DefaultContext, TEvent extends EventObject = EventObject>(node: StateNode<TContext, any, TEvent>, options?: Partial<ValueAdjMapOptions<TContext, TEvent>>): AdjacencyMap<TContext, TEvent>;
export declare function getShortestPaths<TContext = DefaultContext, TEvent extends EventObject = EventObject>(machine: StateMachine<TContext, any, TEvent>, options?: Partial<ValueAdjMapOptions<TContext, TEvent>>): StatePathsMap<TContext, TEvent>;
export declare function getSimplePaths<TContext = DefaultContext, TEvent extends EventObject = EventObject>(machine: StateMachine<TContext, any, TEvent>, options?: Partial<ValueAdjMapOptions<TContext, TEvent>>): StatePathsMap<TContext, TEvent>;
export declare function getSimplePathsAsArray<TContext = DefaultContext, TEvent extends EventObject = EventObject>(machine: StateNode<TContext, any, TEvent>, options?: ValueAdjMapOptions<TContext, TEvent>): Array<StatePaths<TContext, TEvent>>;
//# sourceMappingURL=graph.d.ts.map

@@ -45,10 +45,13 @@ "use strict";

var utils_1 = require("xstate/lib/utils");
var actions_1 = require("xstate/lib/actions");
var EMPTY_MAP = {};
function getNodes(node) {
var states = node.states;
/**
* Returns all state nodes of the given `node`.
* @param stateNode State node to recursively get child state nodes from
*/
function getStateNodes(stateNode) {
var states = stateNode.states;
var nodes = utils_1.keys(states).reduce(function (accNodes, stateKey) {
var subState = states[stateKey];
var subNodes = getNodes(states[stateKey]);
accNodes.push.apply(accNodes, __spread([subState], subNodes));
var childStateNode = states[stateKey];
var childStateNodes = getStateNodes(childStateNode);
accNodes.push.apply(accNodes, __spread([childStateNode], childStateNodes));
return accNodes;

@@ -58,130 +61,3 @@ }, []);

}
exports.getNodes = getNodes;
function getEventEdges(node, event) {
var transitions = node.definition.on[event];
return utils_1.flatten(transitions.map(function (transition) {
var targets = transition.target
? [].concat(transition.target)
: undefined;
if (!targets) {
return [
{
source: node,
target: node,
event: event,
actions: transition.actions
? transition.actions.map(utils_1.getActionType)
: [],
cond: transition.cond,
transition: transition
}
];
}
return targets
.map(function (target) {
try {
var targetNode = target
? node.getRelativeStateNodes(target, undefined, false)[0]
: node;
return {
source: node,
target: targetNode,
event: event,
actions: transition.actions
? transition.actions.map(utils_1.getActionType)
: [],
cond: transition.cond,
transition: transition
};
}
catch (e) {
// tslint:disable-next-line:no-console
utils_1.warn(e, "Target '" + target + "' not found on '" + node.id + "'");
return undefined;
}
})
.filter(function (maybeEdge) { return maybeEdge !== undefined; });
}));
}
exports.getEventEdges = getEventEdges;
function getEdges(node, options) {
var e_1, _a, e_2, _b, e_3, _c;
var _d = (options || {}).depth, depth = _d === void 0 ? null : _d;
var edges = [];
if (node.states && depth === null) {
try {
for (var _e = __values(utils_1.keys(node.states)), _f = _e.next(); !_f.done; _f = _e.next()) {
var stateKey = _f.value;
edges.push.apply(edges, __spread(getEdges(node.states[stateKey])));
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
}
finally { if (e_1) throw e_1.error; }
}
}
else if (depth && depth > 0) {
try {
for (var _g = __values(utils_1.keys(node.states)), _h = _g.next(); !_h.done; _h = _g.next()) {
var stateKey = _h.value;
edges.push.apply(edges, __spread(getEdges(node.states[stateKey], { depth: depth - 1 })));
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
}
finally { if (e_2) throw e_2.error; }
}
}
try {
for (var _j = __values(utils_1.keys(node.on)), _k = _j.next(); !_k.done; _k = _j.next()) {
var event_1 = _k.value;
edges.push.apply(edges, __spread(getEventEdges(node, event_1)));
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (_k && !_k.done && (_c = _j.return)) _c.call(_j);
}
finally { if (e_3) throw e_3.error; }
}
return edges;
}
exports.getEdges = getEdges;
function adjacencyMap(node, context) {
var adjacency = {};
var events = node.events;
function findAdjacencies(stateValue) {
var e_4, _a;
var stateKey = JSON.stringify(stateValue);
if (adjacency[stateKey]) {
return;
}
adjacency[stateKey] = {};
try {
for (var events_1 = __values(events), events_1_1 = events_1.next(); !events_1_1.done; events_1_1 = events_1.next()) {
var event_2 = events_1_1.value;
var nextState = node.transition(stateValue, event_2, context);
adjacency[stateKey][event_2] = { state: nextState.value };
findAdjacencies(nextState.value);
}
}
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {
try {
if (events_1_1 && !events_1_1.done && (_a = events_1.return)) _a.call(events_1);
}
finally { if (e_4) throw e_4.error; }
}
}
findAdjacencies(node.initialState.value);
return adjacency;
}
exports.adjacencyMap = adjacencyMap;
exports.getStateNodes = getStateNodes;
function serializeState(state) {

@@ -208,4 +84,4 @@ var value = state.value, context = state.context;

};
function getValueAdjacencyMap(node, options) {
var e_5, _a;
function getAdjacencyMap(node, options) {
var e_1, _a;
var optionsWithDefaults = __assign({}, defaultValueAdjMapOptions, options);

@@ -216,7 +92,7 @@ var filter = optionsWithDefaults.filter, stateSerializer = optionsWithDefaults.stateSerializer, eventSerializer = optionsWithDefaults.eventSerializer;

for (var _b = __values(node.events), _c = _b.next(); !_c.done; _c = _b.next()) {
var event_3 = _c.value;
events[event_3] = [event_3];
var event_1 = _c.value;
events[event_1] = [event_1];
}
}
catch (e_5_1) { e_5 = { error: e_5_1 }; }
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {

@@ -226,3 +102,3 @@ try {

}
finally { if (e_5) throw e_5.error; }
finally { if (e_1) throw e_1.error; }
}

@@ -232,3 +108,3 @@ Object.assign(events, optionsWithDefaults.events);

function findAdjacencies(state) {
var e_6, _a;
var e_2, _a;
var nextEvents = state.nextEvents;

@@ -240,16 +116,19 @@ var stateHash = stateSerializer(state);

adjacency[stateHash] = {};
var potentialEvents = utils_1.flatten(nextEvents.map(function (nextEvent) { return events[nextEvent] || []; })).map(function (event) { return actions_1.toEventObject(event); });
var potentialEvents = utils_1.flatten(nextEvents.map(function (nextEvent) { return events[nextEvent] || []; })).map(function (event) { return utils_1.toEventObject(event); });
try {
for (var potentialEvents_1 = __values(potentialEvents), potentialEvents_1_1 = potentialEvents_1.next(); !potentialEvents_1_1.done; potentialEvents_1_1 = potentialEvents_1.next()) {
var event_4 = potentialEvents_1_1.value;
var event_2 = potentialEvents_1_1.value;
var nextState = void 0;
try {
nextState = node.transition(state, event_4);
nextState = node.transition(state, event_2);
}
catch (e) {
throw new Error("Unable to transition from state " + stateSerializer(state) + " on event " + eventSerializer(event_4) + ": " + e.message);
throw new Error("Unable to transition from state " + stateSerializer(state) + " on event " + eventSerializer(event_2) + ": " + e.message);
}
if ((!filter || filter(nextState)) &&
stateHash !== stateSerializer(nextState)) {
adjacency[stateHash][eventSerializer(event_4)] = nextState;
adjacency[stateHash][eventSerializer(event_2)] = {
state: nextState,
event: event_2
};
findAdjacencies(nextState);

@@ -259,3 +138,3 @@ }

}
catch (e_6_1) { e_6 = { error: e_6_1 }; }
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {

@@ -265,3 +144,3 @@ try {

}
finally { if (e_6) throw e_6.error; }
finally { if (e_2) throw e_2.error; }
}

@@ -272,5 +151,5 @@ }

}
exports.getValueAdjacencyMap = getValueAdjacencyMap;
exports.getAdjacencyMap = getAdjacencyMap;
function getShortestPaths(machine, options) {
var e_7, _a, e_8, _b;
var e_3, _a, e_4, _b;
if (!machine.states) {

@@ -281,3 +160,3 @@ // return EMPTY_MAP;

var optionsWithDefaults = __assign({ events: {}, stateSerializer: serializeState, eventSerializer: serializeEvent }, options);
var adjacency = getValueAdjacencyMap(machine, optionsWithDefaults);
var adjacency = getAdjacencyMap(machine, optionsWithDefaults);
// weight, state, event

@@ -294,13 +173,13 @@ var weightMap = new Map();

try {
for (var unvisited_1 = __values(unvisited), unvisited_1_1 = unvisited_1.next(); !unvisited_1_1.done; unvisited_1_1 = unvisited_1.next()) {
for (var unvisited_1 = (e_3 = void 0, __values(unvisited)), unvisited_1_1 = unvisited_1.next(); !unvisited_1_1.done; unvisited_1_1 = unvisited_1.next()) {
var vertex = unvisited_1_1.value;
var _c = __read(weightMap.get(vertex), 1), weight = _c[0];
try {
for (var _d = __values(utils_1.keys(adjacency[vertex])), _e = _d.next(); !_e.done; _e = _d.next()) {
var event_5 = _e.value;
var nextState = adjacency[vertex][event_5];
var nextVertex = optionsWithDefaults.stateSerializer(nextState);
stateMap.set(nextVertex, nextState);
for (var _d = (e_4 = void 0, __values(utils_1.keys(adjacency[vertex]))), _e = _d.next(); !_e.done; _e = _d.next()) {
var event_3 = _e.value;
var nextSegment = adjacency[vertex][event_3];
var nextVertex = optionsWithDefaults.stateSerializer(nextSegment.state);
stateMap.set(nextVertex, nextSegment.state);
if (!weightMap.has(nextVertex)) {
weightMap.set(nextVertex, [weight + 1, vertex, event_5]);
weightMap.set(nextVertex, [weight + 1, vertex, event_3]);
}

@@ -310,3 +189,3 @@ else {

if (nextWeight > weight + 1) {
weightMap.set(nextVertex, [weight + 1, vertex, event_5]);
weightMap.set(nextVertex, [weight + 1, vertex, event_3]);
}

@@ -319,3 +198,3 @@ }

}
catch (e_8_1) { e_8 = { error: e_8_1 }; }
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {

@@ -325,3 +204,3 @@ try {

}
finally { if (e_8) throw e_8.error; }
finally { if (e_4) throw e_4.error; }
}

@@ -332,3 +211,3 @@ visited.add(vertex);

}
catch (e_7_1) { e_7 = { error: e_7_1 }; }
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {

@@ -338,88 +217,36 @@ try {

}
finally { if (e_7) throw e_7.error; }
finally { if (e_3) throw e_3.error; }
}
}
var pathMap = {};
var statePathMap = {};
weightMap.forEach(function (_a, stateSerial) {
var _b = __read(_a, 3), weight = _b[0], fromState = _b[1], fromEvent = _b[2];
pathMap[stateSerial] = {
state: stateMap.get(stateSerial),
weight: weight,
path: (function () {
if (!fromState) {
return [];
}
return pathMap[fromState].path.concat({
state: stateMap.get(fromState),
event: deserializeEventString(fromEvent)
});
})()
var state = stateMap.get(stateSerial);
statePathMap[stateSerial] = {
state: state,
paths: !fromState
? [
{
state: state,
segments: [],
weight: weight
}
]
: [
{
state: state,
segments: statePathMap[fromState].paths[0].segments.concat({
state: stateMap.get(fromState),
event: deserializeEventString(fromEvent)
}),
weight: weight
}
]
};
});
return pathMap;
return statePathMap;
}
exports.getShortestPaths = getShortestPaths;
// export function getShortestValuePaths<
// TContext = DefaultContext,
// TEvent extends EventObject = EventObject
// >(
// machine: StateNode<TContext, any, TEvent>,
// options: ValueAdjMapOptions<TContext, TEvent> = defaultValueAdjMapOptions
// ): PathMap<TContext, TEvent> {
// if (!machine.states) {
// return EMPTY_MAP;
// }
// const adjacency = getValueAdjacencyMap<TContext, TEvent>(machine, options);
// const pathMap: PathMap<TContext, TEvent> = {};
// const visited: Set<string> = new Set();
// function util(state: State<TContext>): PathMap<TContext, TEvent> {
// const stateKey = serializeState(state);
// visited.add(stateKey);
// const eventMap = adjacency[stateKey];
// for (const eventType of keys(eventMap)) {
// const { value, context } = eventMap[eventType];
// if (!value) {
// continue;
// }
// const nextState = State.from(value, context);
// const nextStateId = serializeState(nextState);
// if (
// !pathMap[nextStateId] ||
// pathMap[nextStateId].length > pathMap[stateKey].length + 1
// ) {
// pathMap[nextStateId] = [
// ...(pathMap[stateKey] || []),
// {
// state: { value, context: state.context },
// event: deserializeEventString(eventType) as TEvent
// }
// ];
// }
// if (visited.has(nextStateId)) {
// continue;
// }
// util(nextState);
// }
// return pathMap;
// }
// util(machine.initialState);
// return pathMap;
// }
// export function getShortestPathsAsArray<
// TContext = DefaultContext,
// TEvent extends EventObject = EventObject
// >(
// machine: StateNode<TContext, any, TEvent>
// ): Array<PathItem<TContext, TEvent>> {
// const result = getShortestValuePaths<TContext, TEvent>(
// machine,
// defaultValueAdjMapOptions
// );
// return keys(result).map(key => ({
// state: JSON.parse(key),
// path: result[key]
// }));
// }
function getSimplePaths(machine, options) {
var e_9, _a;
var e_5, _a;
var optionsWithDefaults = __assign({}, defaultValueAdjMapOptions, options);

@@ -430,3 +257,3 @@ var stateSerializer = optionsWithDefaults.stateSerializer;

}
var adjacency = getValueAdjacencyMap(machine, optionsWithDefaults);
var adjacency = getAdjacencyMap(machine, optionsWithDefaults);
var stateMap = new Map();

@@ -436,4 +263,5 @@ var visited = new Set();

var paths = {};
function util(fromStateSerial, toStateSerial) {
var e_10, _a;
function util(fromState, toStateSerial) {
var e_6, _a;
var fromStateSerial = stateSerializer(fromState);
visited.add(fromStateSerial);

@@ -447,3 +275,7 @@ if (fromStateSerial === toStateSerial) {

}
paths[toStateSerial].paths.push(__spread(path));
paths[toStateSerial].paths.push({
state: fromState,
weight: path.length,
segments: __spread(path)
});
}

@@ -454,8 +286,8 @@ else {

var subEvent = _c.value;
var nextState = adjacency[fromStateSerial][subEvent];
if (!nextState) {
var nextSegment = adjacency[fromStateSerial][subEvent];
if (!nextSegment) {
continue;
}
var nextStateSerial = stateSerializer(nextState);
stateMap.set(nextStateSerial, nextState);
var nextStateSerial = stateSerializer(nextSegment.state);
stateMap.set(nextStateSerial, nextSegment.state);
if (!visited.has(nextStateSerial)) {

@@ -466,7 +298,7 @@ path.push({

});
util(nextStateSerial, toStateSerial);
util(nextSegment.state, toStateSerial);
}
}
}
catch (e_10_1) { e_10 = { error: e_10_1 }; }
catch (e_6_1) { e_6 = { error: e_6_1 }; }
finally {

@@ -476,3 +308,3 @@ try {

}
finally { if (e_10) throw e_10.error; }
finally { if (e_6) throw e_6.error; }
}

@@ -488,6 +320,6 @@ }

var nextStateSerial = _c.value;
util(initialStateSerial, nextStateSerial);
util(machine.initialState, nextStateSerial);
}
}
catch (e_9_1) { e_9 = { error: e_9_1 }; }
catch (e_5_1) { e_5 = { error: e_5_1 }; }
finally {

@@ -497,3 +329,3 @@ try {

}
finally { if (e_9) throw e_9.error; }
finally { if (e_5) throw e_5.error; }
}

@@ -508,1 +340,2 @@ return paths;

exports.getSimplePathsAsArray = getSimplePathsAsArray;
//# sourceMappingURL=graph.js.map

@@ -1,2 +0,3 @@

import { getNodes, getEdges, getSimplePaths, getShortestPaths, serializeEvent, serializeState, adjacencyMap } from './graph';
export { adjacencyMap, getNodes, getEdges, getSimplePaths, getShortestPaths, serializeEvent, serializeState };
import { getStateNodes, getSimplePaths, getShortestPaths, serializeEvent, serializeState } from './graph';
export { getStateNodes, getSimplePaths, getShortestPaths, serializeEvent, serializeState };
//# sourceMappingURL=index.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var graph_1 = require("./graph");
exports.getNodes = graph_1.getNodes;
exports.getEdges = graph_1.getEdges;
exports.getStateNodes = graph_1.getStateNodes;
exports.getSimplePaths = graph_1.getSimplePaths;

@@ -10,2 +9,2 @@ exports.getShortestPaths = graph_1.getShortestPaths;

exports.serializeState = graph_1.serializeState;
exports.adjacencyMap = graph_1.adjacencyMap;
//# sourceMappingURL=index.js.map
{
"name": "@xstate/graph",
"version": "0.2.1",
"version": "1.0.0-rc1",
"description": "XState graph utilities",

@@ -17,2 +17,3 @@ "keywords": [

"main": "lib/index.js",
"sideEffects": false,
"files": [

@@ -26,4 +27,7 @@ "lib"

"scripts": {
"test": "tsc && mocha --require ts-node/register test/**.ts test/**/*.test.ts",
"prepublish": "tsc && npm run test"
"clean": "rm -rf dist lib tsconfig.tsbuildinfo",
"prebuild": "npm run clean",
"build": "tsc",
"test": "jest",
"prepublish": "npm run build && npm run test"
},

@@ -33,21 +37,12 @@ "bugs": {

},
"dependencies": {
"peerDependencies": {
"xstate": "^4.5.0"
},
"devDependencies": {
"@babel/preset-typescript": "^7.3.3",
"@types/jest": "^24.0.11",
"@types/jsdom": "^12.2.3",
"@types/react": "^16.8.8",
"@types/react-dom": "^16.8.2",
"jest": "^24.5.0",
"jsdom": "^14.0.0",
"jsdom-global": "^3.0.2",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"react-testing-library": "^6.0.1",
"ts-jest": "^24.0.0",
"ts-node": "^8.0.2",
"typescript": "^3.3.3"
"jest": "^24.8.0",
"lerna-alias": "3.0.3-0",
"ts-jest": "^24.0.2",
"typescript": "^3.5.3"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc