{"_id":"n-behaviour","_rev":"10-2e8808fa665ca8d3d73c5b386d92b133","name":"n-behaviour","description":"Finite State Machine / Behaviour management for nodejs and the browser","dist-tags":{"latest":"1.1.0"},"versions":{"1.0.0":{"name":"n-behaviour","version":"1.0.0","description":"Finite State Machine / Behaviour management for nodejs and the browser","main":"dist/behaviour.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"repository":{"type":"git","url":"https://github.com/netgusto/n-behaviour"},"author":{"name":"Net Gusto / Jérôme Schneider","url":"http://netgusto.com"},"license":"MIT","bugs":{"url":"https://github.com/netgusto/n-behaviour/issues"},"homepage":"https://github.com/netgusto/n-behaviour","dependencies":{"graphlib":"git+https://github.com/cpettitt/graphlib"},"devDependencies":{"grunt":"^0.4.5","grunt-babel":"^5.0.0","grunt-cli":"^0.1.13","load-grunt-tasks":"^3.1.0"},"gitHead":"750e8d4c7b258c08846d6f496ba09f327889d563","_id":"n-behaviour@1.0.0","_shasum":"a90b48f360cfce012a1cc08089f06d9a6c6664d3","_from":".","_npmVersion":"2.7.1","_nodeVersion":"0.12.0","_npmUser":{"name":"netgusto","email":"contact@netgusto.com"},"dist":{"shasum":"a90b48f360cfce012a1cc08089f06d9a6c6664d3","tarball":"https://registry.npmjs.org/n-behaviour/-/n-behaviour-1.0.0.tgz","integrity":"sha512-D2Sz87bwPfev4GM2Vk9xfiixsYT4T9faQsEhCNAnAOhIFXCeWGqBsSWv/Y0J2zJM88mTiIplrbT8QBr065B27A==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCR7nfjXBfKOfJ+15BQrDT2+CGP0MsBfJtuRpXz+oMgtAIgHb8ulcSOo/t+K6fxsrms/6ZsKVGbznFvDGAnPfgs48g="}]},"maintainers":[{"name":"netgusto","email":"contact@netgusto.com"}]},"1.0.1":{"name":"n-behaviour","version":"1.0.1","description":"Finite State Machine / Behaviour management for nodejs and the browser","main":"dist/behaviour.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"repository":{"type":"git","url":"https://github.com/netgusto/n-behaviour"},"author":{"name":"Net Gusto / Jérôme Schneider","url":"http://netgusto.com"},"license":"MIT","bugs":{"url":"https://github.com/netgusto/n-behaviour/issues"},"homepage":"https://github.com/netgusto/n-behaviour","dependencies":{"graphlib":"git+https://github.com/cpettitt/graphlib"},"devDependencies":{"grunt":"^0.4.5","grunt-babel":"^5.0.0","grunt-cli":"^0.1.13","load-grunt-tasks":"^3.1.0"},"gitHead":"750e8d4c7b258c08846d6f496ba09f327889d563","_id":"n-behaviour@1.0.1","_shasum":"7e895743314e2e0350d8407561b1fa20db493f7c","_from":".","_npmVersion":"2.7.1","_nodeVersion":"0.12.0","_npmUser":{"name":"netgusto","email":"contact@netgusto.com"},"dist":{"shasum":"7e895743314e2e0350d8407561b1fa20db493f7c","tarball":"https://registry.npmjs.org/n-behaviour/-/n-behaviour-1.0.1.tgz","integrity":"sha512-i1EKCompG07KMSVz/UMF3Wdi2sfQWzJfMkcOvir7oSUVQA/jFswIcV55yvb7qgr3nkmj7/3MvXaFaNmxZsm9tQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIAwOka640F7XR7dhegdEXSE1U6hnZzlH1Z4F08Ks7RkPAiEA31xGQdHzX03Mb0lGyYfQGK7qmK3I0bycWf+vKQg8r5o="}]},"maintainers":[{"name":"netgusto","email":"contact@netgusto.com"}]},"1.1.0":{"name":"n-behaviour","version":"1.1.0","description":"Finite State Machine / Behaviour management for nodejs and the browser","main":"dist/behaviour.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"repository":{"type":"git","url":"https://github.com/netgusto/n-behaviour"},"author":{"name":"Net Gusto / Jérôme Schneider","url":"http://netgusto.com"},"license":"MIT","bugs":{"url":"https://github.com/netgusto/n-behaviour/issues"},"homepage":"https://github.com/netgusto/n-behaviour","dependencies":{"graphlib":"git+https://github.com/cpettitt/graphlib"},"devDependencies":{"grunt":"^0.4.5","grunt-babel":"^5.0.0","grunt-cli":"^0.1.13","load-grunt-tasks":"^3.1.0"},"gitHead":"06b1446789b34a9b3e2701c073f1ac0143451be8","_id":"n-behaviour@1.1.0","_shasum":"8554b2d8d9d2112181075baca73577a257a260b6","_from":".","_npmVersion":"2.7.0","_nodeVersion":"0.10.35","_npmUser":{"name":"netgusto","email":"contact@netgusto.com"},"maintainers":[{"name":"netgusto","email":"contact@netgusto.com"}],"dist":{"shasum":"8554b2d8d9d2112181075baca73577a257a260b6","tarball":"https://registry.npmjs.org/n-behaviour/-/n-behaviour-1.1.0.tgz","integrity":"sha512-JpKWaHFGjP9wCo+tTWjJXgGy6yZGUv/6GjdgYY8hDu+ik0ZqxILGQowKrCW3nYDC5GtdNaH2sv38Rk238vDpmg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQD0vIVDj7loZBFVrrkgrY3VTMU92kUio2wctIunaeTh0QIhANZhl8KHWUPpnqsOBTXMJ5/C9NmN9zItp/zAQ3Rpy3QE"}]}}},"readme":"# n-behaviour\n\nFinite State Machine / Behaviour management for nodejs and the browser.\n\n## Install\n\n```bash\nnpm install --save n-behaviour\n```\n\n## Usage\n\n```javascript\n'use strict';\n\nimport Behaviour from 'n-behaviour';\nimport State from './Shared/StaticData/State';\n\nexport default class MyEntityBehaviour extends Behaviour {\n\n    constructor() {\n\n        const affect = [State.AFFECTED, {\n            apply: (entity, someone) => entity.set('dateaffected', new Date()).set('user', someone),\n            check: (entity) => entity.get('dateaffected') !== null && entity.get('user') !== null\n        }];\n\n        const unaffect = [State.NEW, {\n            apply: (entity) => entity.set('dateaffected', null).set('user', null),\n            check: (entity) => entity.get('dateaffected') === null && entity.get('user') === null\n        }];\n\n        const cancel = [State.CANCELLED, {\n            apply: (entity) => entity.set('datecancelled', new Date()),\n            check: (entity) => entity.get('datecancelled') !== null\n        }];\n\n        const states = {\n            [State.NEW]: {\n                affect,\n                cancel\n            },\n            [State.AFFECTED]: {\n                processed: State.CLOSED,   // shorthand when only setting the state\n                unaffect,\n                cancel\n            },\n            [State.CANCELLED]: {}\n            [State.CLOSED]: {}\n        };\n\n        super({\n            states,\n            getState: (entity) => entity.get('state'),\n            setState: (entity, state) => entity.set('state', state)\n        });\n    }\n\n    // Public API\n    affect(entity, someone) { return this.handle(\"affect\", entity, someone); }\n\n    unaffect(entity) { return this.handle(\"unaffect\", entity); }\n\n    processed(entity) { return this.handle(\"processed\", entity); }\n\n    cancel(entity) { return this.handle(\"cancel\", entity); }\n}\n```\n\nAnd then, in your code:\n\n```javascript\nimport { doSomeJob } from './myCode';\nimport MyEntityBehaviour from './MyEntityBehaviour';\n\nconst behaviour = new MyEntityBehaviour();\nconst entity = new MyEntity();    // Immutable.js Record for instance\n\nconsole.log(entity.get('state'));   // State.NEW\n\nentity = behaviour.affect(entity, 'jerome');\nconsole.log(entity.get('state'));   // State.AFFECTED\n\nentity = doSomeJob(entity);\n\nentity = behaviour.processed(entity);\nconsole.log(entity.get('state'));   // State.CLOSED\n\n```\n\nChecking if transition is conform afterwards (ie, for server-side validation of client-side state change before persistence).\n0 and 1-hop state transitions are valid.\n\n```javascript\nconst ourEntity = db.find('MyEntity', request.params.id);\nconst theirEntity = new MyEntity(newrequest.params.entity);    // Object decoded from JSON payload, for instance\n\nconst behaviour = new MyEntityBehaviour();\nif(behaviour.check(ourEntity, theirEntity)) {\n    throw new Exception('Invalid state transition detected !');\n}\n\n```\n","maintainers":[{"name":"netgusto","email":"contact@netgusto.com"}],"time":{"modified":"2022-06-20T18:49:25.191Z","created":"2015-04-30T21:22:42.513Z","1.0.0":"2015-04-30T21:22:42.513Z","1.0.1":"2015-04-30T21:23:05.964Z","1.1.0":"2015-05-25T12:14:48.197Z"},"homepage":"https://github.com/netgusto/n-behaviour","repository":{"type":"git","url":"https://github.com/netgusto/n-behaviour"},"author":{"name":"Net Gusto / Jérôme Schneider","url":"http://netgusto.com"},"bugs":{"url":"https://github.com/netgusto/n-behaviour/issues"},"license":"MIT","readmeFilename":"README.md","users":{"netgusto":true}}