quark-state
Simple state manager based on Singleton design pattern.
This package is part of quark
framework but it can be used independently.
Installation
npm install quark-state --save
Usage
Basic
Initialize a container and set/get a prop.
const initialUserState = 'name': 'John Doe' 'age': 36 'location': 'latitude': 34564756 'longitude': 32804872 // Initialize a containerState // Get a prop from a containerconst name = State // = 'John Doe' // Set a prop on a containerState
Container
Init a container
const initialContainerState = {}State
Destroy a container
State
Clear
Clear the State by destroying all containers
const initialContainerState = {}State Stateclear
Set
Set a (deep) prop
const initialContainerState = 'deep': 'prop': true State State
Set an object prop (merge)
const initialContainerState = 'deep': 'prop': 'integer': 10 'boolean': true State // By default, it will merge the two objectsState
Set an object prop (overwrite)
const initialContainerState = 'deep': 'prop': 'integer': 10 'boolean': true State // If you set the third argument to true, it will overwrite the prop valueState
Get
const initialContainerState = 'boolean': trueState State // = true
Has
Check if the given query exists (container or prop)
const initialContainerState = 'string': 'foo'State State // = trueState // = trueState // = false
On change
Add
When a prop is modified, call a callback with old and new values
const initialContainerState = 'string': 'foo' 'deep': 'prop': true State // When a prop is modified, callback is calledState // oldVal = 'foo', newVal = 'bar'State // When a deep prop is modified, it also triggers parent callbackState // oldVal = true, newVal = falseState
Remove
const initialContainerState = 'string': 'foo' 'deep': 'prop': true State const callback = { } // Won't be trigger StateState State
API
See https://fm-ph.github.io/quark-state/
Build
To build the sources with babel
in ./lib
directory :
npm run build
Documentation
To generate the JSDoc
:
npm run docs
To generate the documentation and deploy on gh-pages
branch :
npm run docs:deploy
Testing
To run the tests, first clone the repository and install its dependencies :
git clone https://github.com/fm_ph/quark-state.gitcd quark-statenpm install
Then, run the tests :
npm test
To watch (test-driven development) :
npm run test:watch
For coverage :
npm run test:coverage
License
MIT License © Patrick Heng Fabien Motte