1.0.4 • Published 8 years ago

state-handler v1.0.4

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

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

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago