diff --git a/app/scripts/controllers/network.js b/app/scripts/controllers/network.js index a1db8946a88c..4ccf365e1cc9 100644 --- a/app/scripts/controllers/network.js +++ b/app/scripts/controllers/network.js @@ -11,6 +11,7 @@ const createEventEmitterProxy = require('../lib/events-proxy.js') const RPC_ADDRESS_LIST = require('../config.js').network const DEFAULT_RPC = RPC_ADDRESS_LIST['rinkeby'] const INFURA_PROVIDER_TYPES = ['ropsten', 'rinkeby', 'kovan', 'mainnet'] +const noop = function () {} module.exports = class NetworkController extends EventEmitter { @@ -58,15 +59,21 @@ module.exports = class NetworkController extends EventEmitter { return this.getNetworkState() === 'loading' } - lookupNetwork () { + lookupNetwork (cb = noop){ // Prevent firing when provider is not defined. if (!this.ethQuery || !this.ethQuery.sendAsync) { return log.warn('NetworkController - lookupNetwork aborted due to missing ethQuery') } this.ethQuery.sendAsync({ method: 'net_version' }, (err, network) => { - if (err) return this.setNetworkState('loading') + if (err) { + this.setNetworkState('loading') + console.error(err) + cb(err) + return + } log.info('web3.getNetwork returned ' + network) this.setNetworkState(network) + cb(null, network) }) } diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 000e17b9ea1e..e083d11c4bf3 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -61,6 +61,21 @@ module.exports = class MetamaskController extends EventEmitter { // network store this.networkController = new NetworkController(initState.NetworkController) + // setup onLine lister for restarting the provider + if (this.platform.addOnLineListener) { + this.platform.addOnLineListener(() => { + // guard against premature firring of the event listener + if (this.provider) { + // start the block tracker up + this.provider.stop() + this.provider.start() + } + // look up the network + this.networkController.lookupNetwork(() => { + this.sendUpdate() + }) + }) + } // config manager this.configManager = new ConfigManager({ store: this.store, @@ -91,6 +106,7 @@ module.exports = class MetamaskController extends EventEmitter { // rpc provider this.provider = this.initializeProvider() + this.blockTracker = this.provider._blockTracker this.recentBlocksController = new RecentBlocksController({ diff --git a/app/scripts/platforms/extension.js b/app/scripts/platforms/extension.js index 2f47512eb272..f3c362f7c397 100644 --- a/app/scripts/platforms/extension.js +++ b/app/scripts/platforms/extension.js @@ -26,6 +26,14 @@ class ExtensionPlatform { cb(e) } } + + isOnline () { + return navigator.onLine + } + + addOnLineListener (cb) { + window.addEventListener('online', cb) + } } module.exports = ExtensionPlatform