aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/observable
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2017-01-12 14:47:56 +0800
committerkumavis <aaron@kumavis.me>2017-01-12 14:47:56 +0800
commit3bc996878b467e1fa5fd63656bd465377daa137d (patch)
tree9c1661af778b58cd6d83c7d5fb5727f7a61b6dd9 /app/scripts/lib/observable
parent2019c02fc0fc33d4bd98416654370250d7aa0ca6 (diff)
downloadtangerine-wallet-browser-3bc996878b467e1fa5fd63656bd465377daa137d.tar.gz
tangerine-wallet-browser-3bc996878b467e1fa5fd63656bd465377daa137d.tar.zst
tangerine-wallet-browser-3bc996878b467e1fa5fd63656bd465377daa137d.zip
background - move pojo migrator to outside of metamask controller
Diffstat (limited to 'app/scripts/lib/observable')
-rw-r--r--app/scripts/lib/observable/host.js4
-rw-r--r--app/scripts/lib/observable/index.js10
-rw-r--r--app/scripts/lib/observable/local-storage.js37
-rw-r--r--app/scripts/lib/observable/remote.js2
-rw-r--r--app/scripts/lib/observable/util/sync.js24
-rw-r--r--app/scripts/lib/observable/util/transform.js13
6 files changed, 73 insertions, 17 deletions
diff --git a/app/scripts/lib/observable/host.js b/app/scripts/lib/observable/host.js
index 69f674be8..d1b110503 100644
--- a/app/scripts/lib/observable/host.js
+++ b/app/scripts/lib/observable/host.js
@@ -12,14 +12,14 @@ class HostStore extends ObservableStore {
constructor (initState, opts) {
super(initState)
- this.opts = opts || {}
+ this._opts = opts || {}
}
createStream () {
const self = this
// setup remotely exposed api
let remoteApi = {}
- if (!self.opts.readOnly) {
+ if (!self._opts.readOnly) {
remoteApi.put = (newState) => self.put(newState)
}
// listen for connection to remote
diff --git a/app/scripts/lib/observable/index.js b/app/scripts/lib/observable/index.js
index d193e5554..1ff112e95 100644
--- a/app/scripts/lib/observable/index.js
+++ b/app/scripts/lib/observable/index.js
@@ -7,22 +7,30 @@ class ObservableStore extends EventEmitter {
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)
diff --git a/app/scripts/lib/observable/local-storage.js b/app/scripts/lib/observable/local-storage.js
new file mode 100644
index 000000000..6ed3860f6
--- /dev/null
+++ b/app/scripts/lib/observable/local-storage.js
@@ -0,0 +1,37 @@
+const ObservableStore = require('./index')
+
+//
+// LocalStorageStore
+//
+// uses localStorage instead of a cache
+//
+
+class LocalStorageStore extends ObservableStore {
+
+ constructor (opts) {
+ super()
+ delete this._state
+
+ this._opts = opts || {}
+ if (!this._opts.storageKey) {
+ throw new Error('LocalStorageStore - no "storageKey" specified')
+ }
+ this._storageKey = this._opts.storageKey
+ }
+
+ get() {
+ try {
+ return JSON.parse(global.localStorage[this._storageKey])
+ } catch (err) {
+ return undefined
+ }
+ }
+
+ _put(newState) {
+ global.localStorage[this._storageKey] = JSON.stringify(newState)
+ this.emit('update', newState)
+ }
+
+}
+
+module.exports = LocalStorageStore
diff --git a/app/scripts/lib/observable/remote.js b/app/scripts/lib/observable/remote.js
index b5a3254a2..603f6f0b8 100644
--- a/app/scripts/lib/observable/remote.js
+++ b/app/scripts/lib/observable/remote.js
@@ -12,7 +12,7 @@ class RemoteStore extends ObservableStore {
constructor (initState, opts) {
super(initState)
- this.opts = opts || {}
+ this._opts = opts || {}
this._remote = null
}
diff --git a/app/scripts/lib/observable/util/sync.js b/app/scripts/lib/observable/util/sync.js
new file mode 100644
index 000000000..c61feb02e
--- /dev/null
+++ b/app/scripts/lib/observable/util/sync.js
@@ -0,0 +1,24 @@
+
+//
+// synchronizeStore(inStore, outStore, stateTransform)
+//
+// keeps outStore synchronized with inStore, via an optional stateTransform
+//
+
+module.exports = synchronizeStore
+
+
+function synchronizeStore(inStore, outStore, stateTransform) {
+ stateTransform = stateTransform || transformNoop
+ const initState = stateTransform(inStore.get())
+ outStore.put(initState)
+ inStore.subscribe((inState) => {
+ const outState = stateTransform(inState)
+ outStore.put(outState)
+ })
+ return outStore
+}
+
+function transformNoop(state) {
+ return state
+} \ No newline at end of file
diff --git a/app/scripts/lib/observable/util/transform.js b/app/scripts/lib/observable/util/transform.js
deleted file mode 100644
index 87946f402..000000000
--- a/app/scripts/lib/observable/util/transform.js
+++ /dev/null
@@ -1,13 +0,0 @@
-
-module.exports = transformStore
-
-
-function transformStore(inStore, outStore, stateTransform) {
- const initState = stateTransform(inStore.get())
- outStore.put(initState)
- inStore.subscribe((inState) => {
- const outState = stateTransform(inState)
- outStore.put(outState)
- })
- return outStore
-} \ No newline at end of file