aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2017-09-28 05:44:13 +0800
committerkumavis <aaron@kumavis.me>2017-09-28 05:44:13 +0800
commitf2d9b75e94ba52ce34faff0640f494028d037246 (patch)
treee58d0c6d6f62313f25fea62160c946fb1759ce40
parentc781e11c7a4b8c6d0c74cc741af4d805d8bac00f (diff)
downloadtangerine-wallet-browser-f2d9b75e94ba52ce34faff0640f494028d037246.tar
tangerine-wallet-browser-f2d9b75e94ba52ce34faff0640f494028d037246.tar.gz
tangerine-wallet-browser-f2d9b75e94ba52ce34faff0640f494028d037246.tar.bz2
tangerine-wallet-browser-f2d9b75e94ba52ce34faff0640f494028d037246.tar.lz
tangerine-wallet-browser-f2d9b75e94ba52ce34faff0640f494028d037246.tar.xz
tangerine-wallet-browser-f2d9b75e94ba52ce34faff0640f494028d037246.tar.zst
tangerine-wallet-browser-f2d9b75e94ba52ce34faff0640f494028d037246.zip
network controller - refactor to use _setProvider
-rw-r--r--app/scripts/controllers/network.js45
-rw-r--r--test/unit/network-contoller-test.js6
2 files changed, 31 insertions, 20 deletions
diff --git a/app/scripts/controllers/network.js b/app/scripts/controllers/network.js
index 253a365e2..1ed9f7eca 100644
--- a/app/scripts/controllers/network.js
+++ b/app/scripts/controllers/network.js
@@ -15,17 +15,16 @@ module.exports = class NetworkController extends EventEmitter {
this.networkStore = new ObservableStore('loading')
this.providerStore = new ObservableStore(config.provider)
this.store = new ComposedStore({ provider: this.providerStore, network: this.networkStore })
- this._providerListeners = {}
+ this._proxy = createEventEmitterProxy()
this.on('networkDidChange', this.lookupNetwork)
- this.providerStore.subscribe((state) => this.switchNetwork({rpcUrl: state.rpcTarget}))
+ this.providerStore.subscribe((state) => this.switchNetwork({ rpcUrl: state.rpcTarget }))
}
initializeProvider (opts, providerContructor = MetaMaskProvider) {
- this._providerInit = opts
- this._provider = providerContructor(opts)
- this._proxy = createEventEmitterProxy(this._provider)
- this._proxy._blockTracker = createEventEmitterProxy(this._provider._blockTracker)
+ this._baseProviderParams = opts
+ const provider = providerContructor(opts)
+ this._setProvider(provider)
this._proxy.on('block', this._logBlock.bind(this))
this._proxy.on('error', this.verifyNetwork.bind(this))
this.ethQuery = new EthQuery(this._proxy)
@@ -33,21 +32,33 @@ module.exports = class NetworkController extends EventEmitter {
return this._proxy
}
- switchNetwork (providerInit) {
+ switchNetwork (opts) {
this.setNetworkState('loading')
- const newInit = extend(this._providerInit, providerInit)
- this._providerInit = newInit
-
- this._proxy.removeAllListeners()
- this._proxy.stop()
- this._provider = MetaMaskProvider(newInit)
- // apply the listners created by other controllers
- const blockTrackerHandlers = this._proxy._blockTracker.proxyEventHandlers
- this._proxy.setTarget(this._provider)
- this._proxy._blockTracker = createEventEmitterProxy(this._provider._blockTracker, blockTrackerHandlers)
+ const providerParams = extend(this._baseProviderParams, opts)
+ this._baseProviderParams = providerParams
+ const provider = MetaMaskProvider(providerParams)
+ this._setProvider(provider)
this.emit('networkDidChange')
}
+ _setProvider (provider) {
+ // collect old block tracker events
+ const oldProvider = this._provider
+ let blockTrackerHandlers
+ if (oldProvider) {
+ // capture old block handlers
+ blockTrackerHandlers = oldProvider._blockTracker.proxyEventHandlers
+ // tear down
+ oldProvider.removeAllListeners()
+ oldProvider.stop()
+ }
+ // override block tracler
+ provider._blockTracker = createEventEmitterProxy(provider._blockTracker, blockTrackerHandlers)
+ // set as new provider
+ this._provider = provider
+ this._proxy.setTarget(provider)
+ }
+
verifyNetwork () {
// Check network when restoring connectivity:
if (this.isNetworkLoading()) this.lookupNetwork()
diff --git a/test/unit/network-contoller-test.js b/test/unit/network-contoller-test.js
index c1fdaf032..0b3b5adeb 100644
--- a/test/unit/network-contoller-test.js
+++ b/test/unit/network-contoller-test.js
@@ -20,9 +20,9 @@ describe('# Network Controller', function () {
describe('#provider', function () {
it('provider should be updatable without reassignment', function () {
networkController.initializeProvider(networkControllerProviderInit, dummyProviderConstructor)
- const provider = networkController.provider
- networkController.provider.setTarget({test: true, on: () => {}})
- assert.ok(provider.test)
+ const proxy = networkController._proxy
+ proxy.setTarget({ test: true, on: () => {} })
+ assert.ok(proxy.test)
})
})
describe('#getNetworkState', function () {