Fix bug: extra update blobs generated on doc load

This commit is contained in:
2025-09-07 00:31:36 -07:00
parent d65d1307e2
commit f832191c0b
2 changed files with 8 additions and 7 deletions
-1
View File
@@ -10,7 +10,6 @@ import Chooser from './Chooser';
import Editor from './Editor'; import Editor from './Editor';
import { HashRouter, Route, useNavigate } from '@solidjs/router'; import { HashRouter, Route, useNavigate } from '@solidjs/router';
import { defaultVault } from '../sync'; import { defaultVault } from '../sync';
import * as Y from 'yjs';
interface NavbarProps { interface NavbarProps {
fileId: Accessor<string | null>; fileId: Accessor<string | null>;
+8 -6
View File
@@ -26,15 +26,14 @@ export class LocalDocument {
private updates: Uint8Array[]; private updates: Uint8Array[];
private totalUpdates: number; private totalUpdates: number;
constructor(id: string) { private constructor(id: string, ydoc: Y.Doc) {
this.id = id; this.id = id;
this.doc = new Y.Doc(); this.doc = ydoc;
this.updates = []; this.updates = [];
this.totalUpdates = 0; this.totalUpdates = 0;
const flush = _.debounce(() => this.flush(), 1000); const flush = _.debounce(() => this.flush(), 1000);
this.doc.on('update', (update: Uint8Array) => { this.doc.on('update', (update: Uint8Array) => {
// TODO: Is this generating update events on load?
this.updates.push(update); this.updates.push(update);
flush(); flush();
}); });
@@ -45,14 +44,17 @@ export class LocalDocument {
} }
public static async load(id: string): Promise<LocalDocument> { public static async load(id: string): Promise<LocalDocument> {
// Build Y.Doc from records on disk
const updates: Uint8Array[] = await db.getAll( const updates: Uint8Array[] = await db.getAll(
LOCAL_UPDATE_STORE, LOCAL_UPDATE_STORE,
IDBKeyRange.bound([id, -Infinity], [id, Infinity]) IDBKeyRange.bound([id, -Infinity], [id, Infinity])
); );
// console.log(`load(${id}): got ${updates.length} updates`); const ydoc = new Y.Doc();
const result = new LocalDocument(id); Y.applyUpdate(ydoc, Y.mergeUpdates(updates));
// Build LocalDocument instance
const result = new LocalDocument(id, ydoc);
result.totalUpdates = updates.length; result.totalUpdates = updates.length;
Y.applyUpdate(result.doc, Y.mergeUpdates(updates));
return result; return result;
} }