Merge pull request 'feat: merge localState recursively with initial' (#38) from feat/debug-node into main
All checks were successful
🚀 Lint & Test & Deploy / release (push) Successful in 3m50s
All checks were successful
🚀 Lint & Test & Deploy / release (push) Successful in 3m50s
Reviewed-on: #38
This commit was merged in pull request #38.
This commit is contained in:
@@ -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(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user