0.1.1 • Published 9 years ago

emit-state v0.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
9 years ago

emit-state

Build Status js-standard-style

EventEmitter for mutating a single state object.

Inspired by substack/virtual-dom-unidirectional-example.

Installation

npm install emit-state

Usage

var createStateEmitter = require('emit-state')
var actions = require('./actions.js')

var store = createStateEmitter(actions)
store.on('update', function (newState, oldState) {
  // main render loop
  // this is fired anytime there is a state change from
  // the registered actions
})

// Kick off initial action
store.emit('init')

actions.js

var xhr = require('xhr')

module.exports = [
  {
    type: 'init',
    fn: function (store) {
      store.emit('fetchSomethingAsync')
    }
  },

  {
    type: 'fetchSomethingAsync',
    fn: function (store) {
      store.emit('fetchingSomething')
      xhr(
        { method: 'GET', url: 'http://someapi.com' },
        function (err, resp, body) {
          if (err) {
            store.emit('fetchFailed', err)
            return
          }

          store.emit('updateItems', body)
        }
      )
    }
  },

  {
    type: 'updateItems',
    fn: function (store, items) {
      var state = store.getState()
      state.isFetching = false
      state.fetchFailed = false
      state.items = items
      return state
    }
  },

  {
    type: 'fetchingSomething',
    fn: function (store) {
      var state = store.getState()
      state.isFetching = true
      return state
    }
  },

  type: 'fetchFailed',
  fn: function (store) {
    var state = store.getState()
    state.isFetching = false
    state.fetchFailed = true
    return state
  }
]