store-emitter
v2.2.0
Published
[![npm](https://img.shields.io/npm/v/store-emitter.svg?style=flat-square)](https://npmjs.org/store-emitter)
Downloads
9
Readme
store-emitter
A simple redux-inspired state management library that provides more flexible options for listening to changes.
Example
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(previous.example)
})
store({ type: 'example' })
API
createStoreEmitter
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' }
}
})
store
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'
})
store.getState
Get the current state of the store
Examples
var state = store.getState()
store.initialState
Get the initial state of the store
Examples
var state = store.initialState()
store.off
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) {
})
store.on
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) {
})
store.once
Listen for a single change to the store
Parameters
event
string – an action typecallback
Function
Examples
store.once('article', function (action, state, oldState) {
})
See also
- 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