{"_id":"sfsm","_rev":"11-1cdd33d729b811bbf5bb2f6aa26c1efc","name":"sfsm","description":"Simple Finite State Machine. Based on Javascript State Machine v2 (https://github.com/jakesgordon/javascript-state-machine/) By jakesgordon","dist-tags":{"latest":"0.0.4"},"versions":{"0.0.1":{"author":{"name":"Sergey Tsapenko","email":"4031651@gmail.com"},"name":"sfsm","description":"Simple Finite State Machine. Based on Javascript State Machine v2 (https://github.com/jakesgordon/javascript-state-machine/) By jakesgordon","version":"0.0.1","homepage":"https://github.com/4031651/node-javascript-state-machine","repository":{"type":"git","url":"git://github.com/4031651/node-javascript-state-machine.git"},"main":"index.js","engines":{"node":">= 0.4.12"},"_npmUser":{"name":"4031651","email":"4031651@gmail.com"},"_id":"sfsm@0.0.1","dependencies":{},"devDependencies":{},"_engineSupported":true,"_npmVersion":"1.0.99","_nodeVersion":"v0.4.12","_defaultsLoaded":true,"dist":{"shasum":"323c3f78500d88af934603177d88af220ab4f014","tarball":"https://registry.npmjs.org/sfsm/-/sfsm-0.0.1.tgz","integrity":"sha512-WRkGpaiska6nhhfL4QRPbPhPHpuSyUu3l1XXg8VqduGsaibv2pAM8JU63g5ENMlz69bcUt6+RSs6ABm4Ykz1Qg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCiMjVSQi4ZQqMeEsyyqY5NPpe2Sfd5jGcjEQ32sXCKPQIgPNAbVymG5/ImR6D47qTHeY+oNDyXqKn6gpx32Xg4R8w="}]},"maintainers":[{"name":"4031651","email":"4031651@gmail.com"}]},"0.0.2":{"author":{"name":"Sergey Tsapenko","email":"4031651@gmail.com"},"name":"sfsm","description":"Simple Finite State Machine. Based on Javascript State Machine v2 (https://github.com/jakesgordon/javascript-state-machine/) By jakesgordon","version":"0.0.2","homepage":"https://github.com/4031651/node-javascript-state-machine","repository":{"type":"git","url":"git://github.com/4031651/node-javascript-state-machine.git"},"main":"index.js","engines":{"node":">= 0.4.12"},"_npmUser":{"name":"4031651","email":"4031651@gmail.com"},"_id":"sfsm@0.0.2","dependencies":{},"devDependencies":{},"_engineSupported":true,"_npmVersion":"1.0.99","_nodeVersion":"v0.4.12","_defaultsLoaded":true,"dist":{"shasum":"6424e345485f12db59d60e100817d52d07c714bf","tarball":"https://registry.npmjs.org/sfsm/-/sfsm-0.0.2.tgz","integrity":"sha512-w7N7xRWQs99zdd7AVgx2X8z3MglaqIEwLGijPi4vDfm/TUW1hthLxmSFJtxZml0UiEkCTz6klOTQvyhMHHm0hw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIDt3cdMbB9RUSUFqUV69Uz+WjNrQm4IhSn1RqpMmaJN4AiBLDN46cHf2YutHmYMwLRJk5zUbFyNkPirSHOixSSKGRA=="}]},"maintainers":[{"name":"4031651","email":"4031651@gmail.com"}]},"0.0.3":{"author":{"name":"Sergey Tsapenko","email":"4031651@gmail.com"},"name":"sfsm","description":"Simple Finite State Machine. Based on Javascript State Machine v2 (https://github.com/jakesgordon/javascript-state-machine/) By jakesgordon","version":"0.0.3","homepage":"https://github.com/4031651/node-javascript-state-machine","repository":{"type":"git","url":"git://github.com/4031651/node-javascript-state-machine.git"},"main":"index.js","engines":{"node":">= 0.4.12"},"_npmUser":{"name":"4031651","email":"4031651@gmail.com"},"_id":"sfsm@0.0.3","dependencies":{},"devDependencies":{},"_engineSupported":true,"_npmVersion":"1.0.99","_nodeVersion":"v0.4.12","_defaultsLoaded":true,"dist":{"shasum":"fce4d4ecfd532fbe56c4e3f4698b0619cf9cd1a0","tarball":"https://registry.npmjs.org/sfsm/-/sfsm-0.0.3.tgz","integrity":"sha512-LEFwtniy54R2a2TooaEjeg9iRXRPkrPSLLjBTYHMpbQMaBhF8wehDNvbPuxy0SfnGe9urJW96WPKTXZRqAI6tQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCc2nRRF1da+WoO1DYXfkEWTzR5MYUS+desF1O6TyYMwQIgfGpRd5DlSM3SpiB91Hobwvg6/yijd7rL00o3OG1mYGw="}]},"maintainers":[{"name":"4031651","email":"4031651@gmail.com"}]},"0.0.4":{"author":{"name":"Sergey Tsapenko","email":"4031651@gmail.com"},"name":"sfsm","description":"Simple Finite State Machine. Based on Javascript State Machine v2 (https://github.com/jakesgordon/javascript-state-machine/) By jakesgordon","version":"0.0.4","homepage":"https://github.com/4031651/node-javascript-state-machine","repository":{"type":"git","url":"https://github.com/4031651/node-javascript-state-machine"},"main":"state-machine.js","engines":{"node":">= 0.4.12"},"scripts":{"test":"TEST=1 ./node_modules/qunit/bin/cli.js -c state-machine.js -t test/test_basics.js test/test_async.js test/test_advanced.js test/test_classes.js test/test_initialize.js"},"devDependencies":{"qunit":"*"},"readme":"Javascript Finite State Machine (v2.2.0)\n========================================\n\nThis standalone javascript micro-framework provides a finite state machine for your pleasure.\n\n * You can find the [code here](https://github.com/jakesgordon/javascript-state-machine)\n * You can find a [description here](http://codeincomplete.com/posts/2013/1/26/javascript_state_machine_v2_2_0/)\n * You can find a [working demo here](http://codeincomplete.com/posts/2011/8/19/javascript_state_machine_v2/example/)\n\nDownload\n========\n\nYou can download [state-machine.js](https://github.com/jakesgordon/javascript-state-machine/raw/master/state-machine.js),\nor the [minified version](https://github.com/jakesgordon/javascript-state-machine/raw/master/state-machine.min.js)\n\nAlternatively:\n\n    git clone git@github.com:jakesgordon/javascript-state-machine\n\n\n * All code is in state-machine.js\n * Minified version provided in state-machine.min.js\n * No 3rd party library is required\n * Demo can be found in /index.html\n * QUnit tests can be found in /test/index.html\n\nUsage\n=====\n\nInclude `state-machine.min.js` in your application.\n\nIn its simplest form, create a standalone state machine using:\n\n    var fsm = StateMachine.create({\n      initial: 'green',\n      events: [\n        { name: 'warn',  from: 'green',  to: 'yellow' },\n        { name: 'panic', from: 'yellow', to: 'red'    },\n        { name: 'calm',  from: 'red',    to: 'yellow' },\n        { name: 'clear', from: 'yellow', to: 'green'  }\n    ]});\n\n... will create an object with a method for each event:\n\n * fsm.warn()  - transition from 'green' to 'yellow'\n * fsm.panic() - transition from 'yellow' to 'red'\n * fsm.calm()  - transition from 'red' to 'yellow'\n * fsm.clear() - transition from 'yellow' to 'green'\n\nalong with the following members:\n\n * fsm.current   - contains the current state\n * fsm.is(s)     - return true if state `s` is the current state\n * fsm.can(e)    - return true if event `e` can be fired in the current state\n * fsm.cannot(e) - return true if event `e` cannot be fired in the current state\n\nMultiple 'from' and 'to' states for a single event\n==================================================\n\nIf an event is allowed **from** multiple states, and always transitions to the same\nstate, then simply provide an array of states in the `from` attribute of an event. However,\nif an event is allowed from multiple states, but should transition **to** a different\nstate depending on the current state, then provide multiple event entries with\nthe same name:\n\n    var fsm = StateMachine.create({\n      initial: 'hungry',\n      events: [\n        { name: 'eat',  from: 'hungry',                                to: 'satisfied' },\n        { name: 'eat',  from: 'satisfied',                             to: 'full'      },\n        { name: 'eat',  from: 'full',                                  to: 'sick'      },\n        { name: 'rest', from: ['hungry', 'satisfied', 'full', 'sick'], to: 'hungry'    },\n    ]});\n\nThis example will create an object with 2 event methods:\n\n * fsm.eat()\n * fsm.rest()\n\nThe `rest` event will always transition to the `hungry` state, while the `eat` event\nwill transition to a state that is dependent on the current state.\n\n>> NOTE: The `rest` event could use a wildcard '*' for the 'from' state if it should be\nallowed from any current state.\n\n>> NOTE: The `rest` event in the above example can also be specified as multiple events with\nthe same name if you prefer the verbose approach.\n\nCallbacks\n=========\n\n4 types of callback are available by attaching methods to your StateMachine using the following naming conventions:\n\n * `onbeforeEVENT` - fired before the event\n * `onleaveSTATE`  - fired when leaving the old state\n * `onenterSTATE`  - fired when entering the new state\n * `onafterEVENT`  - fired after the event\n\n>> (using your **specific** EVENT and STATE names)\n\nFor convenience, the 2 most useful callbacks can be shortened:\n\n * `onEVENT` - convenience shorthand for `onafterEVENT`\n * `onSTATE` - convenience shorthand for `onenterSTATE`\n\nIn addition, 4 general-purpose callbacks can be used to capture **all** event and state changes:\n\n * `onbeforeevent` - fired before *any* event\n * `onleavestate`  - fired when leaving *any* state\n * `onenterstate`  - fired when entering *any* state\n * `onafterevent`  - fired after *any* event\n\nAll callbacks will be passed the same arguments:\n\n * **event** name\n * **from** state\n * **to** state\n * _(followed by any arguments you passed into the original event method)_\n\nCallbacks can be specified when the state machine is first created:\n\n    var fsm = StateMachine.create({\n      initial: 'green',\n      events: [\n        { name: 'warn',  from: 'green',  to: 'yellow' },\n        { name: 'panic', from: 'yellow', to: 'red'    },\n        { name: 'calm',  from: 'red',    to: 'yellow' },\n        { name: 'clear', from: 'yellow', to: 'green'  }\n      ],\n      callbacks: {\n        onpanic:  function(event, from, to, msg) { alert('panic! ' + msg);               },\n        onclear:  function(event, from, to, msg) { alert('thanks to ' + msg);            },\n        ongreen:  function(event, from, to)      { document.body.className = 'green';    },\n        onyellow: function(event, from, to)      { document.body.className = 'yellow';   },\n        onred:    function(event, from, to)      { document.body.className = 'red';      },\n      }\n    });\n\n    fsm.panic('killer bees');\n    fsm.clear('sedatives in the honey pots');\n    ...\n\nAdditionally, they can be added and removed from the state machine at any time:\n\n    fsm.ongreen      = null;\n    fsm.onyellow     = null;\n    fsm.onred        = null;\n    fsm.onenterstate = function(event, from, to) { document.body.className = to; };\n\n\nThe order in which callbacks occur is as follows:\n\n>> assume event **go** transitions from **red** state to **green**\n\n * `onbeforego`    - specific handler for the **go** event only\n * `onbeforeevent` - generic  handler for all events\n * `onleavered`    - specific handler for the **red** state only\n * `onleavestate`  - generic  handler for all states\n * `onentergreen`  - specific handler for the **green** state only\n * `onenterstate`  - generic  handler for all states\n * `onaftergo`     - specific handler for the **go** event only\n * `onafterevent`  - generic  handler for all events\n\n>> NOTE: the legacy `onchangestate` handler has been deprecated and will be removed in a future version\n\nYou can affect the event in 3 ways:\n\n * return `false` from an `onbeforeEVENT` handler to cancel the event.\n * return `false` from an `onleaveSTATE` handler to cancel the event.\n * return `ASYNC` from an `onleaveSTATE` handler to perform an asynchronous state transition (see next section)\n\nAsynchronous State Transitions\n==============================\n\nSometimes, you need to execute some asynchronous code during a state transition and ensure the\nnew state is not entered until your code has completed.\n\nA good example of this is when you transition out of a `menu` state, perhaps you want to gradually\nfade the menu away, or slide it off the screen and don't want to transition to your `game` state\nuntil after that animation has been performed.\n\nYou can now return `StateMachine.ASYNC` from your `onleavestate` handler and the state machine\nwill be _'put on hold'_ until you are ready to trigger the transition using the new `transition()`\nmethod.\n\nFor example, using jQuery effects:\n\n    var fsm = StateMachine.create({\n\n      initial: 'menu',\n\n      events: [\n        { name: 'play', from: 'menu', to: 'game' },\n        { name: 'quit', from: 'game', to: 'menu' }\n      ],\n\n      callbacks: {\n\n        onentermenu: function() { $('#menu').show(); },\n        onentergame: function() { $('#game').show(); },\n\n        onleavemenu: function() {\n          $('#menu').fadeOut('fast', function() {\n            fsm.transition();\n          });\n          return StateMachine.ASYNC; // tell StateMachine to defer next state until we call transition (in fadeOut callback above)\n        },\n\n        onleavegame: function() {\n          $('#game').slideDown('slow', function() {\n            fsm.transition();\n          };\n          return StateMachine.ASYNC; // tell StateMachine to defer next state until we call transition (in slideDown callback above)\n        }\n\n      }\n    });\n\n>> _NOTE: If you decide to cancel the ASYNC event, you can call `fsm.transition.cancel();`\n\nState Machine Classes\n=====================\n\nYou can also turn all instances of a  _class_ into an FSM by applying\nthe state machine functionality to the prototype, including your callbacks\nin your prototype, and providing a `startup` event for use when constructing\ninstances:\n\n    MyFSM = function() {    // my constructor function\n      this.startup();\n    };\n\n    MyFSM.prototype = {\n\n      onpanic: function(event, from, to) { alert('panic');        },\n      onclear: function(event, from, to) { alert('all is clear'); },\n\n      // my other prototype methods\n\n    };\n\n    StateMachine.create({\n      target: MyFSM.prototype,\n      events: [\n        { name: 'startup', from: 'none',   to: 'green'  },\n        { name: 'warn',    from: 'green',  to: 'yellow' },\n        { name: 'panic',   from: 'yellow', to: 'red'    },\n        { name: 'calm',    from: 'red',    to: 'yellow' },\n        { name: 'clear',   from: 'yellow', to: 'green'  }\n      ]});\n\n\nThis should be easy to adjust to fit your appropriate mechanism for object construction.\n\n>> _NOTE: the `startup` event can be given any name, but it must be present in some form to \n   ensure that each instance constructed is initialized with its own unique `current` state._\n\nInitialization Options\n======================\n\nHow the state machine should initialize can depend on your application requirements, so\nthe library provides a number of simple options.\n\nBy default, if you dont specify any initial state, the state machine will be in the `'none'`\nstate and you would need to provide an event to take it out of this state:\n\n    var fsm = StateMachine.create({\n      events: [\n        { name: 'startup', from: 'none',  to: 'green' },\n        { name: 'panic',   from: 'green', to: 'red'   },\n        { name: 'calm',    from: 'red',   to: 'green' },\n    ]});\n    alert(fsm.current); // \"none\"\n    fsm.startup();\n    alert(fsm.current); // \"green\"\n\nIf you specify the name of your initial state (as in all the earlier examples), then an\nimplicit `startup` event will be created for you and fired when the state machine is constructed.\n\n    var fsm = StateMachine.create({\n      initial: 'green',\n      events: [\n        { name: 'panic', from: 'green', to: 'red'   },\n        { name: 'calm',  from: 'red',   to: 'green' },\n    ]});\n    alert(fsm.current); // \"green\"\n\nIf your object already has a `startup` method you can use a different name for the initial event\n\n    var fsm = StateMachine.create({\n      initial: { state: 'green', event: 'init' },\n      events: [\n        { name: 'panic', from: 'green', to: 'red'   },\n        { name: 'calm',  from: 'red',   to: 'green' },\n    ]});\n    alert(fsm.current); // \"green\"\n\nFinally, if you want to wait to call the initial state transition event until a later date you\ncan `defer` it:\n\n    var fsm = StateMachine.create({\n      initial: { state: 'green', event: 'init', defer: true },\n      events: [\n        { name: 'panic', from: 'green', to: 'red'   },\n        { name: 'calm',  from: 'red',   to: 'green' },\n    ]});\n    alert(fsm.current); // \"none\"\n    fsm.init();\n    alert(fsm.current); // \"green\"\n\nOf course, we have now come full circle, this last example is pretty much functionally the\nsame as the first example in this section where you simply define your own startup event.\n\nSo you have a number of choices available to you when initializing your state machine.\n\n>> _IMPORTANT NOTE: if you are using the pattern described in the previous section \"State Machine\n   Classes\", and wish to declare an `initial` state in this manner, you MUST use the `defer: true`\n   attribute and manually call the starting event in your constructor function. This will ensure\n   that each instance gets its own unique `current` state, rather than an (unwanted) shared\n   `current` state on the prototype object itself._\n\nHandling Failures\n======================\n\nBy default, if you try to call an event method that is not allowed in the current state, the\nstate machine will throw an exception. If you prefer to handle the problem yourself, you can\ndefine a custom `error` handler:\n\n    var fsm = StateMachine.create({\n      initial: 'green',\n      error: function(eventName, from, to, args, errorCode, errorMessage) {\n        return 'event ' + eventName + ' was naughty :- ' + errorMessage;\n      },\n      events: [\n        { name: 'panic', from: 'green', to: 'red'   },\n        { name: 'calm',  from: 'red',   to: 'green' },\n    ]});\n    alert(fsm.calm()); // \"event calm was naughty :- event not allowed in current state green\"\n\nRelease Notes\n=============\n\nSee [RELEASE NOTES](https://github.com/jakesgordon/javascript-state-machine/blob/master/RELEASE_NOTES.md) file.\n\nLicense\n=======\n\nSee [LICENSE](https://github.com/jakesgordon/javascript-state-machine/blob/master/LICENSE) file.\n\nContact\n=======\n\nIf you have any ideas, feedback, requests or bug reports, you can reach me at\n[jake@codeincomplete.com](mailto:jake@codeincomplete.com), or via\nmy website: [Code inComplete](http://codeincomplete.com/)\n\n\n\n\n\n","readmeFilename":"README.md","bugs":{"url":"https://github.com/4031651/node-javascript-state-machine/issues"},"_id":"sfsm@0.0.4","dist":{"shasum":"92ee09d4bde7ea2cc3e84be8fa13da181e823136","tarball":"https://registry.npmjs.org/sfsm/-/sfsm-0.0.4.tgz","integrity":"sha512-V5p91Eya1d8UlecRLQ363DFk25HJNN6B09WMJnnkdQPGKAKorSJelevaoapKIW9YDodZEkBbUZysR+MRtuw1dw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDaVn8YXRBXQ4N/dChI7wDuqwyzItpOLBcW8ENTX002lAIgIdAZhVcDkzhbmVbR9QCn+AbxZyoqcknMNGzJa4lrZl4="}]},"_from":".","_npmVersion":"1.2.25","_npmUser":{"name":"4031651","email":"4031651@gmail.com"},"maintainers":[{"name":"4031651","email":"4031651@gmail.com"}]}},"maintainers":[{"name":"4031651","email":"4031651@gmail.com"}],"time":{"modified":"2022-06-26T18:27:16.000Z","created":"2011-12-01T12:54:30.858Z","0.0.1":"2011-12-01T12:54:32.643Z","0.0.2":"2011-12-01T13:59:35.198Z","0.0.3":"2011-12-14T16:39:11.947Z","0.0.4":"2013-06-13T11:29:56.893Z"},"author":{"name":"Sergey Tsapenko","email":"4031651@gmail.com"},"repository":{"type":"git","url":"https://github.com/4031651/node-javascript-state-machine"}}