A redux-inspired state management library with flexible options for listening to changes.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
tests
.gitignore
LICENSE.md
README.md
index.js
package.json

README.md

store-emitter

npm

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(oldState.example)
})

store({ type: 'example' })

API

createStoreEmitter

Create the store

Parameters

  • modifier function
  • initialState [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 object
    • action.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 type
  • callback [Function] – optional callback

Examples

store.off('article', function (action, state, oldState) {

})

store.on

Listen for changes to the store

Parameters

  • event string – an action type
  • callback 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 type
  • callback 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

LICENSE

MIT