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

account-tracker - only track blocks when there are activeControllerConnections

This commit is contained in:
kumavis 2018-08-21 16:49:24 -07:00
parent 3ac2b40dcf
commit a2654108be
2 changed files with 24 additions and 2 deletions

View File

@ -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)
}
/**

View File

@ -131,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]