brenton-store
v1.1.3
Published
Lean, predictable state management. Based on Flux, specifically the Redux implementation.
Downloads
7
Readme
brenton-store
Lean, predictable state management. Based on Flux, specifically the Redux implementation.
npm install brenton-store
API:
import createStore from 'brenton-store'
createStore( [initialState] )
const initialState = {
foo: {
bar: 'baz'
}
}
const store = createStore(initialState)
store.getState()
store.getState().foo // === { bar: 'baz' }
store.getStateAt(path);
store.getStateAt(['foo', 'bar']) // === 'baz'
store.subscribe(type, handler)
store.subscribe('EVENT_TYPE', (nextState, prevState) => {
console.log(nextState, prevState)
})
subscribeReference.unsubscribe()
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()
store.emit(type)
// calls all eventHandlers subscribed to 'EVENT_TYPE'
store.emit('EVENT_TYPE')
store.update(type, payload)
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' }
store.updateAt(path, type, payload)
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'] }