{"name":"@xstate/graph","version":"1.0.0-rc1","keywords":["state","machine","statechart","scxml","state","graph"],"author":{"name":"David Khourshid","email":"davidkpiano@gmail.com"},"license":"MIT","_id":"@xstate/graph@1.0.0-rc1","maintainers":[{"name":"davidkpiano","email":"davidkpiano@gmail.com"}],"homepage":"https://github.com/davidkpiano/xstate/tree/master/packages/xstate-test#readme","bugs":{"url":"https://github.com/davidkpiano/xstate/issues"},"dist":{"shasum":"e9be27f8803e05ea5d13bf2567cddf487cf434c8","tarball":"http://registry-npm.ywwl.com/@xstate/graph/download/@xstate/graph-1.0.0-rc1.tgz","fileCount":14,"integrity":"sha512-aOra7aDWge9aE/IIp3sBzs6hJd0QrPoBxFIexk/8zx+qMa5xlGLNPJU833voQbdT9pnWDYjkw/eFonOHN4OUKQ==","signatures":[{"sig":"MEUCIFkx6fxF0TIntrlhjS+PCqeYDJAd4BZM+gexKjmLPN1HAiEApQXBgUM4Is9A/INgCug2H9esOz7glptSXjJl61Mwlxw=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":37791,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdXdanCRA9TVsSAnZWagAAxVcP/1Yjf1uRnaxmUVYqS3P0\nJfsqFPd4RkfeQp8Ya8iTLVn7EA5OBifyukwke53x1rw2H0IIZSLkDzh82cQt\ndrU1doQsoZcwPSQQYfy108m7gwrJHkQNjfvx7CpaZqfL2IzPrMiqWCG+90EK\nBIYQ3ddO850IGKK22SXWKrLW/wW3XQ5IYd6WP73AE80/lgg6eHwPsm6xEIi5\nVoXe3OHqip+XmX46cgqVRGL6sXl3XlILZzQKDIrhGWaqJ7IOreG+RoCG4yUv\n2CuC9I1565gx5rij0/w8+EP5/WYsnHUFYyuIL2xTPY2FtyZECsBXi3QKbB3J\nUOrNEhNZEiTaFLs01Acuk4GyDntzQvanHqgSIjtbR+r2PQwg32vY+F7PxPLN\n59fwPL8YC/Hn+YGGat5XyytuKrCS414FcaBg7af7kOqGoHL3zq1Dq5gme1DJ\nlAhAYzVkGPRa8c8n3qr0eAx67WKnL09pfUeDixca7simZM/xbysgyx+li1nL\nyv2QgtospEiWzlZjIOYUpAV3/zTPBH2Ug0te0wDvihKZKqlSIPTjD0rXqmON\nss9CCARL0uEV09tOz9zqq46BehvQqkubHXG4n80oWjoIPwXp3++kV2H4SiAZ\nuF+W4IZP7F06bvdZXkSS2ABQBVCSMrZIItH60n6w+gp/XSgx0dCfObEA2it2\n6/z7\r\n=5Z4y\r\n-----END PGP SIGNATURE-----\r\n","size":9099,"noattachment":false,"key":"/@xstate/graph/-/@xstate/graph-1.0.0-rc1.tgz"},"main":"lib/index.js","readme":"# @xstate/graph\n\nThis package contains graph algorithms and utilities for XState machines.\n\n## Quick Start\n\n1. Install `xstate` and `@xstate/graph`:\n\n```bash\nnpm install xstate @xstate/graph\n```\n\n2. Import the graph utilities. Example:\n\n```js\nimport { Machine } from 'xstate';\nimport { simplePaths } from '@xstate/graph';\n\nconst machine = Machine(/* ... */);\nconst paths = simplePaths(machine);\n```\n\n## API\n\n### `getShortestPaths(machine, options?)`\n\n**Arguments**\n\n- `machine` - the [`Machine`](https://xstate.js.org/docs/guides/machines.html) to traverse\n- `options` (optional) - [options](#options) that customize how the algorithm will traverse the machine\n\nReturns the [shortest paths (Dijkstra's algorithm)](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) of a [machine](https://xstate.js.org/docs/guides/machines.html) from the initial state to every other state as a mapped object, where the:\n\n- **key** is the stringified state\n- **value** is an object with the properties:\n  - `state` - the target [`State`](https://xstate.js.org/docs/guides/states.html)\n  - `path` - the shortest path to get from the initial state to the target state\n\nThe `path` is an array of segments, where each segment is an object with the properties:\n\n- `state` - the [`State`](https://xstate.js.org/docs/guides/states.html) of the segment\n- `weight` - the total [weight](<https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)#Weighted_graph>) of the path\n  - Currently, each transition from one state to another has a weight of 1. This will be customizable in the future.\n- `event` - the event object that transitions the `machine` from the state to the next state in the path\n\nEvery path starts with the initial state.\n\nThe overall object structure looks like this:\n\n```json\n{\n  \"<SERIALIZED STATE>\": {\n    \"state\": State { ... },\n    \"path\": [\n      {\n        \"state\": State { ... },\n        \"event\": { \"type\": \"<event.type>\", \"<PROP>\": \"<event.PROP>\" }\n      },\n      {\n        \"state\": State { ... },\n        \"event\": { \"type\": \"<event.type>\", \"<PROP>\": \"<event.PROP>\" }\n      },\n      ...\n    ]\n  },\n  ...\n}\n```\n\n**Example**\n\n```js\nimport { Machine } from 'xstate';\nimport { getShortestPaths } from '@xstate/graph';\n\nconst feedbackMachine = Machine({\n  id: 'feedback',\n  initial: 'question',\n  states: {\n    question: {\n      on: {\n        CLICK_GOOD: 'thanks',\n        CLICK_BAD: 'form',\n        CLOSE: 'closed',\n        ESC: 'closed'\n      }\n    },\n    form: {\n      on: {\n        SUBMIT: 'thanks',\n        CLOSE: 'closed',\n        ESC: 'closed'\n      }\n    },\n    thanks: {\n      on: {\n        CLOSE: 'closed',\n        ESC: 'closed'\n      }\n    },\n    closed: {\n      type: 'final'\n    }\n  }\n});\n\nconst shortestPaths = getShortestPaths(feedbackMachine);\n\nconsole.log(shortestPaths);\n// => {\n//   '\"question\"': {\n//     state: State { value: 'question', context: undefined },\n//     weight: 0,\n//     path: []\n//   },\n//   '\"thanks\"': {\n//     state: State { value: 'thanks', context: undefined },\n//     weight: 1,\n//     path: [\n//       {\n//         state: State { value: 'question', context: undefined },\n//         event: { type: 'CLICK_GOOD' }\n//       }\n//     ]\n//   },\n//   '\"form\"': {\n//     state: State { value: 'form', context: undefined },\n//     weight: 1,\n//     path: [\n//       {\n//         state: State { value: 'question', context: undefined },\n//         event: { type: 'CLICK_BAD' }\n//       }\n//     ]\n//   },\n//   '\"closed\"': {\n//     state: State { value: 'closed', context: undefined },\n//     weight: 1,\n//     path: [\n//       {\n//         state: State { value: 'question', context: undefined },\n//         event: { type: 'CLOSE' }\n//       }\n//     ]\n//   }\n// };\n```\n\n### `getSimplePaths(machine, options?)`\n\n**Arguments**\n\n- `machine` - the [`Machine`](https://xstate.js.org/docs/guides/machines.html) to traverse\n- `options` (optional) - [options](#options) that customize how the algorithm will traverse the machine\n\nReturns the [simple paths](<https://en.wikipedia.org/wiki/Path_(graph_theory)#Definitions>) of a [machine](https://xstate.js.org/docs/guides/machines.html) as a mapped object, where the:\n\n- **key** is the stringified state\n- **value** is an object with the properties:\n  - `state` - the target [`State`](https://xstate.js.org/docs/guides/states.html)\n  - `paths` - the array of paths to get from the initial state to the target state\n\nEach `path` in `paths` is an array of segments, where each segment of the path is an object with the properties:\n\n- `state` - the [`State`](https://xstate.js.org/docs/guides/states.html) of the segment\n- `event` - the event object that transitions the `machine` from the state to the next state in the path\n\nEvery path starts with the initial state.\n\nThe overall object structure looks like this:\n\n```json\n{\n  \"<SERIALIZED STATE>\": {\n    \"state\": State { ... },\n    \"paths\": [\n      [\n        {\n          \"state\": State { ... },\n          \"event\": { \"type\": \"<event.type>\", \"<PROP>\": \"<event.PROP>\" }\n        },\n        {\n          \"state\": State { ... },\n          \"event\": { \"type\": \"<event.type>\", \"<PROP>\": \"<event.PROP>\" }\n        },\n        ...\n      ],\n      ...\n    ]\n  },\n  ...\n}\n```\n\n**Example**\n\n```js\nimport { Machine } from 'xstate';\nimport { getSimplePaths } from '@xstate/graph';\n\nconst feedbackMachine = Machine({\n  id: 'feedback',\n  initial: 'question',\n  states: {\n    question: {\n      on: {\n        CLICK_GOOD: 'thanks',\n        CLICK_BAD: 'form',\n        CLOSE: 'closed',\n        ESC: 'closed'\n      }\n    },\n    form: {\n      on: {\n        SUBMIT: 'thanks',\n        CLOSE: 'closed',\n        ESC: 'closed'\n      }\n    },\n    thanks: {\n      on: {\n        CLOSE: 'closed',\n        ESC: 'closed'\n      }\n    },\n    closed: {\n      type: 'final'\n    }\n  }\n});\n\nconst simplePaths = getSimplePaths(feedbackMachine);\n\nconsole.log(simplePaths);\n// => {\n//   '\"question\"': {\n//     state: { value: 'question', context: undefined },\n//     paths: [[]]\n//   },\n//   '\"thanks\"': {\n//     state: { value: 'thanks', context: undefined },\n//     paths: [\n//       [\n//         {\n//           state: { value: 'question', context: undefined },\n//           event: { type: 'CLICK_GOOD' }\n//         }\n//       ],\n//       [\n//         {\n//           state: { value: 'question', context: undefined },\n//           event: { type: 'CLICK_BAD' }\n//         },\n//         {\n//           state: { value: 'form', context: undefined },\n//           event: { type: 'SUBMIT' }\n//         }\n//       ]\n//     ]\n//   },\n//   '\"closed\"': {\n//     state: { value: 'closed', context: undefined },\n//     paths: [\n//       [\n//         {\n//           state: { value: 'question', context: undefined },\n//           event: { type: 'CLICK_GOOD' }\n//         },\n//         {\n//           state: { value: 'thanks', context: undefined },\n//           event: { type: 'CLOSE' }\n//         }\n//       ],\n//       [\n//         {\n//           state: { value: 'question', context: undefined },\n//           event: { type: 'CLICK_GOOD' }\n//         },\n//         {\n//           state: { value: 'thanks', context: undefined },\n//           event: { type: 'ESC' }\n//         }\n//       ],\n//       ...\n//     ]\n//   },\n//   ...\n// };\n```\n\n## Options\n\nOptions can be passed into `getShortestPaths` or `getSimplePaths` to customize how the graph represented by the machine should be traversed:\n\n- `events` - a mapping of event types to an array of event objects to be used for those events\n- `filter` - a function that determines whether a `state` should be traversed. If `false`, the traversal algorithm(s) will assume the state was \"seen\" and ignore traversing it.\n\n**Examples**\n\nIn the below example, the `INC` event is expanded to include two possible events, with `value: 1` and `value: 2` as the payload. It also ensures that the `state.context.count <= 5`; otherwise, this machine would be traversed infinitely.\n\n```js\nconst counterMachine = Machine({\n  id: 'counter',\n  initial: 'active',\n  context: { count: 0 },\n  states: {\n    active: {\n      on: {\n        INC: {\n          actions: assign({ count: (ctx, e) => ctx.count + e.value })\n        }\n      }\n    }\n  }\n});\n\nconst shortestPaths = getShortestPaths(counterMachine, {\n  events: {\n    INC: [{ type: 'INC', value: 1 }, { type: 'INC', value: 2 }]\n  },\n  filter: state => state.context.count <= 5\n});\n\nconsole.log(shortestPaths);\n// => {\n//   '\"active\" | {\"count\":0}': {\n//     state: { value: 'active', context: { count: 0 } },\n//     weight: 0,\n//     path: []\n//   },\n//   '\"active\" | {\"count\":1}': {\n//     state: { value: 'active', context: { count: 1 } },\n//     weight: 1,\n//     path: [\n//       {\n//         state: { value: 'active', context: { count: 0 } },\n//         event: { type: 'INC', value: 1 }\n//       }\n//     ]\n//   },\n//   '\"active\" | {\"count\":2}': {\n//     state: { value: 'active', context: { count: 2 } },\n//     weight: 1,\n//     path: [\n//       {\n//         state: { value: 'active', context: { count: 0 } },\n//         event: { type: 'INC', value: 2 }\n//       }\n//     ]\n//   },\n//   '\"active\" | {\"count\":3}': {\n//     state: { value: 'active', context: { count: 3 } },\n//     weight: 2,\n//     path: [\n//       {\n//         state: { value: 'active', context: { count: 0 } },\n//         event: { type: 'INC', value: 1 }\n//       },\n//       {\n//         state: { value: 'active', context: { count: 1 } },\n//         event: { type: 'INC', value: 2 }\n//       }\n//     ]\n//   },\n//   ...\n// };\n```\n","scripts":{"test":"jest","build":"tsc","clean":"rm -rf dist lib tsconfig.tsbuildinfo","prebuild":"npm run clean","prepublish":"npm run build && npm run test"},"_npmUser":{"name":"davidkpiano","email":"davidkpiano@gmail.com"},"repository":{"url":"git+ssh://git@github.com/davidkpiano/xstate.git","type":"git"},"_npmVersion":"6.4.1","description":"XState graph utilities","directories":{},"sideEffects":false,"_nodeVersion":"10.15.3","_hasShrinkwrap":false,"readmeFilename":"README.md","devDependencies":{"jest":"^24.8.0","ts-jest":"^24.0.2","typescript":"^3.5.3","@types/jest":"^24.0.11","lerna-alias":"3.0.3-0"},"peerDependencies":{"xstate":"^4.5.0"},"_npmOperationalInternal":{"tmp":"tmp/graph_1.0.0-rc1_1566430886625_0.3503880569716753","host":"s3://npm-registry-packages"},"publish_time":1566430886765,"_cnpm_publish_time":1566430886765,"dist-tags":{"alpha":"2.0.0-alpha.2","beta":"2.0.0-beta.6","latest":"3.0.4","next":"1.0.0-rc1.1"}}