wrap block tracker in events proxy

feature/default_network_editable
frankiebee 7 years ago
parent 87fc44c45a
commit b654eb9b1f
  1. 22
      app/scripts/controllers/network.js
  2. 31
      app/scripts/lib/events-proxy.js
  3. 1
      test/unit/network-contoller-test.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')
}

@ -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
}

@ -71,6 +71,7 @@ function dummyProviderConstructor() {
// provider
sendAsync: noop,
// block tracker
_blockTracker: {},
start: noop,
stop: noop,
on: noop,

Loading…
Cancel
Save