├── README.md ├── package.json └── lib └── docstate.js /README.md: -------------------------------------------------------------------------------- 1 | # Docstate, for stately Couch stuff 2 | 3 | A very basic state manager for document based state machines. 4 | 5 | ## Usage 6 | 7 | Here is how you would create a useless infinite loop, all serialized through a CouchDB changes feed. 8 | 9 | ```javascript 10 | var control = require('docstate').control(); 11 | 12 | control.safe("user", "new", function(doc){ 13 | console.log(doc.type) // "user" 14 | doc.state = "old"; 15 | // go save the doc 16 | }) 17 | 18 | control.safe("user", "old", function(doc){ 19 | doc.state = "new"; 20 | // go save the doc 21 | }) 22 | 23 | control.start() 24 | 25 | mythicalChangesListener.onChange(function(info, doc){ 26 | control.handle(doc) 27 | }) 28 | ``` 29 | 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "docstate" 3 | , "description" : "Couch + Stately equals a reliable state machine" 4 | , "homepage" : "http://github.com/jchris/stately" 5 | , "version" : "0.0.1" 6 | , "author" : "J Chris Anderson (http://jchrisa.net)" 7 | , "contributors" : [] 8 | , "bugs" : 9 | { "mail" : "jchris@couchbase.com" 10 | , "web" : "http://github.com/jchris/stately/issues" 11 | } 12 | , "directories" : { "lib" : "./lib" } 13 | , "engines" : { "node" : ">=0.2.0" } 14 | , "licenses" : 15 | [ { "type" : "Apache 2.0" 16 | , "url" : "http://www.apache.org/licenses/LICENSE-2.0.html" 17 | } 18 | ] 19 | , "main" : "./lib/docstate" 20 | , "dependencies": 21 | { "stately" : "0.1.x" } 22 | } -------------------------------------------------------------------------------- /lib/docstate.js: -------------------------------------------------------------------------------- 1 | var stately = require("stately"); 2 | 3 | exports.control = function() { 4 | var safeMachine, safeStates = {} 5 | , cautiousMachine, unsafeStates = {} 6 | ; 7 | 8 | function makeMachines() { 9 | safeMachine = stately.define(safeStates); 10 | cautiousMachine = stately.define(unsafeStates); 11 | }; 12 | 13 | function handle(doc) { 14 | safeMachine.handle(doc); 15 | cautiousMachine.handle(doc); 16 | }; 17 | 18 | function registerSafeCallback(type, state, cb) { 19 | safeStates[type] = safeStates[type] || {}; 20 | safeStates[type][state] = cb; 21 | } 22 | 23 | function registerUnsafeCallback(type, state, cb) { 24 | // todo this needs expiring lock 25 | unsafeStates[type] = unsafeStates[type] || {}; 26 | unsafeStates[type][state] = cb; 27 | } 28 | 29 | return { 30 | start : makeMachines, 31 | safe : registerSafeCallback, 32 | unsafe : registerUnsafeCallback, 33 | handle : handle 34 | }; 35 | }; 36 | --------------------------------------------------------------------------------