aboutsummaryrefslogblamecommitdiffstats
path: root/app/scripts/lib/observable/index.js
blob: 1ff112e95ec90c9222dad6e4157fb9ecc003a30a (plain) (tree)
1
2
3
4
5
6
7
8
9








                                                   
                                


                      

                                



                       
                         



                              
                           



                                          



            







                                 
const EventEmitter = require('events').EventEmitter

class ObservableStore extends EventEmitter {

  constructor (initialState) {
    super()
    this._state = initialState
  }

  // wrapper around internal get
  get () {
    return this._state
  }
  
  // wrapper around internal put
  put (newState) {
    this._put(newState)
  }

  // subscribe to changes
  subscribe (handler) {
    this.on('update', handler)
  }

  // unsubscribe to changes
  unsubscribe (handler) {
    this.removeListener('update', handler)
  }

  //
  // private
  //

  _put (newState) {
    this._state = newState
    this.emit('update', newState)
  }

}

module.exports = ObservableStore