48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import type { Event, Filter } from 'nostr-tools'
|
|
import { SimplePool } from 'nostr-tools'
|
|
import { getPrimaryRelaySync } from './config'
|
|
import { createSubscription } from '@/types/nostr-tools-extended'
|
|
|
|
/**
|
|
* Subscribe to events with timeout
|
|
* Supports both sync and async parsers
|
|
*/
|
|
export function subscribeWithTimeout<T>(
|
|
pool: SimplePool,
|
|
filters: Filter[],
|
|
parser: (event: Event) => T | null | Promise<T | null>,
|
|
timeout: number = 5000
|
|
): Promise<T | null> {
|
|
return new Promise((resolve) => {
|
|
const resolved = { value: false }
|
|
const relayUrl = getPrimaryRelaySync()
|
|
const sub = createSubscription(pool, [relayUrl], filters)
|
|
let timeoutId: ReturnType<typeof setTimeout> | null = null
|
|
|
|
const cleanup = (): void => {
|
|
if (timeoutId) {
|
|
clearTimeout(timeoutId)
|
|
}
|
|
sub.unsub()
|
|
}
|
|
|
|
const resolveOnce = (value: T | null): void => {
|
|
if (resolved.value) {
|
|
return
|
|
}
|
|
resolved.value = true
|
|
cleanup()
|
|
resolve(value)
|
|
}
|
|
|
|
sub.on('event', async (event: Event): Promise<void> => {
|
|
const result = await parser(event)
|
|
resolveOnce(result)
|
|
})
|
|
sub.on('eose', (): void => {
|
|
resolveOnce(null)
|
|
})
|
|
timeoutId = setTimeout(() => resolveOnce(null), timeout)
|
|
})
|
|
}
|