Only persist state after we loaded one.

This commit is contained in:
Roger Braun 2017-04-09 16:30:21 +02:00
parent a53555254a
commit e531c78079

View file

@ -3,6 +3,8 @@ import objectPath from 'object-path'
import localforage from 'localforage' import localforage from 'localforage'
import { throttle, each } from 'lodash' import { throttle, each } from 'lodash'
let loaded = false
const defaultReducer = (state, paths) => ( const defaultReducer = (state, paths) => (
paths.length === 0 ? state : paths.reduce((substate, path) => { paths.length === 0 ? state : paths.reduce((substate, path) => {
objectPath.set(substate, path, objectPath.get(state, path)) objectPath.set(substate, path, objectPath.get(state, path))
@ -15,8 +17,12 @@ const defaultStorage = (() => {
})() })()
const defaultSetState = (key, state, storage) => { const defaultSetState = (key, state, storage) => {
if (!loaded) {
console.log('waiting for old state to be loaded...')
} else {
return storage.setItem(key, state) return storage.setItem(key, state)
} }
}
export default function createPersistedState ({ export default function createPersistedState ({
key = 'vuex-lz', key = 'vuex-lz',
@ -44,6 +50,7 @@ export default function createPersistedState ({
merge({}, store.state, savedState) merge({}, store.state, savedState)
) )
} }
loaded = true
}) })
subscriber(store)((mutation, state) => { subscriber(store)((mutation, state) => {