From 3a01e621010f6be23f1f408f1dc1ac1e06b974b2 Mon Sep 17 00:00:00 2001 From: vcoppe Date: Sat, 4 May 2024 11:00:56 +0200 Subject: [PATCH] avoid livequerying all patches --- website/src/lib/db.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/website/src/lib/db.ts b/website/src/lib/db.ts index 7acd6ead..ee382b99 100644 --- a/website/src/lib/db.ts +++ b/website/src/lib/db.ts @@ -107,9 +107,9 @@ liveQuery(() => db.fileids.toArray()).subscribe(dbFileIds => { }); const patchIndex: Readable = dexieStore(() => db.settings.get('patchIndex'), -1); -const patches: Readable<{ patch: Patch[], inversePatch: Patch[] }[]> = dexieStore(() => db.patches.toArray(), []); +const patchCount: Readable = dexieStore(() => db.patches.count(), 0); export const canUndo: Readable = derived(patchIndex, ($patchIndex) => $patchIndex >= 0); -export const canRedo: Readable = derived([patchIndex, patches], ([$patchIndex, $patches]) => $patchIndex < $patches.length - 1); +export const canRedo: Readable = derived([patchIndex, patchCount], ([$patchIndex, $patchCount]) => $patchIndex < $patchCount - 1); export function applyGlobal(callback: (files: Map) => void) { const [newFileState, patch, inversePatch] = produceWithPatches(fileState, callback); @@ -177,16 +177,24 @@ function getFileIds(n: number) { export function undo() { if (get(canUndo)) { let index = get(patchIndex); - applyPatch(get(patches)[index].inversePatch); - db.settings.put(index - 1, 'patchIndex'); + db.patches.get(index).then(patch => { + if (patch) { + applyPatch(patch.inversePatch); + db.settings.put(index - 1, 'patchIndex'); + } + }); } } export function redo() { if (get(canRedo)) { let index = get(patchIndex) + 1; - applyPatch(get(patches)[index].patch); - db.settings.put(index, 'patchIndex'); + db.patches.get(index).then(patch => { + if (patch) { + applyPatch(patch.patch); + db.settings.put(index, 'patchIndex'); + } + }); } }