A simple state manager
one-stop
wraps an object model (which can include root functions) and returns by default an object having a state
property and all functions declared in the original object.
As Write Mode, one-stop
accepts three arguments
model
object which should declare mutations functions in a dedicatedactions
propertynotify
function which is called after any changes (in the timeframe oftimeout
option)timeout
number being the maximum time after whichnotify
function is called (optional, default: 50ms)
As Read-Only Mode
one-stop
accepts only themodel
as argument- the
actions
as not acessible - the state is read-only
import oneStop from 'one-stop'
const model = {
count:0,
array : ['one'],
nest : {
count : 10
},
isZeroCount() {
return this.count === 0
},
actions { // actions will be flatten to the root object
increment (increment) {
this.count += increment
}
}
}
const notifyChanges = () => console.log('state updated')
const {state, increment, isZeroCount} = oneStop(initialState, notifyChanges)
// WRITING (prints "state updated")
state.array.push('two')
increment(1)
// READING (prints false 1 undefined)
console.log(
isZeroCount(),
state.count,
state.foo
)
// CREATING new property throws a TypeError
state.foo = 1