mirror of
https://github.com/CharaChorder/DeviceManager.git
synced 2026-01-20 17:03:42 +00:00
feat: de-clutter navbar
fix: backup option not working refactor: persistent writable stores [deploy]
This commit is contained in:
17
src/lib/storage.ts
Normal file
17
src/lib/storage.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import type {Writable} from "svelte/store"
|
||||
import {writable} from "svelte/store"
|
||||
import {browser} from "$app/environment"
|
||||
|
||||
export function persistentWritable<T>(key: string, value: T, condition?: () => boolean): Writable<T> {
|
||||
if (browser) {
|
||||
const persistedValue = localStorage.getItem(key)
|
||||
const store = persistedValue !== null ? writable(JSON.parse(persistedValue)) : writable(value)
|
||||
store.subscribe(value => {
|
||||
if (!condition || condition()) localStorage.setItem(key, JSON.stringify(value))
|
||||
})
|
||||
|
||||
return store
|
||||
} else {
|
||||
return writable(value)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user