From 9c0a16ebcd246b77e587d48e3b91321f1acba186 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 3 Jan 2024 22:20:30 -0600 Subject: [PATCH] client: use EventSet, normalizeFilters --- src/client.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/client.ts b/src/client.ts index 993242f..34d4544 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,18 +1,23 @@ import { Debug, type Event, type Filter, matchFilters } from '@/deps.ts'; +import { normalizeFilters } from '@/filter.ts'; import * as pipeline from '@/pipeline.ts'; import { activeRelays, pool } from '@/pool.ts'; import { type EventStore, type GetEventsOpts, type StoreEventOpts } from '@/storages/types.ts'; +import { EventSet } from '@/utils/event-set.ts'; const debug = Debug('ditto:client'); /** Get events from a NIP-01 filter. */ function getEvents(filters: Filter[], opts: GetEventsOpts = {}): Promise[]> { + filters = normalizeFilters(filters); + if (opts.signal?.aborted) return Promise.resolve([]); if (!filters.length) return Promise.resolve([]); + debug('REQ', JSON.stringify(filters)); return new Promise((resolve) => { - const results: Event[] = []; + const results = new EventSet>(); const unsub = pool.subscribe( filters, @@ -20,9 +25,9 @@ function getEvents(filters: Filter[], opts: GetEventsOpts = (event: Event | null) => { if (event && matchFilters(filters, event)) { pipeline.handleEvent(event).catch(() => {}); - results.push({ + results.add({ id: event.id, - kind: event.kind, + kind: event.kind as K, pubkey: event.pubkey, content: event.content, tags: event.tags, @@ -30,21 +35,21 @@ function getEvents(filters: Filter[], opts: GetEventsOpts = sig: event.sig, }); } - if (typeof opts.limit === 'number' && results.length >= opts.limit) { + if (typeof opts.limit === 'number' && results.size >= opts.limit) { unsub(); - resolve(results as Event[]); + resolve([...results]); } }, undefined, () => { unsub(); - resolve(results as Event[]); + resolve([...results]); }, ); opts.signal?.addEventListener('abort', () => { unsub(); - resolve(results as Event[]); + resolve([...results]); }); }); }