aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/observable/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'app/scripts/lib/observable/index.js')
-rw-r--r--app/scripts/lib/observable/index.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/app/scripts/lib/observable/index.js b/app/scripts/lib/observable/index.js
new file mode 100644
index 000000000..1ff112e95
--- /dev/null
+++ b/app/scripts/lib/observable/index.js
@@ -0,0 +1,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