MindSift CI & Docker Release / Build, Test in Container & Push (push) Successful in 1m26s
Phase 2 of the collaboration plan: - frontend/src/lib/docSync: TypeScript port of the block tree, operations, effects and order keys (kept identical to Go via shared test vectors), diff from the editor's flat list to operations, IndexedDB persistence, HTTP transport and DocSyncEngine (one request in flight, idempotent resends, backoff, 3 s/30 s polling, keepalive flush and leave on exit, block leases for focus, 20 s grace and new blocks). - Text changes carry the version the user saw when typing (first change wins when coalescing; a stale focused field keeps its old base), so a concurrent edit becomes a conflict copy instead of a silent overwrite. - DocumentEditor uses the engine instead of the 800 ms whole-document PUT; editors (not only owners) can edit, remote changes appear live. - Server: task IDs of linked blocks are delivered again, linked tasks follow ticked/deleted blocks after sync, note tags are derived, title changes notify other users; block IDs already used in another note are renamed (starter content with fixed IDs was empty for every further user). - Fix: unlinking tasks from blocks wrote to non-existent columns and never worked (error was swallowed). - Tests: Go/TS order key vectors, diff round trips, engine races (lost responses, offline + reload, edits arriving between typing and sending, stale focused field, duplicate first block), plus a Playwright test with two browsers against the real image (e2e/run.sh). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
88 lines
2.7 KiB
TypeScript
88 lines
2.7 KiB
TypeScript
// Persists the sync state of each note in IndexedDB so that typed text survives reloads, crashes and
|
|
// offline phases. Falls back to memory when IndexedDB is unavailable (private mode, old browsers).
|
|
import type { Block, Op } from './model';
|
|
|
|
export interface StoredDocState {
|
|
docId: string;
|
|
title: string;
|
|
titleVersion: number;
|
|
lastSeq: number;
|
|
role: string;
|
|
blocks: Block[];
|
|
pending: Op[];
|
|
inflight: Op[] | null;
|
|
savedAt: number;
|
|
}
|
|
|
|
export interface SyncStorage {
|
|
load(docId: string): Promise<StoredDocState | null>;
|
|
save(state: StoredDocState): Promise<void>;
|
|
}
|
|
|
|
const DB_NAME = 'mindsift-sync';
|
|
const STORE = 'docs';
|
|
|
|
export class MemoryStorage implements SyncStorage {
|
|
private data = new Map<string, StoredDocState>();
|
|
async load(docId: string) {
|
|
const s = this.data.get(docId);
|
|
return s ? JSON.parse(JSON.stringify(s)) : null;
|
|
}
|
|
async save(state: StoredDocState) {
|
|
this.data.set(state.docId, JSON.parse(JSON.stringify(state)));
|
|
}
|
|
}
|
|
|
|
export class IndexedDBStorage implements SyncStorage {
|
|
private dbPromise: Promise<IDBDatabase> | null = null;
|
|
|
|
constructor(private factory: IDBFactory = indexedDB) {}
|
|
|
|
private open(): Promise<IDBDatabase> {
|
|
if (!this.dbPromise) {
|
|
this.dbPromise = new Promise((resolve, reject) => {
|
|
const req = this.factory.open(DB_NAME, 1);
|
|
req.onupgradeneeded = () => {
|
|
if (!req.result.objectStoreNames.contains(STORE)) req.result.createObjectStore(STORE, { keyPath: 'docId' });
|
|
};
|
|
req.onsuccess = () => resolve(req.result);
|
|
req.onerror = () => reject(req.error);
|
|
});
|
|
this.dbPromise.catch(() => {
|
|
this.dbPromise = null;
|
|
});
|
|
}
|
|
return this.dbPromise;
|
|
}
|
|
|
|
async load(docId: string): Promise<StoredDocState | null> {
|
|
const db = await this.open();
|
|
return new Promise((resolve, reject) => {
|
|
const req = db.transaction(STORE, 'readonly').objectStore(STORE).get(docId);
|
|
req.onsuccess = () => resolve((req.result as StoredDocState) || null);
|
|
req.onerror = () => reject(req.error);
|
|
});
|
|
}
|
|
|
|
async save(state: StoredDocState): Promise<void> {
|
|
const db = await this.open();
|
|
return new Promise((resolve, reject) => {
|
|
const tx = db.transaction(STORE, 'readwrite');
|
|
tx.objectStore(STORE).put(state);
|
|
tx.oncomplete = () => resolve();
|
|
tx.onerror = () => reject(tx.error);
|
|
tx.onabort = () => reject(tx.error);
|
|
});
|
|
}
|
|
}
|
|
|
|
/** IndexedDB when available, otherwise memory (state is then lost on reload). */
|
|
export function defaultStorage(): SyncStorage {
|
|
try {
|
|
if (typeof indexedDB !== 'undefined') return new IndexedDBStorage(indexedDB);
|
|
} catch {
|
|
// ignore
|
|
}
|
|
return new MemoryStorage();
|
|
}
|