diff options
author | frankiebee <frankie.diamond@gmail.com> | 2019-02-09 05:24:55 +0800 |
---|---|---|
committer | frankiebee <frankie.diamond@gmail.com> | 2019-02-12 03:16:03 +0800 |
commit | 7e64a578a49edcbf4162bc61b7814885286a35e0 (patch) | |
tree | 33e7266598c267ee23c900dca32c9fef42d42f6a /app/scripts/controllers/recent-blocks.js | |
parent | fbc2d5880f89550a3a31ae1c5d03e07f4c72e45b (diff) | |
download | tangerine-wallet-browser-7e64a578a49edcbf4162bc61b7814885286a35e0.tar tangerine-wallet-browser-7e64a578a49edcbf4162bc61b7814885286a35e0.tar.gz tangerine-wallet-browser-7e64a578a49edcbf4162bc61b7814885286a35e0.tar.bz2 tangerine-wallet-browser-7e64a578a49edcbf4162bc61b7814885286a35e0.tar.lz tangerine-wallet-browser-7e64a578a49edcbf4162bc61b7814885286a35e0.tar.xz tangerine-wallet-browser-7e64a578a49edcbf4162bc61b7814885286a35e0.tar.zst tangerine-wallet-browser-7e64a578a49edcbf4162bc61b7814885286a35e0.zip |
recent-blocks - dont listen for block when on infura providers
Diffstat (limited to 'app/scripts/controllers/recent-blocks.js')
-rw-r--r-- | app/scripts/controllers/recent-blocks.js | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/app/scripts/controllers/recent-blocks.js b/app/scripts/controllers/recent-blocks.js index d270f6f44..b88aaeb0c 100644 --- a/app/scripts/controllers/recent-blocks.js +++ b/app/scripts/controllers/recent-blocks.js @@ -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() } |