From bbaf1cb012659a8ceab7511e16ee416a4cc913e7 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 16 Aug 2023 21:21:29 -0500 Subject: [PATCH] mixer: pass opts through to all getFilters functions --- src/client.ts | 14 +++++--------- src/db/events.ts | 9 ++++++--- src/mixer.ts | 11 +++++++---- src/types.ts | 7 ++++++- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/client.ts b/src/client.ts index 71a644f..ce3b9e1 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,9 +1,9 @@ +import { Conf } from '@/config.ts'; import { Author, type Filter, findReplyTag, matchFilters, RelayPool, TTLCache } from '@/deps.ts'; import { type Event, type SignedEvent } from '@/event.ts'; +import { eventDateComparator, type PaginationParams, Time } from '@/utils.ts'; -import { Conf } from './config.ts'; - -import { eventDateComparator, type PaginationParams, Time } from './utils.ts'; +import type { GetFiltersOpts } from '@/types.ts'; const db = await Deno.openKv(); @@ -29,12 +29,8 @@ function getPool(): Pool { return pool; } -interface GetFilterOpts { - timeout?: number; -} - /** Get events from a NIP-01 filter. */ -function getFilters(filters: Filter[], opts: GetFilterOpts = {}): Promise[]> { +function getFilters(filters: Filter[], opts: GetFiltersOpts = {}): Promise[]> { return new Promise((resolve) => { let tid: number; const results: SignedEvent[] = []; @@ -79,7 +75,7 @@ function getFilters(filters: Filter[], opts: GetFilterOpts } /** @deprecated Use `getFilters` instead. */ -function getFilter(filter: Filter, opts: GetFilterOpts = {}): Promise[]> { +function getFilter(filter: Filter, opts: GetFiltersOpts = {}): Promise[]> { return getFilters([filter], opts); } diff --git a/src/db/events.ts b/src/db/events.ts index 15e24f4..7bd94ce 100644 --- a/src/db/events.ts +++ b/src/db/events.ts @@ -2,7 +2,7 @@ import { db, type TagRow } from '@/db.ts'; import { type Insertable } from '@/deps.ts'; import { type SignedEvent } from '@/event.ts'; -import type { DittoFilter } from '@/types.ts'; +import type { DittoFilter, GetFiltersOpts } from '@/types.ts'; type TagCondition = ({ event, count }: { event: SignedEvent; count: number }) => boolean; @@ -105,7 +105,10 @@ function getFilterQuery(filter: DittoFilter) { } /** Get events for filters from the database. */ -async function getFilters(filters: DittoFilter[]): Promise[]> { +async function getFilters( + filters: DittoFilter[], + _opts?: GetFiltersOpts, +): Promise[]> { const events = await filters .map(getFilterQuery) .reduce((acc, curr) => acc.union(curr)) @@ -116,7 +119,7 @@ async function getFilters(filters: DittoFilter[]): Promise< )); } -/** Get events for a filter from the database. */ +/** @deprecated Use `getFilters` instead. */ function getFilter(filter: DittoFilter): Promise[]> { return getFilters([filter]); } diff --git a/src/mixer.ts b/src/mixer.ts index ee123cc..426c612 100644 --- a/src/mixer.ts +++ b/src/mixer.ts @@ -5,13 +5,16 @@ import { getFilters as getFiltersDB } from '@/db/events.ts'; import { eventDateComparator } from '@/utils.ts'; import type { SignedEvent } from '@/event.ts'; -import type { DittoFilter } from '@/types.ts'; +import type { DittoFilter, GetFiltersOpts } from '@/types.ts'; /** Get filters from the database and pool, and mix the best results together. */ -async function getFilters(filters: DittoFilter[]): Promise[]> { +async function getFilters( + filters: DittoFilter[], + opts?: GetFiltersOpts, +): Promise[]> { const results = await Promise.allSettled([ - getFiltersClient(filters), - getFiltersDB(filters), + getFiltersClient(filters, opts), + getFiltersDB(filters, opts), ]); const events = results diff --git a/src/types.ts b/src/types.ts index 6c7feba..e333818 100644 --- a/src/types.ts +++ b/src/types.ts @@ -5,4 +5,9 @@ interface DittoFilter extends Filter { local?: boolean; } -export { type DittoFilter }; +/** Additional options to apply to the whole subscription. */ +interface GetFiltersOpts { + timeout?: number; +} + +export type { DittoFilter, GetFiltersOpts };