2017-02-20 17:25:19 +00:00
|
|
|
import merge from 'lodash.merge'
|
|
|
|
import objectPath from 'object-path'
|
2017-03-07 07:43:48 +00:00
|
|
|
import localforage from 'localforage'
|
2018-09-07 15:17:17 +00:00
|
|
|
import { each } from 'lodash'
|
2017-02-20 17:25:19 +00:00
|
|
|
|
2017-04-09 14:30:21 +00:00
|
|
|
let loaded = false
|
|
|
|
|
2017-02-20 17:25:19 +00:00
|
|
|
const defaultReducer = (state, paths) => (
|
|
|
|
paths.length === 0 ? state : paths.reduce((substate, path) => {
|
|
|
|
objectPath.set(substate, path, objectPath.get(state, path))
|
|
|
|
return substate
|
|
|
|
}, {})
|
|
|
|
)
|
|
|
|
|
2018-09-07 15:17:17 +00:00
|
|
|
const saveImmedeatelyActions = [
|
|
|
|
'markNotificationsAsSeen',
|
|
|
|
'clearCurrentUser',
|
|
|
|
'setCurrentUser',
|
2018-09-09 18:51:40 +00:00
|
|
|
'setHighlight',
|
2018-10-26 13:16:23 +00:00
|
|
|
'setOption',
|
|
|
|
'setClientData',
|
|
|
|
'setToken'
|
2018-09-07 15:17:17 +00:00
|
|
|
]
|
|
|
|
|
2017-02-20 17:25:19 +00:00
|
|
|
const defaultStorage = (() => {
|
2017-03-07 07:43:48 +00:00
|
|
|
return localforage
|
2017-02-20 17:25:19 +00:00
|
|
|
})()
|
|
|
|
|
|
|
|
export default function createPersistedState ({
|
2017-02-23 18:11:22 +00:00
|
|
|
key = 'vuex-lz',
|
2017-02-20 17:25:19 +00:00
|
|
|
paths = [],
|
|
|
|
getState = (key, storage) => {
|
2017-02-20 17:54:09 +00:00
|
|
|
let value = storage.getItem(key)
|
2017-03-07 07:43:48 +00:00
|
|
|
return value
|
2017-02-20 17:25:19 +00:00
|
|
|
},
|
2018-09-07 15:17:17 +00:00
|
|
|
setState = (key, state, storage) => {
|
|
|
|
if (!loaded) {
|
|
|
|
console.log('waiting for old state to be loaded...')
|
|
|
|
return Promise.resolve()
|
|
|
|
} else {
|
|
|
|
return storage.setItem(key, state)
|
|
|
|
}
|
|
|
|
},
|
2017-02-20 17:25:19 +00:00
|
|
|
reducer = defaultReducer,
|
|
|
|
storage = defaultStorage,
|
|
|
|
subscriber = store => handler => store.subscribe(handler)
|
|
|
|
} = {}) {
|
2018-10-26 13:16:23 +00:00
|
|
|
return getState(key, storage).then((savedState) => {
|
|
|
|
return store => {
|
2017-04-09 20:15:35 +00:00
|
|
|
try {
|
2019-02-09 09:26:35 +00:00
|
|
|
if (savedState !== null && typeof savedState === 'object') {
|
2017-04-09 20:15:35 +00:00
|
|
|
// build user cache
|
|
|
|
const usersState = savedState.users || {}
|
|
|
|
usersState.usersObject = {}
|
|
|
|
const users = usersState.users || []
|
|
|
|
each(users, (user) => { usersState.usersObject[user.id] = user })
|
|
|
|
savedState.users = usersState
|
2017-03-08 16:59:12 +00:00
|
|
|
|
2017-04-09 20:15:35 +00:00
|
|
|
store.replaceState(
|
|
|
|
merge({}, store.state, savedState)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
loaded = true
|
|
|
|
} catch (e) {
|
|
|
|
console.log("Couldn't load state")
|
2018-10-02 18:43:58 +00:00
|
|
|
console.error(e)
|
2017-04-09 20:15:35 +00:00
|
|
|
loaded = true
|
2017-03-07 07:43:48 +00:00
|
|
|
}
|
2018-10-26 13:16:23 +00:00
|
|
|
subscriber(store)((mutation, state) => {
|
|
|
|
try {
|
|
|
|
if (saveImmedeatelyActions.includes(mutation.type)) {
|
|
|
|
setState(key, reducer(state, paths), storage)
|
|
|
|
.then(success => {
|
|
|
|
if (typeof success !== 'undefined') {
|
2019-02-19 17:38:49 +00:00
|
|
|
if (mutation.type === 'setOption' || mutation.type === 'setCurrentUser') {
|
2018-10-26 13:16:23 +00:00
|
|
|
store.dispatch('settingsSaved', { success })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, error => {
|
2019-02-19 17:38:49 +00:00
|
|
|
if (mutation.type === 'setOption' || mutation.type === 'setCurrentUser') {
|
2018-10-26 13:16:23 +00:00
|
|
|
store.dispatch('settingsSaved', { error })
|
2018-09-07 15:17:17 +00:00
|
|
|
}
|
2018-10-26 13:16:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.log("Couldn't persist state:")
|
|
|
|
console.log(e)
|
2018-09-07 15:17:17 +00:00
|
|
|
}
|
2018-10-26 13:16:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2017-02-20 17:25:19 +00:00
|
|
|
}
|