avoid livequerying all patches

This commit is contained in:
vcoppe
2024-05-04 11:00:56 +02:00
parent d368d9361c
commit 3a01e62101

View File

@@ -107,9 +107,9 @@ liveQuery(() => db.fileids.toArray()).subscribe(dbFileIds => {
}); });
const patchIndex: Readable<number> = dexieStore(() => db.settings.get('patchIndex'), -1); const patchIndex: Readable<number> = dexieStore(() => db.settings.get('patchIndex'), -1);
const patches: Readable<{ patch: Patch[], inversePatch: Patch[] }[]> = dexieStore(() => db.patches.toArray(), []); const patchCount: Readable<number> = dexieStore(() => db.patches.count(), 0);
export const canUndo: Readable<boolean> = derived(patchIndex, ($patchIndex) => $patchIndex >= 0); export const canUndo: Readable<boolean> = derived(patchIndex, ($patchIndex) => $patchIndex >= 0);
export const canRedo: Readable<boolean> = derived([patchIndex, patches], ([$patchIndex, $patches]) => $patchIndex < $patches.length - 1); export const canRedo: Readable<boolean> = derived([patchIndex, patchCount], ([$patchIndex, $patchCount]) => $patchIndex < $patchCount - 1);
export function applyGlobal(callback: (files: Map<string, GPXFile>) => void) { export function applyGlobal(callback: (files: Map<string, GPXFile>) => void) {
const [newFileState, patch, inversePatch] = produceWithPatches(fileState, callback); const [newFileState, patch, inversePatch] = produceWithPatches(fileState, callback);
@@ -177,17 +177,25 @@ function getFileIds(n: number) {
export function undo() { export function undo() {
if (get(canUndo)) { if (get(canUndo)) {
let index = get(patchIndex); let index = get(patchIndex);
applyPatch(get(patches)[index].inversePatch); db.patches.get(index).then(patch => {
if (patch) {
applyPatch(patch.inversePatch);
db.settings.put(index - 1, 'patchIndex'); db.settings.put(index - 1, 'patchIndex');
} }
});
}
} }
export function redo() { export function redo() {
if (get(canRedo)) { if (get(canRedo)) {
let index = get(patchIndex) + 1; let index = get(patchIndex) + 1;
applyPatch(get(patches)[index].patch); db.patches.get(index).then(patch => {
if (patch) {
applyPatch(patch.patch);
db.settings.put(index, 'patchIndex'); db.settings.put(index, 'patchIndex');
} }
});
}
} }
export const dbUtils = { export const dbUtils = {