mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 03:19:46 +00:00
Expose a generic listen method on the database adapters
This commit is contained in:
parent
4df61c0c59
commit
013917d612
4 changed files with 10 additions and 16 deletions
|
|
@ -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<DittoTables>;
|
||||
readonly poolSize: number;
|
||||
readonly availableConnections: number;
|
||||
readonly listenNostr: (onEvent: (event: NostrEvent) => void) => void;
|
||||
listen(channel: string, callback: (payload: string) => void): void;
|
||||
}
|
||||
|
||||
export interface DittoDatabaseOpts {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,11 +6,12 @@ import { Storages } from '@/storages.ts';
|
|||
const sem = new Semaphore(1);
|
||||
|
||||
export async function startNotify(): Promise<void> {
|
||||
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);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue