fix elevation profile fill not resetting, closes #40

This commit is contained in:
vcoppe
2024-08-08 23:39:58 +02:00
parent 1cadc870f7
commit 968e26cd02

View File

@@ -42,14 +42,22 @@ class Database extends Dexie {
export const db = new Database(); export const db = new Database();
// Wrap Dexie live queries in a Svelte store to avoid triggering the query for every subscriber, and updates to the store are pushed to the DB // Wrap Dexie live queries in a Svelte store to avoid triggering the query for every subscriber, and updates to the store are pushed to the DB
export function bidirectionalDexieStore<K, V>(table: Dexie.Table<V, K>, key: K, initial: V, initialize: boolean = true): Writable<V> { export function bidirectionalDexieStore<K, V>(table: Dexie.Table<V, K>, key: K, initial: V, initialize: boolean = true): Writable<V | undefined> {
let store = writable(initialize ? initial : undefined); let first = true;
let store = writable<V | undefined>(initialize ? initial : undefined);
liveQuery(() => table.get(key)).subscribe(value => { liveQuery(() => table.get(key)).subscribe(value => {
if (value === undefined && !initialize) { if (value === undefined) {
store.set(initial); if (first) {
} else if (value !== undefined) { if (!initialize) {
store.set(initial);
}
} else {
store.set(value);
}
} else {
store.set(value); store.set(value);
} }
first = false;
}); });
return { return {
subscribe: store.subscribe, subscribe: store.subscribe,