mastoradio/src/services/store.js

12 lines
421 B
JavaScript
Raw Permalink Normal View History

2020-02-25 17:21:34 +01:00
import { writable, tap } from 'svelte-pipeable-store'
2020-02-18 19:18:46 +01:00
export { get } from 'svelte/store'
export * from 'svelte-pipeable-store'
2020-02-25 17:21:34 +01:00
export const writableStorage = (storage, storageKey, defaultValue) => {
const item = storage.getItem(storageKey)
const value = item === null ? defaultValue : JSON.parse(item)
2020-02-18 19:18:46 +01:00
2020-02-25 17:21:34 +01:00
return writable(value)
.pipe(tap(value => storage.setItem(storageKey, JSON.stringify(value))))
2020-02-18 19:18:46 +01:00
}