diff options
author | Dan Finlay <542863+danfinlay@users.noreply.github.com> | 2018-08-23 04:31:42 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-23 04:31:42 +0800 |
commit | f30b726df79f1ffe0ba088dba9f3ca730ee7d103 (patch) | |
tree | 8829957eae2f9013dc6def202ff11dcf9280035b /app/scripts/metamask-controller.js | |
parent | f495c0e9e03fff740bce5e94c0d14254c2c497e2 (diff) | |
parent | bf6d624e769eb5486b5c491165fad1862435669b (diff) | |
download | tangerine-wallet-browser-f30b726df79f1ffe0ba088dba9f3ca730ee7d103.tar.gz tangerine-wallet-browser-f30b726df79f1ffe0ba088dba9f3ca730ee7d103.tar.zst tangerine-wallet-browser-f30b726df79f1ffe0ba088dba9f3ca730ee7d103.zip |
Merge pull request #5122 from MetaMask/lazy-account-tracker
Network IO Optimization: Lazy AccountTracker
Diffstat (limited to 'app/scripts/metamask-controller.js')
-rw-r--r-- | app/scripts/metamask-controller.js | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 4ee88186a..a6215d51b 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -67,6 +67,10 @@ module.exports = class MetamaskController extends EventEmitter { const initState = opts.initState || {} this.recordFirstTimeInfo(initState) + // this keeps track of how many "controllerStream" connections are open + // the only thing that uses controller connections are open metamask UI instances + this.activeControllerConnections = 0 + // platform-specific api this.platform = opts.platform @@ -127,6 +131,14 @@ module.exports = class MetamaskController extends EventEmitter { provider: this.provider, blockTracker: this.blockTracker, }) + // start and stop polling for balances based on activeControllerConnections + this.on('controllerConnectionChanged', (activeControllerConnections) => { + if (activeControllerConnections > 0) { + this.accountTracker.start() + } else { + this.accountTracker.stop() + } + }) // key mgmt const additionalKeyrings = [TrezorKeyring, LedgerBridgeKeyring] @@ -1197,11 +1209,19 @@ module.exports = class MetamaskController extends EventEmitter { setupControllerConnection (outStream) { const api = this.getApi() const dnode = Dnode(api) + // report new active controller connection + this.activeControllerConnections++ + this.emit('controllerConnectionChanged', this.activeControllerConnections) + // connect dnode api to remote connection pump( outStream, dnode, outStream, (err) => { + // report new active controller connection + this.activeControllerConnections-- + this.emit('controllerConnectionChanged', this.activeControllerConnections) + // report any error if (err) log.error(err) } ) @@ -1440,6 +1460,7 @@ module.exports = class MetamaskController extends EventEmitter { } } + // TODO: Replace isClientOpen methods with `controllerConnectionChanged` events. /** * A method for recording whether the MetaMask user interface is open or not. * @private |