refactor LS Store

This commit is contained in:
Nolan Lawson 2018-01-28 14:56:25 -08:00
parent 3896717c8c
commit 22399df264
1 changed files with 5 additions and 1 deletions

View File

@ -2,6 +2,10 @@ import { Store } from 'svelte/store'
const LS = process.browser && localStorage
function safeParse(str) {
return !str ? undefined : (str === 'undefined' ? undefined : JSON.parse(str))
}
export class LocalStorageStore extends Store {
constructor(state, keysToWatch) {
super(state)
@ -15,7 +19,7 @@ export class LocalStorageStore extends Store {
let key = LS.key(i)
if (key.startsWith('store_')) {
let item = LS.getItem(key)
newState[key.substring(6)] = item === 'undefined' ? undefined : JSON.parse(item)
newState[key.substring(6)] = safeParse(item)
}
}
this.set(newState)