From c781e11c7a4b8c6d0c74cc741af4d805d8bac00f Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 27 Sep 2017 14:10:58 -0700 Subject: network - remove getter/setter --- app/scripts/controllers/network.js | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) (limited to 'app/scripts/controllers') diff --git a/app/scripts/controllers/network.js b/app/scripts/controllers/network.js index dc9978043..253a365e2 100644 --- a/app/scripts/controllers/network.js +++ b/app/scripts/controllers/network.js @@ -11,8 +11,8 @@ const DEFAULT_RPC = RPC_ADDRESS_LIST['rinkeby'] module.exports = class NetworkController extends EventEmitter { constructor (config) { super() - this.networkStore = new ObservableStore('loading') config.provider.rpcTarget = this.getRpcAddressForType(config.provider.type, config.provider) + this.networkStore = new ObservableStore('loading') this.providerStore = new ObservableStore(config.provider) this.store = new ComposedStore({ provider: this.providerStore, network: this.networkStore }) this._providerListeners = {} @@ -21,42 +21,33 @@ module.exports = class NetworkController extends EventEmitter { this.providerStore.subscribe((state) => this.switchNetwork({rpcUrl: state.rpcTarget})) } - get provider () { - return this._proxy - } - - set provider (provider) { - this._provider = provider - } - initializeProvider (opts, providerContructor = MetaMaskProvider) { - this.providerInit = opts + this._providerInit = opts this._provider = providerContructor(opts) 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) + this._proxy._blockTracker = createEventEmitterProxy(this._provider._blockTracker) + this._proxy.on('block', this._logBlock.bind(this)) + this._proxy.on('error', this.verifyNetwork.bind(this)) + this.ethQuery = new EthQuery(this._proxy) this.lookupNetwork() - return this.provider + return this._proxy } switchNetwork (providerInit) { this.setNetworkState('loading') - const newInit = extend(this.providerInit, providerInit) - this.providerInit = newInit + const newInit = extend(this._providerInit, providerInit) + this._providerInit = newInit - this._provider.removeAllListeners() - this._provider.stop() + this._proxy.removeAllListeners() + this._proxy.stop() this._provider = MetaMaskProvider(newInit) // apply the listners created by other controllers - const blockTrackerHandlers = this.provider._blockTracker.proxyEventHandlers - this.provider.setTarget(this._provider) - this.provider._blockTracker = createEventEmitterProxy(this._provider._blockTracker, blockTrackerHandlers) + const blockTrackerHandlers = this._proxy._blockTracker.proxyEventHandlers + this._proxy.setTarget(this._provider) + this._proxy._blockTracker = createEventEmitterProxy(this._provider._blockTracker, blockTrackerHandlers) this.emit('networkDidChange') } - verifyNetwork () { // Check network when restoring connectivity: if (this.isNetworkLoading()) this.lookupNetwork() -- cgit v1.2.3 From f2d9b75e94ba52ce34faff0640f494028d037246 Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 27 Sep 2017 14:44:13 -0700 Subject: network controller - refactor to use _setProvider --- app/scripts/controllers/network.js | 45 ++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 17 deletions(-) (limited to 'app/scripts/controllers') 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() -- cgit v1.2.3 From 06b5dd2096b6bfcdf7d9ebf7c9bb1e40c8aed2e0 Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 27 Sep 2017 14:44:54 -0700 Subject: network controller - move _setProvider to bottom --- app/scripts/controllers/network.js | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'app/scripts/controllers') diff --git a/app/scripts/controllers/network.js b/app/scripts/controllers/network.js index 1ed9f7eca..2a17cdae8 100644 --- a/app/scripts/controllers/network.js +++ b/app/scripts/controllers/network.js @@ -41,24 +41,6 @@ module.exports = class NetworkController extends EventEmitter { 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() @@ -112,6 +94,24 @@ module.exports = class NetworkController extends EventEmitter { return provider && provider.rpcTarget ? provider.rpcTarget : DEFAULT_RPC } + _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) + } + _logBlock (block) { log.info(`BLOCK CHANGED: #${block.number.toString('hex')} 0x${block.hash.toString('hex')}`) this.verifyNetwork() -- cgit v1.2.3