Fix stale cursor used on reconnect (#764)

This commit is contained in:
ekzyis 2024-01-20 23:57:52 +01:00 committed by GitHub
parent dc8d35fdcf
commit 30850acd8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -22,11 +22,16 @@ function subscribeForever (subscribe) {
let sub
try {
return await new Promise((resolve, reject) => {
sub = subscribe(resolve, bail)
const sub = subscribe(resolve, bail)
if (!sub) {
return bail(new Error('function passed to subscribeForever must return a subscription object'))
return bail(new Error('function passed to subscribeForever must return a subscription object or promise'))
}
if (sub.then) {
// sub is promise
sub.then(sub => sub.on('error', reject))
} else {
sub.on('error', reject)
}
})
} catch (error) {
console.error(error)
@ -45,13 +50,12 @@ const logEventError = (name, error) => console.error(`error running ${name}`, er
async function subscribeToDeposits (args) {
const { models, lnd } = args
subscribeForever(async () => {
const [lastConfirmed] = await models.$queryRaw`
SELECT "confirmedIndex"
FROM "Invoice"
ORDER BY "confirmedIndex" DESC NULLS LAST
LIMIT 1`
subscribeForever(() => {
const sub = subscribeToInvoices({ lnd, confirmed_after: lastConfirmed?.confirmedIndex })
sub.on('invoice_updated', async (inv) => {