Fix load bypassing freshness

This commit is contained in:
Jon Staab
2024-09-02 15:57:40 -07:00
parent 9a5a423b3e
commit b910569fc0
8 changed files with 80 additions and 61 deletions
+15 -2
View File
@@ -1,4 +1,3 @@
import {first} from "@welshman/lib"
import {Repository, Relay} from "@welshman/util"
import type {TrustedEvent} from "@welshman/util"
import {Tracker, subscribe as baseSubscribe} from "@welshman/net"
@@ -6,9 +5,11 @@ import type {SubscribeRequest} from "@welshman/net"
import {createEventStore} from "@welshman/store"
export const env: {
BOOTSTRAP_RELAYS: string[]
DUFFLEPUD_URL?: string
[key: string]: any
} = {
BOOTSTRAP_RELAYS: [],
DUFFLEPUD_URL: undefined,
}
@@ -39,4 +40,16 @@ export const load = (request: SubscribeRequest) =>
sub.emitter.on("complete", () => resolve(events))
})
export const loadOne = async (request: SubscribeRequest) => first(await load(request))
export const loadOne = (request: SubscribeRequest) =>
new Promise<TrustedEvent | null>(resolve => {
const sub = subscribe({closeOnEose: true, timeout: 3000, ...request})
sub.emitter.on("event", (url: string, event: TrustedEvent) => {
resolve(event)
sub.close()
})
sub.emitter.on("complete", () => {
resolve(null)
})
})