diff --git a/src/client.ts b/src/client.ts index 3cf2e8a..573aecc 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,13 +1,16 @@ -import { type Event, type Filter, matchFilters } from '@/deps.ts'; +import { Debug, type Event, type Filter, matchFilters } from '@/deps.ts'; import * as pipeline from '@/pipeline.ts'; import { activeRelays, pool } from '@/pool.ts'; import type { GetFiltersOpts } from '@/filter.ts'; +const debug = Debug('ditto:client'); + /** Get events from a NIP-01 filter. */ function getFilters(filters: Filter[], opts: GetFiltersOpts = {}): Promise[]> { if (opts.signal?.aborted) return Promise.resolve([]); if (!filters.length) return Promise.resolve([]); + debug(JSON.stringify(filters)); return new Promise((resolve) => { const results: Event[] = []; diff --git a/src/filter.ts b/src/filter.ts index 38fcff7..7178e2d 100644 --- a/src/filter.ts +++ b/src/filter.ts @@ -52,7 +52,7 @@ function matchDittoFilters(filters: DittoFilter[], event: Event, data: EventData /** Get deterministic ID for a microfilter. */ function getFilterId(filter: MicroFilter): string { if ('ids' in filter) { - return stringifyStable({ ids: [filter.ids] }); + return stringifyStable({ ids: [filter.ids[0]] }); } else { return stringifyStable({ kinds: [filter.kinds[0]], diff --git a/src/reqmeister.ts b/src/reqmeister.ts index 811c6cc..52802dd 100644 --- a/src/reqmeister.ts +++ b/src/reqmeister.ts @@ -7,7 +7,7 @@ const debug = Debug('ditto:reqmeister'); interface ReqmeisterOpts { delay?: number; - signal?: AbortSignal; + timeout?: number; } type ReqmeisterQueueItem = [string, MicroFilter, WebSocket['url'][]]; @@ -34,7 +34,7 @@ class Reqmeister extends EventEmitter<{ [filterId: string]: (event: Event) => an } async #perform() { - const { delay } = this.#opts; + const { delay, timeout = Time.seconds(1) } = this.#opts; await new Promise((resolve) => setTimeout(resolve, delay)); const queue = this.#queue; @@ -59,7 +59,7 @@ class Reqmeister extends EventEmitter<{ [filterId: string]: (event: Event) => an if (filters.length) { debug(JSON.stringify(filters)); - const events = await client.getFilters(filters, { signal: this.#opts.signal }); + const events = await client.getFilters(filters, { signal: AbortSignal.timeout(timeout) }); for (const event of events) { this.encounter(event); @@ -75,7 +75,7 @@ class Reqmeister extends EventEmitter<{ [filterId: string]: (event: Event) => an this.#queue.push([filterId, filter, relays]); return new Promise((resolve, reject) => { this.once(filterId, resolve); - this.#promise.finally(reject); + this.#promise.finally(() => setTimeout(reject, 0)); }); } @@ -93,7 +93,7 @@ class Reqmeister extends EventEmitter<{ [filterId: string]: (event: Event) => an const reqmeister = new Reqmeister({ delay: Time.seconds(1), - signal: AbortSignal.timeout(Time.seconds(1)), + timeout: Time.seconds(1), }); export { reqmeister };