mirror of
https://github.com/wukko/cobalt.git
synced 2024-11-15 12:50:01 +00:00
settings: expose settings that have only been modified
also reduce unnecessary loads from storage
This commit is contained in:
parent
2a0366a58d
commit
2cce4bd521
1 changed files with 12 additions and 7 deletions
|
@ -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<CobaltSettings>) => void;
|
||||
let update: (_: Updater<PartialSettings>) => void;
|
||||
|
||||
// deep merge partial type into full CobaltSettings type
|
||||
const mergeWithDefaults = (partial: PartialSettings) => {
|
||||
return merge(defaultSettings, partial) as CobaltSettings;
|
||||
}
|
||||
|
||||
export default readable<CobaltSettings>(
|
||||
mergeWithDefaults(loadFromStorage()),
|
||||
export const storedSettings = readable<PartialSettings>(
|
||||
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)
|
||||
);
|
Loading…
Reference in a new issue