Minimalistic state container for your vqua components
class Cat {
constructor() {
this.state = {
say: null
}
}
dispatch(state, action) {
switch (action.name) {
case 'update': {
return action.data
break
}
default: {
return action.data
}
}
}
}
const memorux = require('memorux')
const memorux = new Memorux({ Cat })
memorux.store // => { Cat: { say: null } }
memorux.dispatch('Cat#update', { say: 'meow' })
// or
memorux.dispatch({
name: 'Cat#update',
data: { say: 'meow' }
})
// or
memorux.dispatch([
{
name: 'Cat#update',
data: { say: 'meow' }
},
])
memorux.onChange(store =>
// store => { Cat: { say: 'meow' } }
)
just return a promise like callback from your dispatch method
class Cat {
constructor() {
this.state = {
say: null
}
}
dispatch(state, action) {
switch (action.name) {
case 'update': {
return (resolve, reject) => {
setImmediate(() => {
resolve(action.data)
})
}
break
}
default: {
return action.data
}
}
}
}
Also, if you dispatch a few delayed actions you may need an event ending all of the actions, you can do so
memorux.dispatch([
// delayed action
// delayed action
// delayed action
]).then((store) => {
// do something with store
// after all actions ready
})