Reqmeister: pass client as an opt

This commit is contained in:
Alex Gleason 2024-01-07 14:58:17 -06:00
parent ae44c08a2a
commit 20928cdf82
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 8 additions and 6 deletions

View File

@ -1,3 +1,4 @@
import { client } from '@/client.ts';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { db } from '@/db.ts'; import { db } from '@/db.ts';
import { EventsDB } from '@/storages/events-db.ts'; import { EventsDB } from '@/storages/events-db.ts';
@ -15,6 +16,7 @@ const memorelay = new Memorelay({ max: 3000 });
/** Batches requests for single events. */ /** Batches requests for single events. */
const reqmeister = new Reqmeister({ const reqmeister = new Reqmeister({
client,
delay: Time.seconds(1), delay: Time.seconds(1),
timeout: Time.seconds(1), timeout: Time.seconds(1),
}); });

View File

@ -1,4 +1,3 @@
import { client } from '@/client.ts';
import { Debug, type Event, EventEmitter, type Filter } from '@/deps.ts'; import { Debug, type Event, EventEmitter, type Filter } from '@/deps.ts';
import { import {
AuthorMicrofilter, AuthorMicrofilter,
@ -11,9 +10,8 @@ import {
import { type EventStore, GetEventsOpts } from '@/storages/types.ts'; import { type EventStore, GetEventsOpts } from '@/storages/types.ts';
import { Time } from '@/utils/time.ts'; import { Time } from '@/utils/time.ts';
const debug = Debug('ditto:reqmeister');
interface ReqmeisterOpts { interface ReqmeisterOpts {
client: EventStore;
delay?: number; delay?: number;
timeout?: number; timeout?: number;
} }
@ -27,6 +25,8 @@ type ReqmeisterQueueItem = [string, MicroFilter, WebSocket['url'][]];
/** Batches requests to Nostr relays using microfilters. */ /** Batches requests to Nostr relays using microfilters. */
class Reqmeister extends EventEmitter<{ [filterId: string]: (event: Event) => any }> implements EventStore { class Reqmeister extends EventEmitter<{ [filterId: string]: (event: Event) => any }> implements EventStore {
#debug = Debug('ditto:reqmeister');
#opts: ReqmeisterOpts; #opts: ReqmeisterOpts;
#queue: ReqmeisterQueueItem[] = []; #queue: ReqmeisterQueueItem[] = [];
#promise!: Promise<void>; #promise!: Promise<void>;
@ -34,7 +34,7 @@ class Reqmeister extends EventEmitter<{ [filterId: string]: (event: Event) => an
supportedNips = []; supportedNips = [];
constructor(opts: ReqmeisterOpts = {}) { constructor(opts: ReqmeisterOpts) {
super(); super();
this.#opts = opts; this.#opts = opts;
this.#tick(); this.#tick();
@ -49,7 +49,7 @@ class Reqmeister extends EventEmitter<{ [filterId: string]: (event: Event) => an
} }
async #perform() { async #perform() {
const { delay, timeout = Time.seconds(1) } = this.#opts; const { client, 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;
@ -73,7 +73,7 @@ class Reqmeister extends EventEmitter<{ [filterId: string]: (event: Event) => an
if (wantedAuthors.size) filters.push({ kinds: [0], authors: [...wantedAuthors] }); if (wantedAuthors.size) filters.push({ kinds: [0], authors: [...wantedAuthors] });
if (filters.length) { if (filters.length) {
debug('REQ', JSON.stringify(filters)); this.#debug('REQ', JSON.stringify(filters));
const events = await client.getEvents(filters, { signal: AbortSignal.timeout(timeout) }); const events = await client.getEvents(filters, { signal: AbortSignal.timeout(timeout) });
for (const event of events) { for (const event of events) {