fix(preferences): use Object.assign mutation to prevent infinite effect loops

Replacing the preferences object (spread reassignment) caused all consumers'
prefs references to go stale — the loop guard read the old value forever.
Svelte 5 shared state pattern requires a stable const object with property
mutations, not reassignment. Also removes duplicate theme sync $effect from
SettingsPage (already handled in +layout.svelte).
This commit is contained in:
2026-04-18 10:18:29 +01:00
parent 61c96d7805
commit e436a69839
2 changed files with 6 additions and 14 deletions

View File

@@ -281,14 +281,6 @@
saveSettings(); saveSettings();
}); });
// Sync theme setting to preferences store
$effect(() => {
const map = { Dark: 'dark', Light: 'light', System: 'system' };
const mapped = map[settings.theme] || 'dark';
if (prefs.theme !== mapped) {
updatePreferences({ theme: mapped });
}
});
$effect(() => { $effect(() => {
settings.engine; settings.engine;

View File

@@ -98,7 +98,9 @@ function persistToSQLite(prefs) {
}, 500); }, 500);
} }
let preferences = $state(readFromDOM()); // Export as const so it is never reassigned — consumers hold a stable reference
// and mutations are tracked by Svelte 5's deep reactivity.
export const preferences = $state(readFromDOM());
// Ensure data-theme and zone attributes are always present on the DOM, even // Ensure data-theme and zone attributes are always present on the DOM, even
// when there is no Tauri webview injection script (e.g. browser dev mode). // when there is no Tauri webview injection script (e.g. browser dev mode).
@@ -106,21 +108,19 @@ if (typeof window !== 'undefined') {
applyToDOM(preferences); applyToDOM(preferences);
} }
/** @deprecated Use `preferences` directly — kept for backwards compatibility */
export function getPreferences() { export function getPreferences() {
return preferences; return preferences;
} }
export function updatePreferences(updates) { export function updatePreferences(updates) {
preferences = { ...preferences, ...updates }; Object.assign(preferences, updates);
applyToDOM(preferences); applyToDOM(preferences);
persistToSQLite(preferences); persistToSQLite(preferences);
} }
export function updateAccessibility(updates) { export function updateAccessibility(updates) {
preferences = { Object.assign(preferences.accessibility, updates);
...preferences,
accessibility: { ...preferences.accessibility, ...updates }
};
applyToDOM(preferences); applyToDOM(preferences);
persistToSQLite(preferences); persistToSQLite(preferences);
} }