From f832191c0bd06585936caddd1e6684944bbcefdd Mon Sep 17 00:00:00 2001 From: Chris Mounce Date: Sun, 7 Sep 2025 00:31:36 -0700 Subject: [PATCH] Fix bug: extra update blobs generated on doc load --- src/components/App.tsx | 1 - src/sync.ts | 14 ++++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/components/App.tsx b/src/components/App.tsx index 9f8acc4..2ae71c9 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -10,7 +10,6 @@ import Chooser from './Chooser'; import Editor from './Editor'; import { HashRouter, Route, useNavigate } from '@solidjs/router'; import { defaultVault } from '../sync'; -import * as Y from 'yjs'; interface NavbarProps { fileId: Accessor; diff --git a/src/sync.ts b/src/sync.ts index ed7dcbc..278eb25 100644 --- a/src/sync.ts +++ b/src/sync.ts @@ -26,15 +26,14 @@ export class LocalDocument { private updates: Uint8Array[]; private totalUpdates: number; - constructor(id: string) { + private constructor(id: string, ydoc: Y.Doc) { this.id = id; - this.doc = new Y.Doc(); + this.doc = ydoc; this.updates = []; this.totalUpdates = 0; const flush = _.debounce(() => this.flush(), 1000); this.doc.on('update', (update: Uint8Array) => { - // TODO: Is this generating update events on load? this.updates.push(update); flush(); }); @@ -45,14 +44,17 @@ export class LocalDocument { } public static async load(id: string): Promise { + // Build Y.Doc from records on disk const updates: Uint8Array[] = await db.getAll( LOCAL_UPDATE_STORE, IDBKeyRange.bound([id, -Infinity], [id, Infinity]) ); - // console.log(`load(${id}): got ${updates.length} updates`); - const result = new LocalDocument(id); + const ydoc = new Y.Doc(); + Y.applyUpdate(ydoc, Y.mergeUpdates(updates)); + + // Build LocalDocument instance + const result = new LocalDocument(id, ydoc); result.totalUpdates = updates.length; - Y.applyUpdate(result.doc, Y.mergeUpdates(updates)); return result; }