commit
092d7c3662
1 changed files with 43 additions and 51 deletions
|
|
@ -25,73 +25,54 @@ const delay = ms => new Promise(res => setTimeout(res, ms))
|
||||||
const mySecret = () => Promise.resolve(require('../../Mediator').getMySecret())
|
const mySecret = () => Promise.resolve(require('../../Mediator').getMySecret())
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @template T
|
* Just a pointer.
|
||||||
* @param {Promise<T>} promise
|
|
||||||
* @returns {Promise<T>}
|
|
||||||
*/
|
*/
|
||||||
const timeout10 = promise => {
|
const TIMEOUT_PTR = {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number} ms Milliseconds
|
||||||
|
* @returns {<T>(promise: Promise<T>) => Promise<T>}
|
||||||
|
*/
|
||||||
|
const timeout = ms => async promise => {
|
||||||
/** @type {NodeJS.Timeout} */
|
/** @type {NodeJS.Timeout} */
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
let timeoutID
|
let timeoutID
|
||||||
return Promise.race([
|
|
||||||
|
const result = await Promise.race([
|
||||||
promise.then(v => {
|
promise.then(v => {
|
||||||
clearTimeout(timeoutID)
|
clearTimeout(timeoutID)
|
||||||
return v
|
return v
|
||||||
}),
|
}),
|
||||||
|
|
||||||
new Promise((_, rej) => {
|
CommonUtils.makePromise(res => {
|
||||||
timeoutID = setTimeout(() => {
|
timeoutID = setTimeout(() => {
|
||||||
rej(new Error(Constants.ErrorCode.TIMEOUT_ERR))
|
clearTimeout(timeoutID)
|
||||||
}, 10000)
|
res(TIMEOUT_PTR)
|
||||||
|
}, ms)
|
||||||
})
|
})
|
||||||
])
|
])
|
||||||
|
|
||||||
|
if (result === TIMEOUT_PTR) {
|
||||||
|
throw new Error(Constants.TIMEOUT_ERR)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @template T
|
* Time outs at 10 seconds.
|
||||||
* @param {Promise<T>} promise
|
|
||||||
* @returns {Promise<T>}
|
|
||||||
*/
|
*/
|
||||||
const timeout5 = promise => {
|
const timeout10 = timeout(10)
|
||||||
/** @type {NodeJS.Timeout} */
|
|
||||||
// @ts-ignore
|
|
||||||
let timeoutID
|
|
||||||
return Promise.race([
|
|
||||||
promise.then(v => {
|
|
||||||
clearTimeout(timeoutID)
|
|
||||||
return v
|
|
||||||
}),
|
|
||||||
|
|
||||||
new Promise((_, rej) => {
|
|
||||||
timeoutID = setTimeout(() => {
|
|
||||||
rej(new Error(Constants.ErrorCode.TIMEOUT_ERR))
|
|
||||||
}, 5000)
|
|
||||||
})
|
|
||||||
])
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @template T
|
* Time outs at 5 seconds.
|
||||||
* @param {Promise<T>} promise
|
|
||||||
* @returns {Promise<T>}
|
|
||||||
*/
|
*/
|
||||||
const timeout2 = promise => {
|
const timeout5 = timeout(5)
|
||||||
/** @type {NodeJS.Timeout} */
|
|
||||||
// @ts-ignore
|
|
||||||
let timeoutID
|
|
||||||
return Promise.race([
|
|
||||||
promise.then(v => {
|
|
||||||
clearTimeout(timeoutID)
|
|
||||||
return v
|
|
||||||
}),
|
|
||||||
|
|
||||||
new Promise((_, rej) => {
|
/**
|
||||||
timeoutID = setTimeout(() => {
|
* Time outs at 2 seconds.
|
||||||
rej(new Error(Constants.ErrorCode.TIMEOUT_ERR))
|
*/
|
||||||
}, 2000)
|
const timeout2 = timeout(2)
|
||||||
})
|
|
||||||
])
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @template T
|
* @template T
|
||||||
|
|
@ -180,7 +161,9 @@ const tryAndWait = async (promGen, shouldRetry = () => false) => {
|
||||||
*/
|
*/
|
||||||
const pubToEpub = async pub => {
|
const pubToEpub = async pub => {
|
||||||
try {
|
try {
|
||||||
const epub = await timeout10(
|
const TIMEOUT_PTR = {}
|
||||||
|
|
||||||
|
const epubOrTimeout = await Promise.race([
|
||||||
CommonUtils.makePromise(res => {
|
CommonUtils.makePromise(res => {
|
||||||
require('../../Mediator/index')
|
require('../../Mediator/index')
|
||||||
.getGun()
|
.getGun()
|
||||||
|
|
@ -191,14 +174,23 @@ const pubToEpub = async pub => {
|
||||||
res(data)
|
res(data)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}),
|
||||||
|
CommonUtils.makePromise(res => {
|
||||||
|
setTimeout(() => {
|
||||||
|
res(TIMEOUT_PTR)
|
||||||
|
}, 10000)
|
||||||
})
|
})
|
||||||
)
|
])
|
||||||
|
|
||||||
return epub
|
if (epubOrTimeout === TIMEOUT_PTR) {
|
||||||
|
throw new Error(`Timeout inside pubToEpub()`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return epubOrTimeout
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error(`Error inside pubToEpub:`)
|
logger.error(`Error inside pubToEpub:`)
|
||||||
logger.error(err)
|
logger.error(err)
|
||||||
throw new Error(`pubToEpub() -> ${err.message}`)
|
throw err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue