state-manager-object

3.0.0 • Public • Published

State Manager Object

State management module with event subscription and different kind of stores.

NPM npm version npm bundle size npm

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 🐬

ko-fi

Package Sidebar

Install

npm i state-manager-object

Weekly Downloads

16

Version

3.0.0

License

MIT

Unpacked Size

274 kB

Total Files

20

Last publish

Collaborators

  • muratgozel