aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/observable/index.js
blob: d193e5554cca62af93d6c655221c8e7f9078c666 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const EventEmitter = require('events').EventEmitter

class ObservableStore extends EventEmitter {

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

  get () {
    return this._state
  }

  put (newState) {
    this._put(newState)
  }

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

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

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

}

module.exports = ObservableStore