/** * rWork Local-First Client * * Wraps the shared local-first stack into a work/kanban-specific API. */ import * as Automerge from '@automerge/automerge'; import { DocumentManager } from '../../shared/local-first/document'; import type { DocumentId } from '../../shared/local-first/document'; import { EncryptedDocStore } from '../../shared/local-first/storage'; import { DocSyncManager } from '../../shared/local-first/sync'; import { DocCrypto } from '../../shared/local-first/crypto'; import { boardSchema, boardDocId } from './schemas'; import type { BoardDoc, TaskItem, BoardMeta } from './schemas'; export class WorkLocalFirstClient { #space: string; #documents: DocumentManager; #store: EncryptedDocStore; #sync: DocSyncManager; #initialized = false; constructor(space: string, docCrypto?: DocCrypto) { this.#space = space; this.#documents = new DocumentManager(); this.#store = new EncryptedDocStore(space, docCrypto); this.#sync = new DocSyncManager({ documents: this.#documents, store: this.#store, }); this.#documents.registerSchema(boardSchema); } get isConnected(): boolean { return this.#sync.isConnected; } get isInitialized(): boolean { return this.#initialized; } async init(): Promise { if (this.#initialized) return; await this.#store.open(); const cachedIds = await this.#store.listByModule('work', 'boards'); for (const docId of cachedIds) { const binary = await this.#store.load(docId); if (binary) this.#documents.open(docId, boardSchema, binary); } const proto = location.protocol === 'https:' ? 'wss:' : 'ws:'; const wsUrl = `${proto}//${location.host}/ws/${this.#space}`; try { await this.#sync.connect(wsUrl, this.#space); } catch { console.warn('[WorkClient] Working offline'); } this.#initialized = true; } async subscribeBoard(boardId: string): Promise { const docId = boardDocId(this.#space, boardId) as DocumentId; let doc = this.#documents.get(docId); if (!doc) { const binary = await this.#store.load(docId); doc = binary ? this.#documents.open(docId, boardSchema, binary) : this.#documents.open(docId, boardSchema); } await this.#sync.subscribe([docId]); return doc ?? null; } getBoard(boardId: string): BoardDoc | undefined { return this.#documents.get(boardDocId(this.#space, boardId) as DocumentId); } updateTask(boardId: string, taskId: string, changes: Partial): void { const docId = boardDocId(this.#space, boardId) as DocumentId; this.#sync.change(docId, `Update task ${taskId}`, (d) => { if (!d.tasks[taskId]) { d.tasks[taskId] = { id: taskId, spaceId: boardId, title: '', description: '', status: 'TODO', priority: null, labels: [], assigneeId: null, createdBy: null, sortOrder: 0, createdAt: Date.now(), updatedAt: Date.now(), ...changes }; } else { Object.assign(d.tasks[taskId], changes); d.tasks[taskId].updatedAt = Date.now(); } }); } deleteTask(boardId: string, taskId: string): void { const docId = boardDocId(this.#space, boardId) as DocumentId; this.#sync.change(docId, `Delete task ${taskId}`, (d) => { delete d.tasks[taskId]; }); } updateBoard(boardId: string, changes: Partial): void { const docId = boardDocId(this.#space, boardId) as DocumentId; this.#sync.change(docId, 'Update board', (d) => { Object.assign(d.board, changes); d.board.updatedAt = Date.now(); }); } onChange(boardId: string, cb: (doc: BoardDoc) => void): () => void { return this.#sync.onChange(boardDocId(this.#space, boardId) as DocumentId, cb as (doc: any) => void); } onConnect(cb: () => void): () => void { return this.#sync.onConnect(cb); } onDisconnect(cb: () => void): () => void { return this.#sync.onDisconnect(cb); } async disconnect(): Promise { await this.#sync.flush(); this.#sync.disconnect(); } }