crelmstat
v2.0.0
Published
An expedient, minuscule (1kb), super simple, fully tested, zero dependency, generic state management tool.
Maintainers
Readme
crelmstat
An expedient, minuscule (1kb), super simple, fully tested, zero dependency, generic state management tool. Made specifically (but not exclusively) for use with crelm.
installation
npm i crelmstatimport
import crelmstat from 'crelmstat'
const state = crelmstat()
// or
const state = require('crelmstat')()usage
// Set a state value
state.set(key, value)
state.set('sample', true)
// Get a state value
state.get(key)
let sample = state.get('sample')
// Set with a function to modify from the current value
state.set(key, func)
state.set('sample', counter => counter + 1)
// Attach to element/object for auto updates
let obj = {}
state.attach(key, elem:Object or querySelector string, attr)
// If the 2nd argument is a html element reference, the listener will be removed when the element is removed from the DOM
state.attach('sample', obj, 'innerHTML')
state.attach('money', '.player-money', 'innerText')
// Attach function
state.attach(sampleFunction)
state.attach(key, newValue => {
window.sample = newValue
})
state.attach(key, newValue => {
if (window.isHappy) doTheThings()
else return null // returning null will delete the listener
})import/export JSON
let json = state.toJSON()
state.load(json)global state
const {global} = require('crelmstat')
global.attach('test', document.body, 'innerHTML')
global.set('test', 'Hello World!')
global.get('test')
// or
window.state = require('crelmstat')()Changelog
v2.0.0
- ADD: You can use a function in the 2nd argument of the set method. The first argument of said function will be the current value of the state.
