mixer: pass opts through to all getFilters functions
This commit is contained in:
parent
d4721fb82d
commit
bbaf1cb012
|
@ -1,9 +1,9 @@
|
||||||
|
import { Conf } from '@/config.ts';
|
||||||
import { Author, type Filter, findReplyTag, matchFilters, RelayPool, TTLCache } from '@/deps.ts';
|
import { Author, type Filter, findReplyTag, matchFilters, RelayPool, TTLCache } from '@/deps.ts';
|
||||||
import { type Event, type SignedEvent } from '@/event.ts';
|
import { type Event, type SignedEvent } from '@/event.ts';
|
||||||
|
import { eventDateComparator, type PaginationParams, Time } from '@/utils.ts';
|
||||||
|
|
||||||
import { Conf } from './config.ts';
|
import type { GetFiltersOpts } from '@/types.ts';
|
||||||
|
|
||||||
import { eventDateComparator, type PaginationParams, Time } from './utils.ts';
|
|
||||||
|
|
||||||
const db = await Deno.openKv();
|
const db = await Deno.openKv();
|
||||||
|
|
||||||
|
@ -29,12 +29,8 @@ function getPool(): Pool {
|
||||||
return pool;
|
return pool;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface GetFilterOpts {
|
|
||||||
timeout?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Get events from a NIP-01 filter. */
|
/** Get events from a NIP-01 filter. */
|
||||||
function getFilters<K extends number>(filters: Filter<K>[], opts: GetFilterOpts = {}): Promise<SignedEvent<K>[]> {
|
function getFilters<K extends number>(filters: Filter<K>[], opts: GetFiltersOpts = {}): Promise<SignedEvent<K>[]> {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
let tid: number;
|
let tid: number;
|
||||||
const results: SignedEvent[] = [];
|
const results: SignedEvent[] = [];
|
||||||
|
@ -79,7 +75,7 @@ function getFilters<K extends number>(filters: Filter<K>[], opts: GetFilterOpts
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @deprecated Use `getFilters` instead. */
|
/** @deprecated Use `getFilters` instead. */
|
||||||
function getFilter<K extends number>(filter: Filter<K>, opts: GetFilterOpts = {}): Promise<SignedEvent<K>[]> {
|
function getFilter<K extends number>(filter: Filter<K>, opts: GetFiltersOpts = {}): Promise<SignedEvent<K>[]> {
|
||||||
return getFilters([filter], opts);
|
return getFilters([filter], opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { db, type TagRow } from '@/db.ts';
|
||||||
import { type Insertable } from '@/deps.ts';
|
import { type Insertable } from '@/deps.ts';
|
||||||
import { type SignedEvent } from '@/event.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;
|
type TagCondition = ({ event, count }: { event: SignedEvent; count: number }) => boolean;
|
||||||
|
|
||||||
|
@ -105,7 +105,10 @@ function getFilterQuery(filter: DittoFilter) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get events for filters from the database. */
|
/** Get events for filters from the database. */
|
||||||
async function getFilters<K extends number>(filters: DittoFilter<K>[]): Promise<SignedEvent<K>[]> {
|
async function getFilters<K extends number>(
|
||||||
|
filters: DittoFilter<K>[],
|
||||||
|
_opts?: GetFiltersOpts,
|
||||||
|
): Promise<SignedEvent<K>[]> {
|
||||||
const events = await filters
|
const events = await filters
|
||||||
.map(getFilterQuery)
|
.map(getFilterQuery)
|
||||||
.reduce((acc, curr) => acc.union(curr))
|
.reduce((acc, curr) => acc.union(curr))
|
||||||
|
@ -116,7 +119,7 @@ async function getFilters<K extends number>(filters: DittoFilter<K>[]): Promise<
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get events for a filter from the database. */
|
/** @deprecated Use `getFilters` instead. */
|
||||||
function getFilter<K extends number = number>(filter: DittoFilter<K>): Promise<SignedEvent<K>[]> {
|
function getFilter<K extends number = number>(filter: DittoFilter<K>): Promise<SignedEvent<K>[]> {
|
||||||
return getFilters<K>([filter]);
|
return getFilters<K>([filter]);
|
||||||
}
|
}
|
||||||
|
|
11
src/mixer.ts
11
src/mixer.ts
|
@ -5,13 +5,16 @@ import { getFilters as getFiltersDB } from '@/db/events.ts';
|
||||||
import { eventDateComparator } from '@/utils.ts';
|
import { eventDateComparator } from '@/utils.ts';
|
||||||
|
|
||||||
import type { SignedEvent } from '@/event.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. */
|
/** Get filters from the database and pool, and mix the best results together. */
|
||||||
async function getFilters<K extends number>(filters: DittoFilter<K>[]): Promise<SignedEvent<K>[]> {
|
async function getFilters<K extends number>(
|
||||||
|
filters: DittoFilter<K>[],
|
||||||
|
opts?: GetFiltersOpts,
|
||||||
|
): Promise<SignedEvent<K>[]> {
|
||||||
const results = await Promise.allSettled([
|
const results = await Promise.allSettled([
|
||||||
getFiltersClient(filters),
|
getFiltersClient(filters, opts),
|
||||||
getFiltersDB(filters),
|
getFiltersDB(filters, opts),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const events = results
|
const events = results
|
||||||
|
|
|
@ -5,4 +5,9 @@ interface DittoFilter<K extends number = number> extends Filter<K> {
|
||||||
local?: boolean;
|
local?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export { type DittoFilter };
|
/** Additional options to apply to the whole subscription. */
|
||||||
|
interface GetFiltersOpts {
|
||||||
|
timeout?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type { DittoFilter, GetFiltersOpts };
|
||||||
|
|
Loading…
Reference in New Issue