{"_id":"nanostate","_rev":"9-cac3c6ec68f087aaf3cafb8751b4401a","name":"nanostate","description":"Small Finite State Machine implementation","dist-tags":{"latest":"1.2.1"},"versions":{"1.0.0":{"name":"nanostate","description":"Small Finite State Machine implementation","repository":{"type":"git","url":"git+https://github.com/yoshuawuyts/nanostate.git"},"version":"1.0.0","scripts":{"deps":"dependency-check . && dependency-check . --extra --no-dev","start":"node .","test":"standard && npm run deps"},"dependencies":{},"devDependencies":{"dependency-check":"^2.9.1","standard":"^10.0.3","tape":"^4.8.0"},"keywords":["fsm","finite","state","machine","automata","small","minimal","browser","fast","history","concurrent","chart","charts"],"license":"Apache-2.0","gitHead":"b717503557955c9cb669c27d52d3e6ff58a282aa","bugs":{"url":"https://github.com/yoshuawuyts/nanostate/issues"},"homepage":"https://github.com/yoshuawuyts/nanostate#readme","_id":"nanostate@1.0.0","_npmVersion":"5.4.2","_nodeVersion":"8.8.1","_npmUser":{"name":"yoshuawuyts","email":"i@yoshuawuyts.com"},"dist":{"integrity":"sha512-PQIC9rIYKQsE76dfZgcu99j9VmX4CR0HJSososZaFbS42bO9/p/tTWFLu/rPfXpWCYaKpR/6Dsz7dNKZml61Kg==","shasum":"b28b6d0aedb0db7ac3f9476997d3bb4b7313fc28","tarball":"https://registry.npmjs.org/nanostate/-/nanostate-1.0.0.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDDqo39HflY98+0hchfiLUlKYD4CZK5G0sq2KAGd3DTqAIgITK1aOs7bOF5rG3/x/gkFSurpPjybZpamPw/qVt6reM="}]},"maintainers":[{"name":"yoshuawuyts","email":"i@yoshuawuyts.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nanostate-1.0.0.tgz_1510773564977_0.899334313115105"},"directories":{}},"1.0.1":{"name":"nanostate","description":"Small Finite State Machine implementation","repository":{"type":"git","url":"git+https://github.com/choojs/nanostate.git"},"version":"1.0.1","scripts":{"start":"node .","test":"standard && node test"},"dependencies":{"nanoassert":"^1.1.0","nanobus":"^4.3.1"},"devDependencies":{"dependency-check":"^2.9.1","standard":"^10.0.3","tape":"^4.8.0"},"keywords":["fsm","finite","state","machine","automata","small","minimal","browser","fast","history","concurrent","chart","charts"],"license":"Apache-2.0","gitHead":"7bdaa48c0410df87f59c1c792e83abb21c309741","bugs":{"url":"https://github.com/choojs/nanostate/issues"},"homepage":"https://github.com/choojs/nanostate#readme","_id":"nanostate@1.0.1","_npmVersion":"5.4.2","_nodeVersion":"8.8.1","_npmUser":{"name":"yoshuawuyts","email":"i@yoshuawuyts.com"},"dist":{"integrity":"sha512-ND8csRt6ol1J+81Haihvu3fi594YIPs0BR9MbRW6BFfSrqCPsqG0g2sAQQ3ayhzxGmwNf3f6Se5wfUeQLvbT8Q==","shasum":"2e0ceb85976a4791aad199543d9f4b5ad569f22d","tarball":"https://registry.npmjs.org/nanostate/-/nanostate-1.0.1.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDZn49J3P6Li6XahBvAwpT+KLfapE6759jB/YUQjNCImAIhANzfohZecP0MKY7LNlbtSRJZACtXTy36MkKDNDLOTRb5"}]},"maintainers":[{"name":"yoshuawuyts","email":"i@yoshuawuyts.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nanostate-1.0.1.tgz_1511643673420_0.8826284033712"},"directories":{}},"1.0.2":{"name":"nanostate","description":"Small Finite State Machine implementation","repository":{"type":"git","url":"git+https://github.com/choojs/nanostate.git"},"version":"1.0.2","scripts":{"start":"node .","test":"standard && node test"},"browser":{"assert":"nanoassert"},"dependencies":{"nanoassert":"^1.1.0","nanobus":"^4.3.1"},"devDependencies":{"standard":"^10.0.3","tape":"^4.8.0"},"keywords":["fsm","finite","state","machine","automata","small","minimal","browser","fast","history","concurrent","chart","charts"],"license":"Apache-2.0","gitHead":"e2f690a48abbff6bbf2474a3eeb632e755f1c737","bugs":{"url":"https://github.com/choojs/nanostate/issues"},"homepage":"https://github.com/choojs/nanostate#readme","_id":"nanostate@1.0.2","_npmVersion":"5.4.2","_nodeVersion":"8.8.1","_npmUser":{"name":"yoshuawuyts","email":"i@yoshuawuyts.com"},"dist":{"integrity":"sha512-HbCXV/fRQ42DDnTK6+Y6AcrqPlH26MT+jHHkNkV7avztLWGbrOsDmiwcPkMgs1H1DYXNltUeDs2PSvmt+FUQaw==","shasum":"37ecb471748831092c9445e3b5d57e84e1e93adf","tarball":"https://registry.npmjs.org/nanostate/-/nanostate-1.0.2.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCAXVRwr0WnVV3Ph9nU8EgfpOVGTvXr8EWTRLeXK944EgIhAPuIGSrdLlcyPUy8/3a/xQkoNWsGTl0EIHoxhOkclcNT"}]},"maintainers":[{"name":"yoshuawuyts","email":"i@yoshuawuyts.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nanostate-1.0.2.tgz_1511644188675_0.30602504312992096"},"directories":{}},"1.0.3":{"name":"nanostate","description":"Small Finite State Machine implementation","repository":{"type":"git","url":"git+https://github.com/choojs/nanostate.git"},"version":"1.0.3","scripts":{"start":"node .","test":"standard && node test"},"browser":{"assert":"nanoassert"},"dependencies":{"nanoassert":"^1.1.0","nanobus":"^4.3.1"},"devDependencies":{"standard":"^10.0.3","tape":"^4.8.0"},"keywords":["fsm","finite","state","machine","automata","small","minimal","browser","fast","history","concurrent","chart","charts"],"license":"Apache-2.0","gitHead":"2ba1b6eb1d9c4480b3d4b5b1e38d3163592c5e62","bugs":{"url":"https://github.com/choojs/nanostate/issues"},"homepage":"https://github.com/choojs/nanostate#readme","_id":"nanostate@1.0.3","_npmVersion":"5.4.2","_nodeVersion":"8.8.1","_npmUser":{"name":"yoshuawuyts","email":"i@yoshuawuyts.com"},"dist":{"integrity":"sha512-61EajdMoVV1XEzJiz0KKxh4XUQT5DgxJD2op70okY52NvR7wxlWhp5mKDxGteDtQaTm+9R27xXw4EFhGNvdezQ==","shasum":"ea991e2aa8e56db39f9b25e385456f6ca594dc44","tarball":"https://registry.npmjs.org/nanostate/-/nanostate-1.0.3.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIGLBO3Lgdeb97QJwE2LHROsCDT4uoqlukQMrd9NlwnJ5AiEAons6AIJGlw5kVTuZqGq8gW7BGH2B8peHX1uFBnKyop8="}]},"maintainers":[{"name":"yoshuawuyts","email":"i@yoshuawuyts.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nanostate-1.0.3.tgz_1511644320749_0.9661236619576812"},"directories":{}},"1.1.0":{"name":"nanostate","description":"Small Finite State Machine implementation","repository":{"type":"git","url":"git+https://github.com/choojs/nanostate.git"},"version":"1.1.0","scripts":{"start":"node .","test":"standard && node test"},"browser":{"assert":"nanoassert"},"dependencies":{"nanoassert":"^1.1.0","nanobus":"^4.3.1"},"devDependencies":{"standard":"^10.0.3","tape":"^4.8.0"},"keywords":["fsm","finite","state","machine","automata","small","minimal","browser","fast","history","concurrent","chart","charts"],"license":"Apache-2.0","gitHead":"3839653db644878b0095eb0a5313f348845b0058","bugs":{"url":"https://github.com/choojs/nanostate/issues"},"homepage":"https://github.com/choojs/nanostate#readme","_id":"nanostate@1.1.0","_npmVersion":"5.5.1","_nodeVersion":"9.3.0","_npmUser":{"name":"yoshuawuyts","email":"yoshuawuyts@gmail.com"},"dist":{"integrity":"sha512-Z9VFxUuPOKlrgmM9fi0+TvyS5c38HsAI5dRYB+eJN/fsc/8qLBZkrEYcm3Bu+HBU8JtMy3a2cT+1mUq6YlWIjQ==","shasum":"89e13300b9a4a8ee981e702013e979ae36cffd6a","tarball":"https://registry.npmjs.org/nanostate/-/nanostate-1.1.0.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICTm225zOBT9o0PV4RuYpNKtX8+NtA0UT+Iy/kl2qzf8AiBJ4lNU3N8qEchydQ6h1Z4QiWSSVNbysUDcr5gD0ej72w=="}]},"maintainers":[{"name":"yoshuawuyts","email":"i@yoshuawuyts.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nanostate-1.1.0.tgz_1516022081577_0.9893742701970041"},"directories":{}},"1.2.0":{"name":"nanostate","description":"Small Finite State Machine implementation","repository":{"type":"git","url":"git+https://github.com/choojs/nanostate.git"},"version":"1.2.0","scripts":{"start":"node .","test":"standard && node test"},"browser":{"assert":"nanoassert"},"dependencies":{"nanoassert":"^1.1.0","nanobus":"^4.3.1"},"devDependencies":{"standard":"^10.0.3","tape":"^4.8.0"},"keywords":["fsm","finite","state","machine","automata","small","minimal","browser","fast","history","concurrent","chart","charts"],"license":"Apache-2.0","gitHead":"4ccd21ee6a8b0356b524e46a76d49d629bf389a1","bugs":{"url":"https://github.com/choojs/nanostate/issues"},"homepage":"https://github.com/choojs/nanostate#readme","_id":"nanostate@1.2.0","_npmVersion":"5.6.0","_nodeVersion":"9.3.0","_npmUser":{"name":"yoshuawuyts","email":"yoshuawuyts@gmail.com"},"dist":{"integrity":"sha512-F9TlFSPLZ0SWE4RFOcJ2efBcgPbYXbVf0QfA+F9zxdK115IFX3RxgA07DGeTvmo5naw5Dgde9/ct8oSKEGX9rg==","shasum":"f0c2e7192c1c0fbac3a0eb2eb6a0e45cdcd88340","tarball":"https://registry.npmjs.org/nanostate/-/nanostate-1.2.0.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIGnk/K+oItOKzR7XhzkF8sNaHMpdo2j90B/KWaoXe+/UAiBvkjqb8SVQFop8Qp+1PDA1msLecgIi5EH9pdYTSI/ccA=="}]},"maintainers":[{"email":"jonathan.dupre@gmail.com","name":"kareniel"},{"email":"yoshuawuyts@gmail.com","name":"yoshuawuyts"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nanostate-1.2.0.tgz_1517311531273_0.7822721989359707"},"directories":{}},"1.2.1":{"name":"nanostate","description":"Small Finite State Machine implementation","repository":{"type":"git","url":"git+https://github.com/choojs/nanostate.git"},"version":"1.2.1","scripts":{"start":"node .","test":"standard && node test"},"browser":{"assert":"nanoassert"},"dependencies":{"nanoassert":"^1.1.0","nanobus":"^4.3.1"},"devDependencies":{"standard":"^10.0.3","tape":"^4.8.0"},"keywords":["fsm","finite","state","machine","automata","small","minimal","browser","fast","history","concurrent","chart","charts"],"license":"Apache-2.0","gitHead":"16b1a3e04c98c1e00d627417cd5387f57a577147","bugs":{"url":"https://github.com/choojs/nanostate/issues"},"homepage":"https://github.com/choojs/nanostate#readme","_id":"nanostate@1.2.1","_npmVersion":"5.6.0","_nodeVersion":"9.3.0","_npmUser":{"name":"yoshuawuyts","email":"yoshuawuyts@gmail.com"},"dist":{"integrity":"sha512-AR+/Vd/ObE6X/DmsqcKKt5BaQqXFgCkmC4jGLMb+4h9ih7u1bgHAhF83fuTYad1u8hIAJF6Ml/uM190qxhEdBw==","shasum":"ef2340d5085f195359f85a93f41e1d6aec0f4ab0","tarball":"https://registry.npmjs.org/nanostate/-/nanostate-1.2.1.tgz","fileCount":11,"unpackedSize":26211,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIGquzt4qXGo8dR28b1VKXHERgYqQQ2vjk1rMPOFQesiIAiBX5djtvxBSrNVZGctjF75e3NJ8vZxP/PRttmj0DnrUWg=="}]},"maintainers":[{"email":"jonathan.dupre@gmail.com","name":"kareniel"},{"email":"yoshuawuyts@gmail.com","name":"yoshuawuyts"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nanostate_1.2.1_1518436720669_0.2768755352176011"},"_hasShrinkwrap":false}},"readme":"# nanostate\n[![npm version][2]][3] [![build status][4]][5]\n[![downloads][8]][9] [![js-standard-style][10]][11]\n\nSmall Finite State Machines. Great data structure to make code more readable,\nmaintainable and easier to debug.\n\n## Usage\n```js\nvar nanostate = require('nanostate')\n\nvar machine = nanostate('green', {\n  green: { timer: 'yellow' },\n  yellow: { timer: 'red' },\n  red: { timer: 'green' }\n})\n\nmachine.emit('timer')\nconsole.log(machine.state)\n// => 'yellow'\n\nmachine.emit('timer')\nconsole.log(machine.state)\n// => 'red'\n\nmachine.emit('timer')\nconsole.log(machine.state)\n// => 'green'\n```\n\n## Hierarchical\nLet's implement a traffic light that flashes red whenever there's a power\noutage. Instead of adding a `powerOutage` event to each normal state, we\nintroduce a hierarchy which allows any normal state to emit the `powerOutage`\nevent to change the state to `flashingRed`.\n```js\nvar nanostate = require('nanostate')\n\nvar machine = nanostate('green', {\n  green: { timer: 'yellow' },\n  yellow: { timer: 'red' },\n  red: { timer: 'green' }\n})\n\nmachine.event('powerOutage', nanostate('flashingRed', {\n  flashingRed: { powerRestored: 'green' }\n}))\n\nmachine.emit('timer')\nconsole.log(machine.state)\n// => 'yellow'\n\nmachine.emit('powerOutage')\nconsole.log(machine.state)\n// => 'flashingRed'\n\nmachine.emit('powerRestored')\nconsole.log(machine.state)\n// => 'green'\n```\n\n## History (to be implemented)\nImplementers note: keep track of the last state a machine was in before exiting\nto the next machine. That way if `'$history'` is called, it can be merged into\nthe previous machine.\n\nTODO: figure out how it works if machines are combined in a non-linear fashion.\n\n```js\nvar nanostate = require('nanostate')\n\nvar machine = nanostate('cash', {\n  cash: { check: 'check' },\n  check: { cash: 'cash' }\n})\n\nmachine.join('next', nanostate('review', {\n  review: { previous: '$history' }\n}))\n```\n\n## Parallel\nSometimes there's multiple parallel states that need\nexpressing; `nanostate.parallel` helps with that. For example when editing\ntext, a particular piece of text might be `bold`, `italic` and `underlined` at\nthe same time. The trick is that all of these states operate in parallel\n```js\nvar nanostate = require('nanostate')\n\nvar machine = nanostate.parallel({\n  bold: nanostate('off', {\n    on: { 'toggle': 'off' },\n    off: { 'toggle': 'on' },\n  }),\n  underline: nanostate('off', {\n    on: { 'toggle': 'off' },\n    off: { 'toggle': 'on' },\n  }),\n  italics: nanostate('off', {\n    on: { 'toggle': 'off' },\n    off: { 'toggle': 'on' },\n  }),\n  list: nanostate('none', {\n    none: { bullets: 'bullets', numbers: 'numbers' },\n    bullets: { none: 'none', numbers: 'numbers' },\n    numbers: { bullets: 'bullets', none: 'none' }\n  })\n})\n\nmachine.emit('bold:toggle')\nconsole.log(machine.state)\n// => {\n//   bold: 'on',\n//   italics: 'off',\n//   underline: 'off',\n//   list: 'none'\n// }\n```\n\n## Nanocomponent\nUsage in combination with\n[nanocomponent](https://github.com/choojs/nanocomponent) to create stateful UI\ncomponents.\n```js\nvar Nanocomponent = require('nanocomponent')\nvar nanostate = require('nanostate')\n\nmodule.exports = class Component extends Nanocomponent {\n  constructor (name, state, emit) {\n    super(name, state, emit)\n\n    this.state = {\n      data: {},\n      input: ''\n    }\n\n    this.machine = nanostate('idle', {\n      idle: { click: 'loading' },\n      loading: { resolve: 'data', reject: 'error' },\n      data: { click: 'loading' },\n      error: { click: 'loading' }\n    })\n\n    this.machine.on('loading', () => this.searchRepositories())\n  }\n\n  createElement () {\n    var buttonText = {\n      idle: 'Fetch Github',\n      loading: 'Loading…',\n      error: 'Github fail. Retry?',\n      data: 'Fetch Again?'\n    }[this.machine.state]\n\n    return html`\n      <div>\n        <input\n          type=\"text\"\n          value=${this.state.input}\n          onChange=${e => (this.state.input = e.target.value) && this.rerender()}\n        >\n        <button\n          onClick=${() => this.machine.emit('click')}\n          disabled=${this.machine.state === 'loading'}\n        >\n          ${buttonText}\n        </button>\n        ${data && html`<div>${JSON.stringify(data, null, 2)}</div>`}\n        ${this.machine.state === 'error' && html`<h1>Error</h1>`}\n      </div>\n    `\n  }\n\n  searchRepositories () {\n    fetch(`${ROOT_URL}/${this.state.input}`)\n      .then(res => res.json())\n      .then(res => {\n         this.state.data = res.data\n         this.machine.emit('resolve')\n       })\n      .catch(err => this.machine.emit('reject'))\n  }\n}\n```\n\n## API\n### `machine = nanostate(initialState, transitions)`\nCreate a new instance of Nanostate. Takes the name of the initial state, and a\nmapping of states and their corresponding transitions. A state mapping is\ndefined as `{ 1: { 2: 3 }}`, where `1` is the state's name, `2` is an event\nname it accepts, and `3` is the new state after the event has been emitted.\n\n### `machine.emit(event)`\nMove from the current state to a new state. Will throw if an invalid command is\npassed.\n\n### `machine.on(state, cb)`\nTrigger a callback when a certain state is entered. Useful to trigger side\neffects upon state change.\n\n### `state = machine.state`\nReturn the current state.\n\n### `machine.event(eventName, machine)` (to be implemented)\nAdd another machine to the transition graph. The first argument is an event\nname, which can be transitioned to from all other states in the graph.\n\n### `machine = nanostate.parallel(machines)` (to be implemented)\nCombine several state machines into a single machine. Takes an object of state\nmachines, where the key is used to prefix the events for the state machine.\n\nSay we have two state machine: `'foo'` and `'bar'`. `'foo'` has an event called\n`'beep'`. When combined through `.parallel()`, the event on the combined\nmachine would be `'foo:beep'`.\n\n## Installation\n```sh\n$ npm install nanostate\n```\n\n## See Also\n- [Infinitely Better UIs With Finite Automata (video)](https://www.youtube.com/watch?v=VU1NKX6Qkxc&feature=youtu.be)\n- [yoshuawuyts/fsm-event](https://github.com/yoshuawuyts/fsm-event)\n- [davidkpiano/xstate](https://github.com/davidkpiano/xstate/)\n\n## License\n[Apache-2.0](./LICENSE)\n\n[0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square\n[1]: https://nodejs.org/api/documentation.html#documentation_stability_index\n[2]: https://img.shields.io/npm/v/nanostate.svg?style=flat-square\n[3]: https://npmjs.org/package/nanostate\n[4]: https://img.shields.io/travis/choojs/nanostate/master.svg?style=flat-square\n[5]: https://travis-ci.org/choojs/nanostate\n[6]: https://img.shields.io/codecov/c/github/choojs/nanostate/master.svg?style=flat-square\n[7]: https://codecov.io/github/choojs/nanostate\n[8]: http://img.shields.io/npm/dm/nanostate.svg?style=flat-square\n[9]: https://npmjs.org/package/nanostate\n[10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square\n[11]: https://github.com/feross/standard\n","maintainers":[{"email":"jonathan.dupre@gmail.com","name":"kareniel"},{"email":"yoshuawuyts@gmail.com","name":"yoshuawuyts"}],"time":{"modified":"2022-05-10T09:07:20.386Z","created":"2017-11-15T19:19:25.917Z","1.0.0":"2017-11-15T19:19:25.917Z","1.0.1":"2017-11-25T21:01:14.387Z","1.0.2":"2017-11-25T21:09:49.608Z","1.0.3":"2017-11-25T21:12:01.717Z","1.1.0":"2018-01-15T13:14:41.662Z","1.2.0":"2018-01-30T11:25:32.520Z","1.2.1":"2018-02-12T11:58:41.432Z"},"homepage":"https://github.com/choojs/nanostate#readme","keywords":["fsm","finite","state","machine","automata","small","minimal","browser","fast","history","concurrent","chart","charts"],"repository":{"type":"git","url":"git+https://github.com/choojs/nanostate.git"},"bugs":{"url":"https://github.com/choojs/nanostate/issues"},"license":"Apache-2.0","readmeFilename":"README.md"}