mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 18:00:18 +01:00
recent-blocks - dont listen for block when on infura providers
This commit is contained in:
parent
fbc2d5880f
commit
7e64a578a4
@ -158,7 +158,7 @@ module.exports = class NetworkController extends EventEmitter {
|
||||
_switchNetwork (opts) {
|
||||
this.setNetworkState('loading')
|
||||
this._configureProvider(opts)
|
||||
this.emit('networkDidChange')
|
||||
this.emit('networkDidChange', opts.type)
|
||||
}
|
||||
|
||||
_configureProvider (opts) {
|
||||
|
@ -3,6 +3,14 @@ const extend = require('xtend')
|
||||
const EthQuery = require('eth-query')
|
||||
const log = require('loglevel')
|
||||
const pify = require('pify')
|
||||
const {
|
||||
ROPSTEN,
|
||||
RINKEBY,
|
||||
KOVAN,
|
||||
MAINNET,
|
||||
} = require('./network/enums')
|
||||
const INFURA_PROVIDER_TYPES = [ROPSTEN, RINKEBY, KOVAN, MAINNET]
|
||||
|
||||
|
||||
class RecentBlocksController {
|
||||
|
||||
@ -24,7 +32,7 @@ class RecentBlocksController {
|
||||
*
|
||||
*/
|
||||
constructor (opts = {}) {
|
||||
const { blockTracker, provider } = opts
|
||||
const { blockTracker, provider, networkController } = opts
|
||||
this.blockTracker = blockTracker
|
||||
this.ethQuery = new EthQuery(provider)
|
||||
this.historyLength = opts.historyLength || 40
|
||||
@ -33,13 +41,30 @@ class RecentBlocksController {
|
||||
recentBlocks: [],
|
||||
}, opts.initState)
|
||||
this.store = new ObservableStore(initState)
|
||||
|
||||
this.blockTracker.on('latest', async (newBlockNumberHex) => {
|
||||
const blockListner = async (newBlockNumberHex) => {
|
||||
try {
|
||||
await this.processBlock(newBlockNumberHex)
|
||||
} catch (err) {
|
||||
log.error(err)
|
||||
}
|
||||
}
|
||||
let isListeng = false
|
||||
const { type } = networkController.getProviderConfig()
|
||||
if (!INFURA_PROVIDER_TYPES.includes(type) && type !== 'loading') {
|
||||
this.blockTracker.on('latest', blockListner)
|
||||
isListeng = true
|
||||
}
|
||||
networkController.on('networkDidChange', (newType) => {
|
||||
if (INFURA_PROVIDER_TYPES.includes(newType) && isListeng) {
|
||||
this.blockTracker.removeListener('latest', blockListner)
|
||||
} else if (
|
||||
!INFURA_PROVIDER_TYPES.includes(type) &&
|
||||
type !== 'loading' &&
|
||||
!isListeng
|
||||
) {
|
||||
this.blockTracker.on('latest', blockListner)
|
||||
|
||||
}
|
||||
})
|
||||
this.backfill()
|
||||
}
|
||||
|
@ -126,6 +126,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
||||
this.recentBlocksController = new RecentBlocksController({
|
||||
blockTracker: this.blockTracker,
|
||||
provider: this.provider,
|
||||
networkController: this.networkController,
|
||||
})
|
||||
|
||||
// account tracker watches balances, nonces, and any code at their address.
|
||||
|
Loading…
Reference in New Issue
Block a user