From 77d04c99a0123305bed179e47cbe8e9b30d3862c Mon Sep 17 00:00:00 2001 From: Chris Mounce Date: Sat, 6 Sep 2025 10:13:51 -0700 Subject: [PATCH] Add diagnostic count: number of DB records --- src/components/App.tsx | 8 +++++++- src/components/Editor.tsx | 12 +++++++++++- src/main.css | 4 ++++ src/sync.ts | 8 ++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/components/App.tsx b/src/components/App.tsx index a788c71..cda16a9 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -15,6 +15,8 @@ import * as Y from 'yjs'; interface NavbarProps { fileId: Accessor; setFileId: Setter; + numUpdates: Accessor; + setNumUpdates: Setter; } const NavbarContext = createContext(); @@ -30,6 +32,7 @@ export function useNavbar(): NavbarProps { function Layout(props: any) { const navigate = useNavigate(); const [fileId, setFileId] = createSignal(null); + const [numUpdates, setNumUpdates] = createSignal(null); const title = () => { const id = fileId(); if (id === null) { @@ -43,13 +46,16 @@ function Layout(props: any) { }; return ( - +
{props.children}
diff --git a/src/components/Editor.tsx b/src/components/Editor.tsx index bb138a1..474157c 100644 --- a/src/components/Editor.tsx +++ b/src/components/Editor.tsx @@ -16,10 +16,11 @@ import { import { LocalDocument } from '../sync'; import { useNavbar } from './App'; import { useParams } from '@solidjs/router'; +import _ from 'lodash'; function Editor() { const { id: fileId } = useParams(); - const { setFileId } = useNavbar(); + const { setFileId, setNumUpdates } = useNavbar(); onMount(() => { setFileId(fileId); }); @@ -35,6 +36,14 @@ function Editor() { } const ydoc = doc.doc; + const refreshNumUpdates = _.debounce(() => { + setNumUpdates(doc.numUpdates); + }, 100); + setNumUpdates(doc.numUpdates); + doc.doc.on('update', () => { + setTimeout(refreshNumUpdates, 1500); + }); + const state = EditorState.create({ doc: ydoc.getText().toString(), extensions: [ @@ -54,6 +63,7 @@ function Editor() { }); onCleanup(() => { + setNumUpdates(null); editorView?.destroy(); syncedDoc()?.finish(); }); diff --git a/src/main.css b/src/main.css index e354e0f..f0712b4 100644 --- a/src/main.css +++ b/src/main.css @@ -18,6 +18,10 @@ body { background-color: #cde; } +.navbar .info { + margin-left: auto; +} + .editor-component { flex: 1; overflow: hidden; diff --git a/src/sync.ts b/src/sync.ts index 89e3ab6..426e71e 100644 --- a/src/sync.ts +++ b/src/sync.ts @@ -23,11 +23,13 @@ export class LocalDocument { readonly id: string; readonly doc: Y.Doc; private updates: Uint8Array[]; + private totalUpdates: number; constructor(id: string) { this.id = id; this.doc = new Y.Doc(); this.updates = []; + this.totalUpdates = 0; const flush = _.debounce(() => this.flush(), 1000); this.doc.on('update', (update: Uint8Array) => { @@ -37,6 +39,10 @@ export class LocalDocument { }); } + get numUpdates(): number { + return this.totalUpdates; + } + public static async load(id: string): Promise { const updates: Uint8Array[] = await db.getAll( LOCAL_UPDATE_STORE, @@ -44,6 +50,7 @@ export class LocalDocument { ); // console.log(`load(${id}): got ${updates.length} updates`); const result = new LocalDocument(id); + result.totalUpdates = updates.length; Y.applyUpdate(result.doc, Y.mergeUpdates(updates)); return result; } @@ -55,6 +62,7 @@ export class LocalDocument { const update = Y.mergeUpdates(batch); const key = [this.id, new Date().valueOf()]; await db.add(LOCAL_UPDATE_STORE, update, key); + this.totalUpdates += 1; } }