mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Combine fetch-with-timeout implementations (#7084)
There were two competing utility functions for calling fetch with a timeout. They have been combined into one.
This commit is contained in:
parent
3e9d247d4b
commit
1e7b37d1cc
@ -2,7 +2,8 @@ const ObservableStore = require('obs-store')
|
|||||||
const log = require('loglevel')
|
const log = require('loglevel')
|
||||||
const BN = require('bn.js')
|
const BN = require('bn.js')
|
||||||
const createId = require('../lib/random-id')
|
const createId = require('../lib/random-id')
|
||||||
const { bnToHex, fetchWithTimeout } = require('../lib/util')
|
const { bnToHex } = require('../lib/util')
|
||||||
|
import fetchWithTimeout from '../lib/fetch-with-timeout'
|
||||||
const {
|
const {
|
||||||
MAINNET_CODE,
|
MAINNET_CODE,
|
||||||
ROPSTEN_CODE,
|
ROPSTEN_CODE,
|
||||||
|
23
app/scripts/lib/fetch-with-timeout.js
Normal file
23
app/scripts/lib/fetch-with-timeout.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
const 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default fetchWithTimeout
|
@ -144,29 +144,6 @@ 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 = {
|
module.exports = {
|
||||||
removeListeners,
|
removeListeners,
|
||||||
applyListeners,
|
applyListeners,
|
||||||
@ -177,5 +154,4 @@ module.exports = {
|
|||||||
hexToBn,
|
hexToBn,
|
||||||
bnToHex,
|
bnToHex,
|
||||||
BnMultiplyByFraction,
|
BnMultiplyByFraction,
|
||||||
fetchWithTimeout,
|
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
import assert from 'assert'
|
import assert from 'assert'
|
||||||
import nock from 'nock'
|
import nock from 'nock'
|
||||||
|
|
||||||
import http from './fetch'
|
import fetchWithTimeout from '../../../app/scripts/lib/fetch-with-timeout'
|
||||||
|
|
||||||
describe('custom fetch fn', () => {
|
describe('fetchWithTimeout', () => {
|
||||||
it('fetches a url', async () => {
|
it('fetches a url', async () => {
|
||||||
nock('https://api.infura.io')
|
nock('https://api.infura.io')
|
||||||
.get('/money')
|
.get('/money')
|
||||||
.reply(200, '{"hodl": false}')
|
.reply(200, '{"hodl": false}')
|
||||||
|
|
||||||
const fetch = http()
|
const fetch = fetchWithTimeout()
|
||||||
const response = await (await fetch('https://api.infura.io/money')).json()
|
const response = await (await fetch('https://api.infura.io/money')).json()
|
||||||
assert.deepEqual(response, {
|
assert.deepEqual(response, {
|
||||||
hodl: false,
|
hodl: false,
|
||||||
@ -22,7 +22,7 @@ describe('custom fetch fn', () => {
|
|||||||
.delay(2000)
|
.delay(2000)
|
||||||
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}')
|
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}')
|
||||||
|
|
||||||
const fetch = http({
|
const fetch = fetchWithTimeout({
|
||||||
timeout: 123,
|
timeout: 123,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -40,7 +40,7 @@ describe('custom fetch fn', () => {
|
|||||||
.delay(2000)
|
.delay(2000)
|
||||||
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}')
|
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}')
|
||||||
|
|
||||||
const fetch = http({
|
const fetch = fetchWithTimeout({
|
||||||
timeout: 123,
|
timeout: 123,
|
||||||
})
|
})
|
||||||
|
|
@ -2,7 +2,7 @@ import {
|
|||||||
loadLocalStorageData,
|
loadLocalStorageData,
|
||||||
saveLocalStorageData,
|
saveLocalStorageData,
|
||||||
} from '../../../lib/local-storage-helpers'
|
} from '../../../lib/local-storage-helpers'
|
||||||
import fetchWithTimeout from './fetch'
|
import fetchWithTimeout from '../../../../app/scripts/lib/fetch-with-timeout'
|
||||||
|
|
||||||
const fetchWithCache = async (url, fetchOptions = {}, { cacheRefreshTime = 360000, timeout = 30000 } = {}) => {
|
const fetchWithCache = async (url, fetchOptions = {}, { cacheRefreshTime = 360000, timeout = 30000 } = {}) => {
|
||||||
if (fetchOptions.body || (fetchOptions.method && fetchOptions.method !== 'GET')) {
|
if (fetchOptions.body || (fetchOptions.method && fetchOptions.method !== 'GET')) {
|
||||||
|
@ -1,23 +0,0 @@
|
|||||||
export default function ({ timeout = 120000 } = {}) {
|
|
||||||
return function _fetch (url, opts) {
|
|
||||||
return new Promise(async (resolve, reject) => {
|
|
||||||
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 resolve(res)
|
|
||||||
} catch (e) {
|
|
||||||
clearTimeout(timer)
|
|
||||||
return reject(e)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user