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