Skip to content

Commit

Permalink
fix: blocking persistent stores
Browse files Browse the repository at this point in the history
  • Loading branch information
kurozenzen committed May 7, 2023
1 parent 98bfa21 commit 2e55011
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/lib/common/persistentStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const defaultSerializer = JSON.stringify
*/
export const createPersistentStore = (key, initial, serializer = defaultSerializer, parser = defaultParser) => {
/** @type {null | T} */
const loaded = tryLoad(key, parser)
const loaded = tryLoad(key, parser, initial)
const store = writable(loaded ?? initial)
store.subscribe((value) => localStorage.setItem(key, serializer(value)))

Expand Down Expand Up @@ -47,10 +47,17 @@ export const createDependentPersistentStore = (
* @returns {T | null}
* @param {string} key
* @param {(value: string) => T} parser
* @param {T} initial
*/
const tryLoad = (key, parser) => {
const tryLoad = (key, parser, initial) => {
try {
return parser(localStorage.getItem(key))
const result = parser(localStorage.getItem(key))

if (typeof result === 'object' && typeof initial === 'object') {
return { ...initial, ...result }
}

return result
} catch {
return null
}
Expand Down
1 change: 0 additions & 1 deletion src/lib/search/SortFilterEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import sortStore from './sortStore'
import Button from '../common/Button.svelte'
import Dialog from '../dialog/Dialog.svelte'
import { createEventDispatcher } from 'svelte'
import Selection from '../common/Selection.svelte'
import RotatingIconSelection from '../common/RotatingIconSelection.svelte'
import RotatingTextSelection from '../common/RotatingTextSelection.svelte'
Expand Down

0 comments on commit 2e55011

Please sign in to comment.