state-handler

1.0.4 • Public • Published

NPM Build Status

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

Readme

Keywords

Package Sidebar

Install

npm i state-handler

Weekly Downloads

2

Version

1.0.4

License

MIT

Last publish

Collaborators

  • rhalff