Lean, predictable state management. Based on Flux, specifically the Redux implementation.
npm install brenton-store
import createStore from 'brenton-store'
const initialState = {
foo: {
bar: 'baz'
}
}
const store = createStore(initialState)
store.getState().foo // === { bar: 'baz' }
store.getStateAt(['foo', 'bar']) // === 'baz'
store.subscribe('EVENT_TYPE', (nextState, prevState) => {
console.log(nextState, prevState)
})
const ref1 = store.subscribe('EVENT_TYPE', (nextState, prevState) => {
console.log(nextState, prevState)
})
const ref2 = store.subscribe('EVENT_TYPE', (nextState, prevState) => {
console.log(nextState, prevState)
})
const ref3 = store.subscribe('EVENT_TYPE', (nextState, prevState) => {
console.log(nextState, prevState)
})
// deletes the eventHandler for ref2
ref2.unsubscribe()
// calls all eventHandlers subscribed to 'EVENT_TYPE'
store.emit('EVENT_TYPE')
const payloadToReplaceState = { foo: 'foo' }
// calls all eventHandlers subscribed to 'EVENT_TYPE'
// and replaces state with payload
store.update('EVENT_TYPE', payloadToReplaceState)
store.getState() // === { foo: 'foo' }
const payloadToReplaceValueAtEndOfPath = ['sandwich']
// calls all eventHandlers subscribed to 'EVENT_TYPE'
// and replaces state.foo with payload
store.updateAt(['foo'], 'EVENT_TYPE', payloadToReplaceValueAtEndOfPath)
store.getState() // === { foo: ['sandwich'] }