Debug client, pass timeout into reqmeister

This commit is contained in:
Alex Gleason 2023-12-27 21:14:45 -06:00
parent baf51230ba
commit 4d05398be1
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 10 additions and 7 deletions

View File

@ -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 * as pipeline from '@/pipeline.ts';
import { activeRelays, pool } from '@/pool.ts'; import { activeRelays, pool } from '@/pool.ts';
import type { GetFiltersOpts } from '@/filter.ts'; import type { GetFiltersOpts } from '@/filter.ts';
const debug = Debug('ditto:client');
/** Get events from a NIP-01 filter. */ /** Get events from a NIP-01 filter. */
function getFilters<K extends number>(filters: Filter<K>[], opts: GetFiltersOpts = {}): Promise<Event<K>[]> { function getFilters<K extends number>(filters: Filter<K>[], opts: GetFiltersOpts = {}): Promise<Event<K>[]> {
if (opts.signal?.aborted) return Promise.resolve([]); if (opts.signal?.aborted) return Promise.resolve([]);
if (!filters.length) return Promise.resolve([]); if (!filters.length) return Promise.resolve([]);
debug(JSON.stringify(filters));
return new Promise((resolve) => { return new Promise((resolve) => {
const results: Event[] = []; const results: Event[] = [];

View File

@ -52,7 +52,7 @@ function matchDittoFilters(filters: DittoFilter[], event: Event, data: EventData
/** Get deterministic ID for a microfilter. */ /** Get deterministic ID for a microfilter. */
function getFilterId(filter: MicroFilter): string { function getFilterId(filter: MicroFilter): string {
if ('ids' in filter) { if ('ids' in filter) {
return stringifyStable({ ids: [filter.ids] }); return stringifyStable({ ids: [filter.ids[0]] });
} else { } else {
return stringifyStable({ return stringifyStable({
kinds: [filter.kinds[0]], kinds: [filter.kinds[0]],

View File

@ -7,7 +7,7 @@ const debug = Debug('ditto:reqmeister');
interface ReqmeisterOpts { interface ReqmeisterOpts {
delay?: number; delay?: number;
signal?: AbortSignal; timeout?: number;
} }
type ReqmeisterQueueItem = [string, MicroFilter, WebSocket['url'][]]; type ReqmeisterQueueItem = [string, MicroFilter, WebSocket['url'][]];
@ -34,7 +34,7 @@ class Reqmeister extends EventEmitter<{ [filterId: string]: (event: Event) => an
} }
async #perform() { async #perform() {
const { delay } = this.#opts; const { delay, timeout = Time.seconds(1) } = this.#opts;
await new Promise((resolve) => setTimeout(resolve, delay)); await new Promise((resolve) => setTimeout(resolve, delay));
const queue = this.#queue; const queue = this.#queue;
@ -59,7 +59,7 @@ class Reqmeister extends EventEmitter<{ [filterId: string]: (event: Event) => an
if (filters.length) { if (filters.length) {
debug(JSON.stringify(filters)); 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) { for (const event of events) {
this.encounter(event); this.encounter(event);
@ -75,7 +75,7 @@ class Reqmeister extends EventEmitter<{ [filterId: string]: (event: Event) => an
this.#queue.push([filterId, filter, relays]); this.#queue.push([filterId, filter, relays]);
return new Promise<Event>((resolve, reject) => { return new Promise<Event>((resolve, reject) => {
this.once(filterId, resolve); 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({ const reqmeister = new Reqmeister({
delay: Time.seconds(1), delay: Time.seconds(1),
signal: AbortSignal.timeout(Time.seconds(1)), timeout: Time.seconds(1),
}); });
export { reqmeister }; export { reqmeister };