aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorDan Finlay <542863+danfinlay@users.noreply.github.com>2018-08-23 04:31:42 +0800
committerGitHub <noreply@github.com>2018-08-23 04:31:42 +0800
commitf30b726df79f1ffe0ba088dba9f3ca730ee7d103 (patch)
tree8829957eae2f9013dc6def202ff11dcf9280035b /app
parentf495c0e9e03fff740bce5e94c0d14254c2c497e2 (diff)
parentbf6d624e769eb5486b5c491165fad1862435669b (diff)
downloadtangerine-wallet-browser-f30b726df79f1ffe0ba088dba9f3ca730ee7d103.tar
tangerine-wallet-browser-f30b726df79f1ffe0ba088dba9f3ca730ee7d103.tar.gz
tangerine-wallet-browser-f30b726df79f1ffe0ba088dba9f3ca730ee7d103.tar.bz2
tangerine-wallet-browser-f30b726df79f1ffe0ba088dba9f3ca730ee7d103.tar.lz
tangerine-wallet-browser-f30b726df79f1ffe0ba088dba9f3ca730ee7d103.tar.xz
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')
-rw-r--r--app/scripts/lib/account-tracker.js18
-rw-r--r--app/scripts/metamask-controller.js21
2 files changed, 37 insertions, 2 deletions
diff --git a/app/scripts/lib/account-tracker.js b/app/scripts/lib/account-tracker.js
index b7e2c7cbe..3a52d5e8d 100644
--- a/app/scripts/lib/account-tracker.js
+++ b/app/scripts/lib/account-tracker.js
@@ -43,10 +43,24 @@ class AccountTracker {
this._provider = opts.provider
this._query = pify(new EthQuery(this._provider))
this._blockTracker = opts.blockTracker
- // subscribe to latest block
- this._blockTracker.on('latest', this._updateForBlock.bind(this))
// blockTracker.currentBlock may be null
this._currentBlockNumber = this._blockTracker.getCurrentBlock()
+ // bind function for easier listener syntax
+ this._updateForBlock = this._updateForBlock.bind(this)
+ }
+
+ start () {
+ // remove first to avoid double add
+ this._blockTracker.removeListener('latest', this._updateForBlock)
+ // add listener
+ this._blockTracker.addListener('latest', this._updateForBlock)
+ // fetch account balances
+ this._updateAccounts()
+ }
+
+ stop () {
+ // remove listener
+ this._blockTracker.removeListener('latest', this._updateForBlock)
}
/**
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