diff options
author | kumavis <kumavis@users.noreply.github.com> | 2016-04-16 04:06:28 +0800 |
---|---|---|
committer | kumavis <kumavis@users.noreply.github.com> | 2016-04-16 04:06:28 +0800 |
commit | 83b8741bbb335ad1629b72180616835dbb3f5433 (patch) | |
tree | b8adfc80551c1032dc6fc43aa3a5055169446143 /app/scripts/lib/remote-store.js | |
parent | 81fc1e5f7dc958af40d8113b80c8cfb38c2016ef (diff) | |
parent | d6114292d0c05b42b1d28d75baad8af0c06509a1 (diff) | |
download | tangerine-wallet-browser-83b8741bbb335ad1629b72180616835dbb3f5433.tar tangerine-wallet-browser-83b8741bbb335ad1629b72180616835dbb3f5433.tar.gz tangerine-wallet-browser-83b8741bbb335ad1629b72180616835dbb3f5433.tar.bz2 tangerine-wallet-browser-83b8741bbb335ad1629b72180616835dbb3f5433.tar.lz tangerine-wallet-browser-83b8741bbb335ad1629b72180616835dbb3f5433.tar.xz tangerine-wallet-browser-83b8741bbb335ad1629b72180616835dbb3f5433.tar.zst tangerine-wallet-browser-83b8741bbb335ad1629b72180616835dbb3f5433.zip |
Merge pull request #113 from MetaMask/i53
publicConfigStore for sync provider and selected address
Diffstat (limited to 'app/scripts/lib/remote-store.js')
-rw-r--r-- | app/scripts/lib/remote-store.js | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/app/scripts/lib/remote-store.js b/app/scripts/lib/remote-store.js new file mode 100644 index 000000000..2dbdde811 --- /dev/null +++ b/app/scripts/lib/remote-store.js @@ -0,0 +1,97 @@ +const Dnode = require('dnode') +const inherits = require('util').inherits + +module.exports = { + HostStore: HostStore, + RemoteStore: RemoteStore, +} + +function BaseStore(initState){ + this._state = initState || {} + this._subs = [] +} + +BaseStore.prototype.set = function(key, value){ + throw Error('Not implemented.') +} + +BaseStore.prototype.get = function(key){ + return this._state[key] +} + +BaseStore.prototype.subscribe = function(fn){ + this._subs.push(fn) + var unsubscribe = this.unsubscribe.bind(this, fn) + return unsubscribe +} + +BaseStore.prototype.unsubscribe = function(fn){ + var index = this._subs.indexOf(fn) + if (index !== -1) this._subs.splice(index, 1) +} + +BaseStore.prototype._emitUpdates = function(state){ + this._subs.forEach(function(handler){ + handler(state) + }) +} + +// +// host +// + +inherits(HostStore, BaseStore) +function HostStore(initState, opts){ + BaseStore.call(this, initState) +} + +HostStore.prototype.set = function(key, value){ + this._state[key] = value + process.nextTick(this._emitUpdates.bind(this, this._state)) +} + +HostStore.prototype.createStream = function(){ + var dnode = Dnode({ + // update: this._didUpdate.bind(this), + }) + dnode.on('remote', this._didConnect.bind(this)) + return dnode +} + +HostStore.prototype._didConnect = function(remote){ + this.subscribe(function(state){ + remote.update(state) + }) + remote.update(this._state) +} + +// +// remote +// + +inherits(RemoteStore, BaseStore) +function RemoteStore(initState, opts){ + BaseStore.call(this, initState) + this._remote = null +} + +RemoteStore.prototype.set = function(key, value){ + this._remote.set(key, value) +} + +RemoteStore.prototype.createStream = function(){ + var dnode = Dnode({ + update: this._didUpdate.bind(this), + }) + dnode.once('remote', this._didConnect.bind(this)) + return dnode +} + +RemoteStore.prototype._didConnect = function(remote){ + this._remote = remote +} + +RemoteStore.prototype._didUpdate = function(state){ + this._state = state + this._emitUpdates(state) +} |