Skip to content

Commit

Permalink
Fix stale cursor used on reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Jan 20, 2024
1 parent dc8d35f commit 69e6222
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions worker/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
sub.on('error', reject)
})
} catch (error) {
console.error(error)
Expand All @@ -45,13 +50,12 @@ const logEventError = (name, error) => console.error(`error running ${name}`, er
async function subscribeToDeposits (args) {
const { models, lnd } = args

const [lastConfirmed] = await models.$queryRaw`
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) => {
Expand Down

0 comments on commit 69e6222

Please sign in to comment.