blowater/UI/event_syncer.ts

30 lines
1004 B
TypeScript
Raw Normal View History

2023-11-03 13:09:13 +00:00
import { ConnectionPool } from "../lib/nostr-ts/relay-pool.ts";
import { Datebase_View } from "../database.ts";
2023-08-28 17:58:05 +00:00
import { NoteID } from "../lib/nostr-ts/nip19.ts";
export class EventSyncer {
constructor(private readonly pool: ConnectionPool, private readonly db: Datebase_View) {}
2023-07-30 06:39:53 +00:00
syncEvent(id: NoteID) {
const subID = EventSyncer.name + ":syncEvent";
const e = this.db.get({ id: id.hex });
if (e) {
return e;
}
return (async () => {
await this.pool.closeSub(subID);
let events = await this.pool.newSub(subID, { ids: [id.hex] });
if (events instanceof Error) {
return events;
}
2023-08-28 17:58:05 +00:00
for await (const { res, url } of events.chan) {
2023-09-04 21:32:47 +00:00
if (res.type != "EVENT") {
continue;
}
2023-11-16 09:18:30 +00:00
await this.db.addEvent(res.event, url);
return; // just need to read from 1 relay
}
})();
}
}