state-manager-object
v3.0.0
Published
State management module with event subscription and different kind of stores.
Downloads
71
Readme
State Manager Object
State management module with event subscription and different kind of stores.
Install
npm i state-manager-object
Use
A simple numeric store:
const {NumericStore} = require('state-manager-object')
const initialState = 0 // which is default
const store = new NumericStore(initialState)
store.increment()
store.increment()
store.decrement()
store.get() === 1
An object store:
const {ObjectStore} = require('state-manager-object')
const initialState = {age: 10}
const store = new ObjectStore(initialState)
store.update({age: 11})
store.update({name: 'Abc'})
store.get() === {age: 11, name: 'Abc'}
Generic store:
const {Store} = require('state-manager-object')
const initialState = 'something'
const store = new Store(initialState)
store.update('some other thing')
store.update(9)
store.get() === 9
All store types has a reset
method which reverts state to its initial state:
store.reset()
store.get() === initialState
Listen for changes:
store.subscribe(function(currentState, prevState) {
// do something on state change
})
Comparison is being done based on ===
operator when new state update request received.
Version management of this repository done by releaser 🚀
Thanks for watching 🐬