A simple redux-inspired state management library that provides more flexible options for listening to changes.
function modifier (action, state) {
if (action.type === 'example') {
return { example: true }
}
}
var store = createStore(modifier, {
example: false
})
store.on('*', function (action, state, oldState) {
console.log()
})
store.on('example', function (action, state, oldState) {
t.ok(state.example)
t.notOk(oldState.example)
})
store({ type: 'example' })
Create the store
Parameters
modifier
functioninitialState
[object]
Examples
var createStore = require('store-emitter')
var store = createStore(function (action, state) {
if (action.type === 'change_something') {
return { something: 'changed' }
}
})
Send an action to the store. Takes a single object parameter. Object must include a type
property with a string value, and can contain any other properties.
Parameters
action
objectaction.type
string
Examples
store({
type: 'example'
exampleValue: 'anything'
})
Get the current state of the store
Examples
var state = store.getState()
Get the initial state of the store
Examples
var state = store.initialState()
Stop listening for changes to the store. Passing just the action type will remove all listeners for that action type.
Parameters
event
string – an action typecallback
[Function] – optional callback
Examples
store.off('article', function (action, state, oldState) {
})
Listen for changes to the store
Parameters
event
string – an action typecallback
Function
Examples
store.on('*', function (action, state, oldState) {
})
store.on('article', function (action, state, oldState) {
})
store.on('article:delete', function (action, state, oldState) {
})
Listen for a single change to the store
Parameters
event
string – an action typecallback
Function
Examples
store.once('article', function (action, state, oldState) {
})
- virtual-app – uses store-emitter as a dependency
- namespace-emitter – is a dependency of store-emitter
- yo-yo.js – a library for building UI components that works well with store-emitter
- minidux – a small alternative to redux that means to be a drop-in replacement