state-handler
v1.0.4
Published
State based function chain executor
Downloads
10
Readme
State Handler
Trigger execution chains based on the state of injected parameters.
If a function within the chain returns false
the execution chain stops and
the next function will not be called.
Install
npm install state-handler --save
example
const StateHandler = require('state-handler')
// some example state
const state = {
items: [],
complete: false
}
const sh = StateHandler (state)
// arguments can be an array or a list of parameters
sh.on(// [
(s) => s.items.length === 2,
(s) => alert(JSON.stringify(s.items)),
(s) => {s.complete = true}, // {} prevent return of false
(s) => s.items = [],
// ]
)
state.items.push(1)
sh.exec() // NOP
state.items.push(2)
sh.exec() // exec
off()
state.items.push(3)
state.items.push(4)
sh.exec() // NOP
StateHandler accepts an arbitrary length of parameters, e.g.:
const sh = StateHandler (state, dispatch)
sh.on(
(s) => s.completed === true,
(s, dispatch) => dispatch(s)
)
// modify state
sh.exec()
Download
- Development version: https://npmcdn.com/state-handler/dist/exec.js
- Minified version: https://npmcdn.com/state-handler/dist/exec.min.js