feat: merge localState recursively with initial #38

Merged
max merged 1 commits from feat/debug-node into main 2026-02-12 12:51:29 +01:00

View File

@@ -1,5 +1,37 @@
import { browser } from '$app/environment'; import { browser } from '$app/environment';
function mergeRecursive<T>(current: T, initial: T): T {
if (typeof initial === 'number') {
if (typeof current === 'number') return current;
return initial;
}
if (typeof initial === 'boolean') {
if (typeof current === 'boolean') return current;
return initial;
}
if (Array.isArray(initial)) {
if (Array.isArray(current)) return current;
return initial;
}
if (typeof initial === 'object' && initial) {
const merged = initial;
if (typeof current === 'object' && current) {
for (const key of Object.keys(initial)) {
if (key in current) {
// @ts-expect-error It's safe dont worry about it
merged[key] = mergeRecursive(current[key], initial[key]);
}
}
}
return merged;
}
return current;
}
export class LocalStore<T> { export class LocalStore<T> {
value = $state<T>() as T; value = $state<T>() as T;
key = ''; key = '';
@@ -10,7 +42,10 @@ export class LocalStore<T> {
if (browser) { if (browser) {
const item = localStorage.getItem(key); const item = localStorage.getItem(key);
if (item) this.value = this.deserialize(item); if (item) {
const storedValue = this.deserialize(item);
this.value = mergeRecursive(storedValue, value);
}
} }
$effect.root(() => { $effect.root(() => {