diff options
Diffstat (limited to 'app/scripts')
-rw-r--r-- | app/scripts/controllers/incoming-transactions.js | 38 | ||||
-rw-r--r-- | app/scripts/lib/util.js | 24 | ||||
-rw-r--r-- | app/scripts/metamask-controller.js | 2 |
3 files changed, 52 insertions, 12 deletions
diff --git a/app/scripts/controllers/incoming-transactions.js b/app/scripts/controllers/incoming-transactions.js index 4b4314427..1a970cdfe 100644 --- a/app/scripts/controllers/incoming-transactions.js +++ b/app/scripts/controllers/incoming-transactions.js @@ -2,7 +2,7 @@ const ObservableStore = require('obs-store') const log = require('loglevel') const BN = require('bn.js') const createId = require('../lib/random-id') -const { bnToHex } = require('../lib/util') +const { bnToHex, fetchWithTimeout } = require('../lib/util') const { MAINNET_CODE, ROPSTEN_CODE, @@ -14,11 +14,14 @@ const { MAINNET, } = require('./network/enums') const networkTypeToIdMap = { - [ROPSTEN]: ROPSTEN_CODE, - [RINKEBY]: RINKEYBY_CODE, - [KOVAN]: KOVAN_CODE, - [MAINNET]: MAINNET_CODE, + [ROPSTEN]: String(ROPSTEN_CODE), + [RINKEBY]: String(RINKEYBY_CODE), + [KOVAN]: String(KOVAN_CODE), + [MAINNET]: String(MAINNET_CODE), } +const fetch = fetchWithTimeout({ + timeout: 30000, +}) class IncomingTransactionsController { @@ -33,6 +36,15 @@ class IncomingTransactionsController { this.preferencesController = preferencesController this.getCurrentNetwork = () => networkController.getProviderConfig().type + this._onLatestBlock = async (newBlockNumberHex) => { + const selectedAddress = this.preferencesController.getSelectedAddress() + const newBlockNumberDec = parseInt(newBlockNumberHex, 16) + await this._update({ + address: selectedAddress, + newBlockNumberDec, + }) + } + const initState = Object.assign({ incomingTransactions: {}, incomingTxLastFetchedBlocksByNetwork: { @@ -51,13 +63,6 @@ class IncomingTransactionsController { networkType: newType, }) }) - this.blockTracker.on('latest', async (newBlockNumberHex) => { - const address = this.preferencesController.getSelectedAddress() - await this._update({ - address, - newBlockNumberDec: parseInt(newBlockNumberHex, 16), - }) - }) this.preferencesController.store.subscribe(async ({ selectedAddress }) => { await this._update({ address: selectedAddress, @@ -65,6 +70,15 @@ class IncomingTransactionsController { }) } + start () { + this.blockTracker.removeListener('latest', this._onLatestBlock) + this.blockTracker.addListener('latest', this._onLatestBlock) + } + + stop () { + this.blockTracker.removeListener('latest', this._onLatestBlock) + } + async _update ({ address, newBlockNumberDec, networkType } = {}) { try { const dataForUpdate = await this._getDataForUpdate({ address, newBlockNumberDec, networkType }) diff --git a/app/scripts/lib/util.js b/app/scripts/lib/util.js index 2eb71c0a0..096334794 100644 --- a/app/scripts/lib/util.js +++ b/app/scripts/lib/util.js @@ -144,6 +144,29 @@ function removeListeners (listeners, emitter) { }) } +function fetchWithTimeout ({ timeout = 120000 } = {}) { + return async function _fetch (url, opts) { + const abortController = new AbortController() + const abortSignal = abortController.signal + const f = fetch(url, { + ...opts, + signal: abortSignal, + }) + + const timer = setTimeout(() => abortController.abort(), timeout) + + try { + const res = await f + clearTimeout(timer) + return res + } catch (e) { + clearTimeout(timer) + throw e + } + } +} + + module.exports = { removeListeners, applyListeners, @@ -154,4 +177,5 @@ module.exports = { hexToBn, bnToHex, BnMultiplyByFraction, + fetchWithTimeout, } diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 14fa143f4..b430ea8b9 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -156,8 +156,10 @@ module.exports = class MetamaskController extends EventEmitter { this.on('controllerConnectionChanged', (activeControllerConnections) => { if (activeControllerConnections > 0) { this.accountTracker.start() + this.incomingTransactionsController.start() } else { this.accountTracker.stop() + this.incomingTransactionsController.stop() } }) |