mirror of
https://github.com/CharaChorder/DeviceManager.git
synced 2026-01-06 18:12:51 +00:00
18 lines
581 B
TypeScript
18 lines
581 B
TypeScript
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)
|
|
}
|
|
}
|