From 2cce4bd5211b8ca0060527affaf363c41c938017 Mon Sep 17 00:00:00 2001 From: dumbmoron Date: Sat, 13 Jul 2024 12:25:27 +0000 Subject: [PATCH] settings: expose settings that have only been modified also reduce unnecessary loads from storage --- web/src/lib/settings.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/web/src/lib/settings.ts b/web/src/lib/settings.ts index c09f1f6b..9ce5b53b 100644 --- a/web/src/lib/settings.ts +++ b/web/src/lib/settings.ts @@ -1,4 +1,4 @@ -import { readable, type Updater } from 'svelte/store'; +import { derived, readable, type Updater } from 'svelte/store'; import { merge } from 'ts-deepmerge'; import type { RecursivePartial } from './types/generic'; @@ -49,25 +49,30 @@ const loadFromStorage = () => { return parsed; } -let update: (_: Updater) => void; +let update: (_: Updater) => void; // deep merge partial type into full CobaltSettings type const mergeWithDefaults = (partial: PartialSettings) => { return merge(defaultSettings, partial) as CobaltSettings; } -export default readable( - mergeWithDefaults(loadFromStorage()), +export const storedSettings = readable( + loadFromStorage(), (_, _update) => { update = _update } ); // update settings from outside export function updateSetting(partial: PartialSettings) { - update(() => { + update((current) => { const updated = writeToStorage( - merge(loadFromStorage(), partial) + merge(current, partial) ); - return mergeWithDefaults(updated); + return updated; }); } + +export default derived( + storedSettings, + $settings => mergeWithDefaults($settings) +); \ No newline at end of file