1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Merge pull request from MetaMask/lazy-account-tracker

Network IO Optimization: Lazy AccountTracker
This commit is contained in:
Dan Finlay 2018-08-22 13:31:42 -07:00 committed by GitHub
commit f30b726df7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 2 deletions

@ -43,10 +43,24 @@ class AccountTracker {
this._provider = opts.provider this._provider = opts.provider
this._query = pify(new EthQuery(this._provider)) this._query = pify(new EthQuery(this._provider))
this._blockTracker = opts.blockTracker this._blockTracker = opts.blockTracker
// subscribe to latest block
this._blockTracker.on('latest', this._updateForBlock.bind(this))
// blockTracker.currentBlock may be null // blockTracker.currentBlock may be null
this._currentBlockNumber = this._blockTracker.getCurrentBlock() 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)
} }
/** /**

@ -67,6 +67,10 @@ module.exports = class MetamaskController extends EventEmitter {
const initState = opts.initState || {} const initState = opts.initState || {}
this.recordFirstTimeInfo(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 // platform-specific api
this.platform = opts.platform this.platform = opts.platform
@ -127,6 +131,14 @@ module.exports = class MetamaskController extends EventEmitter {
provider: this.provider, provider: this.provider,
blockTracker: this.blockTracker, 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 // key mgmt
const additionalKeyrings = [TrezorKeyring, LedgerBridgeKeyring] const additionalKeyrings = [TrezorKeyring, LedgerBridgeKeyring]
@ -1197,11 +1209,19 @@ module.exports = class MetamaskController extends EventEmitter {
setupControllerConnection (outStream) { setupControllerConnection (outStream) {
const api = this.getApi() const api = this.getApi()
const dnode = Dnode(api) const dnode = Dnode(api)
// report new active controller connection
this.activeControllerConnections++
this.emit('controllerConnectionChanged', this.activeControllerConnections)
// connect dnode api to remote connection
pump( pump(
outStream, outStream,
dnode, dnode,
outStream, outStream,
(err) => { (err) => {
// report new active controller connection
this.activeControllerConnections--
this.emit('controllerConnectionChanged', this.activeControllerConnections)
// report any error
if (err) log.error(err) 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. * A method for recording whether the MetaMask user interface is open or not.
* @private * @private