2017-02-20 17:25:19 +00:00
|
|
|
import merge from 'lodash.merge'
|
2017-03-07 07:43:48 +00:00
|
|
|
import localforage from 'localforage'
|
2022-03-17 07:02:26 +00:00
|
|
|
import { each, get, set, cloneDeep } 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) => {
|
2020-06-11 16:44:45 +00:00
|
|
|
set(substate, path, get(state, path))
|
2017-02-20 17:25:19 +00:00
|
|
|
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',
|
2019-06-20 03:11:39 +00:00
|
|
|
'setToken',
|
|
|
|
'clearToken'
|
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) {
|
2022-11-15 15:47:32 +00:00
|
|
|
console.error("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)) {
|
2022-03-17 07:02:26 +00:00
|
|
|
setState(key, reducer(cloneDeep(state), paths), storage)
|
2018-10-26 13:16:23 +00:00
|
|
|
.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) {
|
2022-11-15 15:47:32 +00:00
|
|
|
console.error("Couldn't persist state:")
|
|
|
|
console.error(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
|
|
|
}
|