Add diagnostic count: number of DB records

This commit is contained in:
2025-09-06 10:13:51 -07:00
parent 100470b8e2
commit 77d04c99a0
4 changed files with 30 additions and 2 deletions
+7 -1
View File
@@ -15,6 +15,8 @@ import * as Y from 'yjs';
interface NavbarProps { interface NavbarProps {
fileId: Accessor<string | null>; fileId: Accessor<string | null>;
setFileId: Setter<string | null>; setFileId: Setter<string | null>;
numUpdates: Accessor<number | null>;
setNumUpdates: Setter<number | null>;
} }
const NavbarContext = createContext<NavbarProps>(); const NavbarContext = createContext<NavbarProps>();
@@ -30,6 +32,7 @@ export function useNavbar(): NavbarProps {
function Layout(props: any) { function Layout(props: any) {
const navigate = useNavigate(); const navigate = useNavigate();
const [fileId, setFileId] = createSignal<string | null>(null); const [fileId, setFileId] = createSignal<string | null>(null);
const [numUpdates, setNumUpdates] = createSignal<number | null>(null);
const title = () => { const title = () => {
const id = fileId(); const id = fileId();
if (id === null) { if (id === null) {
@@ -43,13 +46,16 @@ function Layout(props: any) {
}; };
return ( return (
<NavbarContext.Provider value={{ fileId, setFileId }}> <NavbarContext.Provider
value={{ fileId, setFileId, numUpdates, setNumUpdates }}
>
<div class="app"> <div class="app">
<div class="navbar"> <div class="navbar">
<Show when={fileId() !== null}> <Show when={fileId() !== null}>
<button onclick={() => navigate('/')}>Back</button> <button onclick={() => navigate('/')}>Back</button>
</Show> </Show>
<div class="title">{title() ?? 'Choose a file'}</div> <div class="title">{title() ?? 'Choose a file'}</div>
<div class="info">{numUpdates() ?? ''}</div>
</div> </div>
{props.children} {props.children}
</div> </div>
+11 -1
View File
@@ -16,10 +16,11 @@ import {
import { LocalDocument } from '../sync'; import { LocalDocument } from '../sync';
import { useNavbar } from './App'; import { useNavbar } from './App';
import { useParams } from '@solidjs/router'; import { useParams } from '@solidjs/router';
import _ from 'lodash';
function Editor() { function Editor() {
const { id: fileId } = useParams(); const { id: fileId } = useParams();
const { setFileId } = useNavbar(); const { setFileId, setNumUpdates } = useNavbar();
onMount(() => { onMount(() => {
setFileId(fileId); setFileId(fileId);
}); });
@@ -35,6 +36,14 @@ function Editor() {
} }
const ydoc = doc.doc; 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({ const state = EditorState.create({
doc: ydoc.getText().toString(), doc: ydoc.getText().toString(),
extensions: [ extensions: [
@@ -54,6 +63,7 @@ function Editor() {
}); });
onCleanup(() => { onCleanup(() => {
setNumUpdates(null);
editorView?.destroy(); editorView?.destroy();
syncedDoc()?.finish(); syncedDoc()?.finish();
}); });
+4
View File
@@ -18,6 +18,10 @@ body {
background-color: #cde; background-color: #cde;
} }
.navbar .info {
margin-left: auto;
}
.editor-component { .editor-component {
flex: 1; flex: 1;
overflow: hidden; overflow: hidden;
+8
View File
@@ -23,11 +23,13 @@ export class LocalDocument {
readonly id: string; readonly id: string;
readonly doc: Y.Doc; readonly doc: Y.Doc;
private updates: Uint8Array[]; private updates: Uint8Array[];
private totalUpdates: number;
constructor(id: string) { constructor(id: string) {
this.id = id; this.id = id;
this.doc = new Y.Doc(); this.doc = new Y.Doc();
this.updates = []; this.updates = [];
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) => {
@@ -37,6 +39,10 @@ export class LocalDocument {
}); });
} }
get numUpdates(): number {
return this.totalUpdates;
}
public static async load(id: string): Promise<LocalDocument> { public static async load(id: string): Promise<LocalDocument> {
const updates: Uint8Array[] = await db.getAll( const updates: Uint8Array[] = await db.getAll(
LOCAL_UPDATE_STORE, LOCAL_UPDATE_STORE,
@@ -44,6 +50,7 @@ export class LocalDocument {
); );
// console.log(`load(${id}): got ${updates.length} updates`); // console.log(`load(${id}): got ${updates.length} updates`);
const result = new LocalDocument(id); const result = new LocalDocument(id);
result.totalUpdates = updates.length;
Y.applyUpdate(result.doc, Y.mergeUpdates(updates)); Y.applyUpdate(result.doc, Y.mergeUpdates(updates));
return result; return result;
} }
@@ -55,6 +62,7 @@ export class LocalDocument {
const update = Y.mergeUpdates(batch); const update = Y.mergeUpdates(batch);
const key = [this.id, new Date().valueOf()]; const key = [this.id, new Date().valueOf()];
await db.add(LOCAL_UPDATE_STORE, update, key); await db.add(LOCAL_UPDATE_STORE, update, key);
this.totalUpdates += 1;
} }
} }