import type { NostrEvent, OkResponse, ReqCommand, ReqFilter, TaggedNostrEvent } from "./nostr" import type { CacheRelay } from "./cache-relay" import type { Connection } from "./connection" import { NoteCollection } from "./note-collection" /** * Use a regular connection as a CacheRelay */ export class ConnectionCacheRelay implements CacheRelay { #eventsSent = new Set() static isInstance(obj: unknown): obj is ConnectionCacheRelay { return obj instanceof ConnectionCacheRelay || (typeof obj === "object" && obj !== null && "connection" in obj) } constructor(readonly connection: Connection) {} async event(ev: NostrEvent): Promise { if (this.#eventsSent.has(ev.id)) return { ok: true, id: ev.id, message: "duplicate", } as OkResponse this.#eventsSent.add(ev.id) return await this.connection.publish(ev) } query(req: ReqCommand): Promise> { const id = crypto.randomUUID() return new Promise((resolve, reject) => { const results = new NoteCollection() const evh = (s: string, e: TaggedNostrEvent) => { if (s === id) { results.add(e) } } const eoh = (s: string) => { if (s === id) { resolve(results.snapshot) this.connection.closeRequest(id) this.connection.off("unverifiedEvent", evh) this.connection.off("eose", eoh) this.connection.off("closed", eoh) } } this.connection.on("unverifiedEvent", evh) this.connection.on("eose", eoh) this.connection.on("closed", eoh) this.connection.request(["REQ", id, ...(req.slice(2) as Array)]) }) } delete(req: ReqCommand): Promise { // ignored return Promise.resolve([]) } }