diff options
author | kumavis <aaron@kumavis.me> | 2017-01-12 11:04:19 +0800 |
---|---|---|
committer | kumavis <aaron@kumavis.me> | 2017-01-12 11:31:21 +0800 |
commit | 8012ede12698477692b80769781096b559159a32 (patch) | |
tree | 17f75325da8f5d0abf2c1f37fa0573320730b6a7 /app/scripts/lib/observable/remote.js | |
parent | cc5e9aca4fa2a1e78c49be680405d93ac918bccf (diff) | |
download | tangerine-wallet-browser-8012ede12698477692b80769781096b559159a32.tar.gz tangerine-wallet-browser-8012ede12698477692b80769781096b559159a32.tar.zst tangerine-wallet-browser-8012ede12698477692b80769781096b559159a32.zip |
background - introduce ObservableStore
Diffstat (limited to 'app/scripts/lib/observable/remote.js')
-rw-r--r-- | app/scripts/lib/observable/remote.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/app/scripts/lib/observable/remote.js b/app/scripts/lib/observable/remote.js new file mode 100644 index 000000000..b5a3254a2 --- /dev/null +++ b/app/scripts/lib/observable/remote.js @@ -0,0 +1,51 @@ +const Dnode = require('dnode') +const ObservableStore = require('./index') +const endOfStream = require('end-of-stream') + +// +// RemoteStore +// +// connects to a HostStore and receives its latest state +// + +class RemoteStore extends ObservableStore { + + constructor (initState, opts) { + super(initState) + this.opts = opts || {} + this._remote = null + } + + put (newState) { + if (!this._remote) throw new Error('RemoteStore - "put" called before connection to HostStore') + this._put(newState) + this._remote.put(newState) + } + + createStream () { + const self = this + const dnode = Dnode({ + put: (newState) => self._put(newState), + }) + // listen for connection to remote + dnode.once('remote', (remote) => { + // setup connection lifecycle + self._onConnect(remote) + endOfStream(dnode, () => self._onDisconnect()) + }) + return dnode + } + + _onConnect (remote) { + this._remote = remote + this.emit('connected') + } + + _onDisconnect () { + this._remote = null + this.emit('disconnected') + } + +} + +module.exports = RemoteStore
\ No newline at end of file |