'use strict'

var assign = require('object-assign')
var EventEmitter = require('eventemitter3')
var objEql = require('obj-eql')

function createStore(obj) {
  return assign(Object.create(EventEmitter.prototype), {
    state: {},
    setState: function(change) {
      this.replaceState(assign({}, this.state, change))
    },
    replaceState: function(newState) {
      if (!objEql(newState, this.state)) {
        this.state = newState
        this.emit('change')
      }
    }
  }, obj)
}

module.exports = createStore

'use strict'

var assign = require('object-assign')
var EventEmitter = require('eventemitter3')
var objEql = require('obj-eql')

function createStore(obj) {
  return assign(Object.create(EventEmitter.prototype), {
    state: {},
    setState: function(change) {
      this.replaceState(assign({}, this.state, change))
    },
    replaceState: function(newState) {
      if (!objEql(newState, this.state)) {
        newState.store = this
        this.state = newState
        this.emit('change')
      }
    }
  }, obj)
}

module.exports = createStore

Add a circular reference from the store state back to the store.
Add a circular reference from the store state back to the store.

Useful if store state is used as a prop, allows access back to store methods
