blob: 1ff112e95ec90c9222dad6e4157fb9ecc003a30a (
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
34
35
36
37
38
39
40
41
|
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
|