simpler orderResponse detection
This commit is contained in:
parent
a44d1965c0
commit
32b548a676
1 changed files with 12 additions and 37 deletions
|
|
@ -983,51 +983,26 @@ const sendSpontaneousPayment = async (to, amount, memo, feeLimit) => {
|
||||||
// eslint-disable-next-line init-declarations
|
// eslint-disable-next-line init-declarations
|
||||||
let timeoutID
|
let timeoutID
|
||||||
|
|
||||||
const onMethod = new Promise(res => {
|
/**
|
||||||
|
* @type {import('shock-common').Schema.OrderResponse}
|
||||||
|
*/
|
||||||
|
const encryptedOrderRes = await new Promise((res, rej) => {
|
||||||
require('../Mediator')
|
require('../Mediator')
|
||||||
.getGun()
|
.getGun()
|
||||||
.user(to)
|
.user(to)
|
||||||
.get(Key.ORDER_TO_RESPONSE)
|
.get(Key.ORDER_TO_RESPONSE)
|
||||||
.get(orderID)
|
.get(orderID)
|
||||||
.on(inv => {
|
.on(orderResponse => {
|
||||||
if (typeof inv === 'string') {
|
if (Schema.isOrderResponse(orderResponse)) {
|
||||||
res(inv)
|
clearTimeout(timeoutID)
|
||||||
|
res(orderResponse)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
||||||
const freshGunMethod = Utils.tryAndWait(
|
timeoutID = setTimeout(() => {
|
||||||
gun =>
|
|
||||||
gun
|
|
||||||
.user(to)
|
|
||||||
.get(Key.ORDER_TO_RESPONSE)
|
|
||||||
.get(orderID)
|
|
||||||
.then(),
|
|
||||||
v => typeof v === 'undefined'
|
|
||||||
)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @type {import('shock-common').Schema.OrderResponse}
|
|
||||||
*/
|
|
||||||
const encryptedOrderRes = await Promise.race([
|
|
||||||
Promise.race([onMethod, freshGunMethod]).then(v => {
|
|
||||||
clearTimeout(timeoutID)
|
|
||||||
return v
|
|
||||||
}),
|
|
||||||
|
|
||||||
new Promise((_, rej) => {
|
|
||||||
setTimeout(() => {
|
|
||||||
rej(new Error(ErrorCode.ORDER_NOT_ANSWERED_IN_TIME))
|
rej(new Error(ErrorCode.ORDER_NOT_ANSWERED_IN_TIME))
|
||||||
}, 20000)
|
}, 20000)
|
||||||
})
|
})
|
||||||
])
|
|
||||||
|
|
||||||
if (!Schema.isOrderResponse(encryptedOrderRes)) {
|
|
||||||
throw new Error(
|
|
||||||
'received response not an OrderResponse, instead got: ' +
|
|
||||||
JSON.stringify(encryptedOrderRes)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @type {import('shock-common').Schema.OrderResponse} */
|
/** @type {import('shock-common').Schema.OrderResponse} */
|
||||||
const orderResponse = {
|
const orderResponse = {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue