diff --git a/src/db/DittoDatabase.ts b/src/db/DittoDatabase.ts index 7df322cc..3979cd12 100644 --- a/src/db/DittoDatabase.ts +++ b/src/db/DittoDatabase.ts @@ -1,4 +1,3 @@ -import { NostrEvent } from '@nostrify/nostrify'; import { Kysely } from 'kysely'; import { DittoTables } from '@/db/DittoTables.ts'; @@ -7,7 +6,7 @@ export interface DittoDatabase { readonly kysely: Kysely; readonly poolSize: number; readonly availableConnections: number; - readonly listenNostr: (onEvent: (event: NostrEvent) => void) => void; + listen(channel: string, callback: (payload: string) => void): void; } export interface DittoDatabaseOpts { diff --git a/src/db/adapters/DittoPglite.ts b/src/db/adapters/DittoPglite.ts index 3bbafe3c..df616458 100644 --- a/src/db/adapters/DittoPglite.ts +++ b/src/db/adapters/DittoPglite.ts @@ -1,6 +1,5 @@ import { PGlite } from '@electric-sql/pglite'; import { pg_trgm } from '@electric-sql/pglite/contrib/pg_trgm'; -import { NostrEvent } from '@nostrify/nostrify'; import { PgliteDialect } from '@soapbox/kysely-pglite'; import { Kysely } from 'kysely'; @@ -27,17 +26,15 @@ export class DittoPglite { log: KyselyLogger, }); - const listenNostr = (onEvent: (event: NostrEvent) => void): void => { - pglite.listen('nostr_event', (payload) => { - onEvent(JSON.parse(payload)); - }); + const listen = (channel: string, callback: (payload: string) => void): void => { + pglite.listen(channel, callback); }; return { kysely, poolSize: 1, availableConnections: 1, - listenNostr, + listen, }; } } diff --git a/src/db/adapters/DittoPostgres.ts b/src/db/adapters/DittoPostgres.ts index 36ea8bc8..180e4a7a 100644 --- a/src/db/adapters/DittoPostgres.ts +++ b/src/db/adapters/DittoPostgres.ts @@ -1,4 +1,3 @@ -import { NostrEvent } from '@nostrify/nostrify'; import { BinaryOperationNode, FunctionNode, @@ -41,10 +40,8 @@ export class DittoPostgres { log: KyselyLogger, }); - const listenNostr = (onEvent: (event: NostrEvent) => void): void => { - pg.listen('nostr_event', (payload) => { - onEvent(JSON.parse(payload)); - }); + const listen = (channel: string, callback: (payload: string) => void): void => { + pg.listen(channel, callback); }; return { @@ -55,7 +52,7 @@ export class DittoPostgres { get availableConnections() { return pg.connections.idle; }, - listenNostr, + listen, }; } } diff --git a/src/notify.ts b/src/notify.ts index a39d574b..4033d6c6 100644 --- a/src/notify.ts +++ b/src/notify.ts @@ -6,11 +6,12 @@ import { Storages } from '@/storages.ts'; const sem = new Semaphore(1); export async function startNotify(): Promise { - const { listenNostr } = await Storages.database(); + const { listen } = await Storages.database(); - listenNostr((event) => { + listen('nostr_event', (payload) => { sem.lock(async () => { try { + const event = JSON.parse(payload); await pipeline.handleEvent(event, AbortSignal.timeout(5000)); } catch (e) { console.warn(e);