be explicit about local storage usage

This commit is contained in:
Nolan Lawson 2018-01-22 21:55:25 -08:00
parent fcfe409633
commit 9c287c0727

View file

@ -1,8 +1,14 @@
import { Store } from 'svelte/store.js' import { Store } from 'svelte/store.js'
const DONT_STORE_THESE_KEYS = [ const LOCAL_STORAGE_KEYS = new Set([
'cachedAccountNames' "currentInstance",
] "currentRegisteredInstance",
"currentRegisteredInstanceName",
"instanceNameInSearch",
"instanceThemes",
"loggedInInstances",
"loggedInInstancesInOrder"
])
const LS = process.browser && localStorage const LS = process.browser && localStorage
class LocalStorageStore extends Store { class LocalStorageStore extends Store {
@ -10,7 +16,7 @@ class LocalStorageStore extends Store {
constructor(state) { constructor(state) {
super(state) super(state)
if (process.browser) { if (process.browser) {
this.lastChanged = {} this.keysToStore = {}
let newState = {} let newState = {}
for (let i = 0, len = LS.length; i < len; i++) { for (let i = 0, len = LS.length; i < len; i++) {
let key = LS.key(i) let key = LS.key(i)
@ -22,9 +28,8 @@ class LocalStorageStore extends Store {
this.set(newState) this.set(newState)
this.onchange((state, changed) => { this.onchange((state, changed) => {
Object.keys(changed).forEach(change => { Object.keys(changed).forEach(change => {
if (!DONT_STORE_THESE_KEYS.includes(change) && if (LOCAL_STORAGE_KEYS.has(change)) {
!this._computed[change]) { // TODO: better way to ignore computed values? this.keysToStore[change] = true
this.lastChanged[change] = true
} }
}) })
}) })
@ -33,10 +38,10 @@ class LocalStorageStore extends Store {
save() { save() {
if (process.browser) { if (process.browser) {
Object.keys(this.lastChanged).forEach(key => { Object.keys(this.keysToStore).forEach(key => {
LS.setItem(`store_${key}`, JSON.stringify(this.get(key))) LS.setItem(`store_${key}`, JSON.stringify(this.get(key)))
}) })
this.lastChanged = {} this.keysToStore = {}
} }
} }
} }