import * as Y from "yjs"; import { ThreadData } from "../../types.js"; import { ThreadStore } from "../ThreadStore.js"; import { ThreadStoreAuth } from "../ThreadStoreAuth.js"; import { yMapToThread } from "./yjsHelpers.js"; /** * This is an abstract class that only implements the READ methods required by the ThreadStore interface. * The data is read from a Yjs Map. */ export abstract class YjsThreadStoreBase extends ThreadStore { constructor( protected readonly threadsYMap: Y.Map, auth: ThreadStoreAuth, ) { super(auth); } // TODO: async / reactive interface? public getThread(threadId: string) { const yThread = this.threadsYMap.get(threadId); if (!yThread) { throw new Error("Thread not found"); } const thread = yMapToThread(yThread); return thread; } public getThreads(): Map { const threadMap = new Map(); this.threadsYMap.forEach((yThread, id) => { if (yThread instanceof Y.Map) { threadMap.set(id, yMapToThread(yThread)); } }); return threadMap; } public subscribe(cb: (threads: Map) => void) { const observer = () => { cb(this.getThreads()); }; this.threadsYMap.observeDeep(observer); return () => { this.threadsYMap.unobserveDeep(observer); }; } }