Add since to reqs that get re-opened to avoid re-downloading and gaps in active subscriptions. Also add some getters for event stores and event sort utils

This commit is contained in:
Jon Staab
2025-12-04 15:16:56 -08:00
parent 7b96e52349
commit d81ec62d2b
5 changed files with 126 additions and 75 deletions
+22 -4
View File
@@ -1,5 +1,5 @@
import {on, ms, nthNe, always, call, sleep, ago, now} from "@welshman/lib"
import {RELAY_JOIN, StampedEvent, SignedEvent} from "@welshman/util"
import {on, ms, omit, nthNe, always, call, sleep, ago, now} from "@welshman/lib"
import {RELAY_JOIN, StampedEvent, SignedEvent, Filter} from "@welshman/util"
import {
ClientMessage,
isClientAuth,
@@ -142,11 +142,29 @@ export const socketPolicyCloseInactive = (socket: Socket) => {
// If the socket closed and we have no error, reopen it but don't flap
if (isClosed && pending.size) {
sleep(Math.max(0, ms(5 - (now() - lastOpen)))).then(() => {
const since = now()
const delay = Math.max(0, ms(5 - (now() - lastOpen)))
sleep(delay).then(() => {
socket.attemptToOpen()
for (const message of pending.values()) {
socket.send(message)
// Add since to avoid re-downloading stuff on reconnect. If limit=0, remove it to catch up
if (isClientReq(message) && delay > 0) {
const filters: Filter[] = []
for (let filter of message.slice(2) as Filter[]) {
if (filter.limit === 0) {
filter = omit(["limit"], filter)
}
filters.push({...filter, since})
}
socket.send([...message.slice(0, 2), ...filters])
} else {
socket.send(message)
}
}
})
}