Initial version

This commit is contained in:
Henry Jameson 2018-09-07 18:17:17 +03:00
parent 7887e42fca
commit f1c16327b6
4 changed files with 69 additions and 13 deletions

View file

@ -1,7 +1,7 @@
import merge from 'lodash.merge'
import objectPath from 'object-path'
import localforage from 'localforage'
import { throttle, each } from 'lodash'
import { each } from 'lodash'
let loaded = false
@ -12,18 +12,17 @@ const defaultReducer = (state, paths) => (
}, {})
)
const saveImmedeatelyActions = [
'markNotificationsAsSeen',
'clearCurrentUser',
'setCurrentUser',
'setOption'
]
const defaultStorage = (() => {
return localforage
})()
const defaultSetState = (key, state, storage) => {
if (!loaded) {
console.log('waiting for old state to be loaded...')
} else {
return storage.setItem(key, state)
}
}
export default function createPersistedState ({
key = 'vuex-lz',
paths = [],
@ -31,7 +30,14 @@ export default function createPersistedState ({
let value = storage.getItem(key)
return value
},
setState = throttle(defaultSetState, 60000),
setState = (key, state, storage) => {
if (!loaded) {
console.log('waiting for old state to be loaded...')
return Promise.resolve()
} else {
return storage.setItem(key, state)
}
},
reducer = defaultReducer,
storage = defaultStorage,
subscriber = store => handler => store.subscribe(handler)
@ -72,7 +78,22 @@ export default function createPersistedState ({
subscriber(store)((mutation, state) => {
try {
setState(key, reducer(state, paths), storage)
if (saveImmedeatelyActions.includes(mutation.type)) {
setState(key, reducer(state, paths), storage)
.then(success => {
if (typeof success !== 'undefined') {
if (mutation.type === 'setOption') {
store.dispatch('settingsSaved', { success })
}
}
}, error => {
if (mutation.type === 'setOption') {
store.dispatch('settingsSaved', { error })
}
})
} else {
console.warn(`Not saving to localStorage for: ${mutation.type}`)
}
} catch (e) {
console.log("Couldn't persist state:")
console.log(e)