blob: 1ff112e95ec90c9222dad6e4157fb9ecc003a30a (
plain) (
tree)
|
|
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
|