0.9.10 • Published 5 years ago

statebase v0.9.10

Weekly downloads
1
License
ISC
Repository
github
Last release
5 years ago

{≡} statebase

A tiny but powerful state management library. Inspired by React setState and the Firebase database API.

example usage

Install:

npm install statebase --save

Include:

import createState from 'statebase'

or 

var createState = require('statebase')

Create state object:

var state = createState({
    a: true,
    b: {
        c: 'wow'
    }
})

Reference part of the state:

var aRef = state.ref('a')
var bRef = state.ref('b')
var cRef = state.ref('b').ref('c')

Read state values:

state.val()     // -> {a: true, b: {c: 'wow'}}
aRef.val()      // -> true
bRef.val()      // -> {c: 'wow'}
cRef.val()      // -> 'wow'

Update state values:

state.val()     // -> {a: true, b: {c: 'wow'}}

aRef.set(false)
aRef.val()      // -> false

cRef.set('nice')
cRef.val()      // -> 'nice'

state.val()     // -> {a: false, b: {c: 'nice'}}

Reset to initial value:

state.val()     // -> {a: true, b: {c: 'wow'}}

state.ref('a').set(false)
state.ref('b').ref('c').set('nice')
state.ref('b').ref('d').set('bubba')

state.val()     // -> {a: false, b: {c: 'nice', d: 'bubba'}}

state.ref('b').reset()

state.val()     // -> {a: false, b: {c: 'wow'}}

Listen for changes:

var callback = function(ref) {
    console.log('b updated: ', ref.val())
}
var unsubscribe = bRef.listen(callback)

cRef.set('amazing')      // value updated, callback triggered
unsubscribe()            // remove listener
cRef.set('incredible')   // value updated, callback *NOT* triggered

Use with React

To use with React.js, use the react-statebase package.

License

MIT

0.9.10

5 years ago

0.9.9

5 years ago

0.9.8

5 years ago

0.9.7

5 years ago

0.9.6

5 years ago

0.9.5

5 years ago

0.9.4

5 years ago

0.9.3

5 years ago

0.9.2

5 years ago