diff options
author | frankiebee <frankie.diamond@gmail.com> | 2017-09-27 02:52:57 +0800 |
---|---|---|
committer | frankiebee <frankie.diamond@gmail.com> | 2017-09-27 04:38:27 +0800 |
commit | b654eb9b1f37cd3757e4614bb048884ab89d2986 (patch) | |
tree | feef7673d9fc25863471a75e0598e3bc5fe548ec /app/scripts | |
parent | 87fc44c45a5bb511fe60b2c3fe7e7c78f5f6584f (diff) | |
download | tangerine-wallet-browser-b654eb9b1f37cd3757e4614bb048884ab89d2986.tar tangerine-wallet-browser-b654eb9b1f37cd3757e4614bb048884ab89d2986.tar.gz tangerine-wallet-browser-b654eb9b1f37cd3757e4614bb048884ab89d2986.tar.bz2 tangerine-wallet-browser-b654eb9b1f37cd3757e4614bb048884ab89d2986.tar.lz tangerine-wallet-browser-b654eb9b1f37cd3757e4614bb048884ab89d2986.tar.xz tangerine-wallet-browser-b654eb9b1f37cd3757e4614bb048884ab89d2986.tar.zst tangerine-wallet-browser-b654eb9b1f37cd3757e4614bb048884ab89d2986.zip |
wrap block tracker in events proxy
Diffstat (limited to 'app/scripts')
-rw-r--r-- | app/scripts/controllers/network.js | 22 | ||||
-rw-r--r-- | app/scripts/lib/events-proxy.js | 31 |
2 files changed, 39 insertions, 14 deletions
diff --git a/app/scripts/controllers/network.js b/app/scripts/controllers/network.js index 0a3e5e26b..6daedbb67 100644 --- a/app/scripts/controllers/network.js +++ b/app/scripts/controllers/network.js @@ -4,6 +4,7 @@ const ObservableStore = require('obs-store') const ComposedStore = require('obs-store/lib/composed') const extend = require('xtend') const EthQuery = require('eth-query') +const createEventEmitterProxy = require('../lib/events-proxy.js') const RPC_ADDRESS_LIST = require('../config.js').network const DEFAULT_RPC = RPC_ADDRESS_LIST['rinkeby'] @@ -31,16 +32,8 @@ module.exports = class NetworkController extends EventEmitter { initializeProvider (opts, providerContructor = MetaMaskProvider) { this.providerInit = opts this._provider = providerContructor(opts) - this._proxy = new Proxy(this._provider, { - get: (obj, name) => { - if (name === 'on') return this._on.bind(this) - return this._provider[name] - }, - set: (obj, name, value) => { - this._provider[name] = value - return value - }, - }) + this._proxy = createEventEmitterProxy(this._provider) + this.provider._blockTracker = createEventEmitterProxy(this._provider._blockTracker) this.provider.on('block', this._logBlock.bind(this)) this.provider.on('error', this.verifyNetwork.bind(this)) this.ethQuery = new EthQuery(this.provider) @@ -55,11 +48,12 @@ module.exports = class NetworkController extends EventEmitter { this._provider.removeAllListeners() this._provider.stop() - this.provider = MetaMaskProvider(newInit) + this._provider = MetaMaskProvider(newInit) // apply the listners created by other controllers - Object.keys(this._providerListeners).forEach((key) => { - this._providerListeners[key].forEach((handler) => this._provider.addListener(key, handler)) - }) + const blockTrackerHandlers = this.provider._blockTracker.proxyEventHandlers + this.provider.setTarget(this._provider) + + this.provider._blockTracker = createEventEmitterProxy(this._provider._blockTracker, blockTrackerHandlers) this.emit('networkDidChange') } diff --git a/app/scripts/lib/events-proxy.js b/app/scripts/lib/events-proxy.js new file mode 100644 index 000000000..d1199a278 --- /dev/null +++ b/app/scripts/lib/events-proxy.js @@ -0,0 +1,31 @@ +module.exports = function createEventEmitterProxy(eventEmitter, listeners) { + let target = eventEmitter + const eventHandlers = listeners || {} + const proxy = new Proxy({}, { + get: (obj, name) => { + // intercept listeners + if (name === 'on') return addListener + if (name === 'setTarget') return setTarget + if (name === 'proxyEventHandlers') return eventHandlers + return target[name] + }, + set: (obj, name, value) => { + target[name] = value + return true + }, + }) + function setTarget (eventEmitter) { + target = eventEmitter + // migrate listeners + Object.keys(eventHandlers).forEach((name) => { + eventHandlers[name].forEach((handler) => target.on(name, handler)) + }) + } + function addListener (name, handler) { + if (!eventHandlers[name]) eventHandlers[name] = [] + eventHandlers[name].push(handler) + target.on(name, handler) + } + if (listeners) proxy.setTarget(eventEmitter) + return proxy +}
\ No newline at end of file |