client: use EventSet, normalizeFilters

This commit is contained in:
Alex Gleason 2024-01-03 22:20:30 -06:00
parent b71124cc30
commit 9c0a16ebcd
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 12 additions and 7 deletions

View File

@ -1,18 +1,23 @@
import { Debug, type Event, type Filter, matchFilters } from '@/deps.ts'; import { Debug, type Event, type Filter, matchFilters } from '@/deps.ts';
import { normalizeFilters } from '@/filter.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 EventStore, type GetEventsOpts, type StoreEventOpts } from '@/storages/types.ts'; import { type EventStore, type GetEventsOpts, type StoreEventOpts } from '@/storages/types.ts';
import { EventSet } from '@/utils/event-set.ts';
const debug = Debug('ditto:client'); const debug = Debug('ditto:client');
/** Get events from a NIP-01 filter. */ /** Get events from a NIP-01 filter. */
function getEvents<K extends number>(filters: Filter<K>[], opts: GetEventsOpts = {}): Promise<Event<K>[]> { function getEvents<K extends number>(filters: Filter<K>[], opts: GetEventsOpts = {}): Promise<Event<K>[]> {
filters = normalizeFilters(filters);
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('REQ', JSON.stringify(filters)); debug('REQ', JSON.stringify(filters));
return new Promise((resolve) => { return new Promise((resolve) => {
const results: Event[] = []; const results = new EventSet<Event<K>>();
const unsub = pool.subscribe( const unsub = pool.subscribe(
filters, filters,
@ -20,9 +25,9 @@ function getEvents<K extends number>(filters: Filter<K>[], opts: GetEventsOpts =
(event: Event | null) => { (event: Event | null) => {
if (event && matchFilters(filters, event)) { if (event && matchFilters(filters, event)) {
pipeline.handleEvent(event).catch(() => {}); pipeline.handleEvent(event).catch(() => {});
results.push({ results.add({
id: event.id, id: event.id,
kind: event.kind, kind: event.kind as K,
pubkey: event.pubkey, pubkey: event.pubkey,
content: event.content, content: event.content,
tags: event.tags, tags: event.tags,
@ -30,21 +35,21 @@ function getEvents<K extends number>(filters: Filter<K>[], opts: GetEventsOpts =
sig: event.sig, sig: event.sig,
}); });
} }
if (typeof opts.limit === 'number' && results.length >= opts.limit) { if (typeof opts.limit === 'number' && results.size >= opts.limit) {
unsub(); unsub();
resolve(results as Event<K>[]); resolve([...results]);
} }
}, },
undefined, undefined,
() => { () => {
unsub(); unsub();
resolve(results as Event<K>[]); resolve([...results]);
}, },
); );
opts.signal?.addEventListener('abort', () => { opts.signal?.addEventListener('abort', () => {
unsub(); unsub();
resolve(results as Event<K>[]); resolve([...results]);
}); });
}); });
} }