{"name":"@xstate/graph","version":"1.0.0-rc1.1","keywords":["state","machine","statechart","scxml","state","graph"],"author":{"name":"David Khourshid","email":"davidkpiano@gmail.com"},"license":"MIT","_id":"@xstate/graph@1.0.0-rc1.1","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":"02075ecf32720d84f71b58beb1aa5fba67e73f2a","tarball":"http://registry-npm.ywwl.com/@xstate/graph/download/@xstate/graph-1.0.0-rc1.1.tgz","fileCount":14,"integrity":"sha512-ywxKr3jWECMZ+Y1kJI9PNB5HzHmAAb1NWgQFmnN/q9F3UgepNlALOI1SjP+Q3ZyDSMl6NrvPSS4e9zIkbPAJqg==","signatures":[{"sig":"MEQCICIfXHNQnit5F+ZdjPX2IT5u9O53IeZCIWd39FSybLn7AiAlxS1xWDoPwKUdPoaaZoPPoFokdxN1Un0Zz2VHGo10OA==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":38381,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdXrC0CRA9TVsSAnZWagAAuK8QAIKhvX4Zy7gCBR19mVex\nWzidfe+uJI3Frii9arz4Qe/VUP+/0+S0FGv3Qm+kxrO1RqzccVRRaSgRvpUC\nGW3A+guHt15Wq9rrtMxF0C4aQ7Tr64ehT2Phao/7YWsz8RWGKnwTEcTKM/r0\nUjCXkCP4PMuFDfxvXQzkMlERvUiRqY/QGFrc1iEHV4CsDXLiAE5DeKkUHYyg\nIDf7oyhYId4FgXHgmKcuiiWXOsYXLMyt1QU+ie4XLZ70SaV9jNjd98xOvvUi\nneu91JscloFd31t6vGXLAXOZE0b/giYAXj9mXJ+2TBSRnaIJw45S33KfnXF7\nLI/LGOuuFXyJnQqOTmGTKa7Bv2JdAfzwTEhZBvt4cPcgO8kl/qQsQ0gaW77B\nDTf8MTAKhT68TsYYmpoIRokSJ0IVTa4OS6EF2OVHSPyAp+jw/Uw9NSXt8x8a\neJbJL/+yCWxKP/qdAhHy2hN//4CbkJnd2qpDjteur8rvJ5H+jQLaejl8aF+b\nhtwpcO4e8ez12uXo/BPFXwWVGbcISkE2lLhMw2MxH73DAcmgXETV7pA3NsrY\ntxf+llKKD8MT56z88LZvOTbzU9oVz2J/SV6SDILiWnYYxHemK2sMAUFpJiKR\ndZ83jRS1Lobbh0Hdom3EJ2kSDdFLVfO7WrtRS03sWi8P35BesMxtc0hrIj2Z\nXiGR\r\n=zEw5\r\n-----END PGP SIGNATURE-----\r\n","size":9223,"noattachment":false,"key":"/@xstate/graph/-/@xstate/graph-1.0.0-rc1.1.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","dependencies":{},"_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.7.0"},"_npmOperationalInternal":{"tmp":"tmp/graph_1.0.0-rc1.1_1566486708188_0.4961059699244017","host":"s3://npm-registry-packages"},"publish_time":1566486708290,"_cnpm_publish_time":1566486708290,"dist-tags":{"alpha":"2.0.0-alpha.2","beta":"2.0.0-beta.6","latest":"3.0.4","next":"1.0.0-rc1.1"}}