a806d43f05
* upstream/develop: (60 commits) whoops whoops DM timeline: stream new statuses update-japanese-translation Add actual user search. incorporate most translation changes from MR 368 update french translation Always show dm panel. Add direct message tab. api service url remove deploy stage remove deploy stage updated and completed German translation On logout switch to public timeline. minor modification of Chinese translation update Chinese translation Add Chinese language Fix posting. Put oauth text into description. Display OAuth login on login form button. ...
104 lines
2.9 KiB
JavaScript
104 lines
2.9 KiB
JavaScript
import merge from 'lodash.merge'
|
|
import objectPath from 'object-path'
|
|
import localforage from 'localforage'
|
|
import { each } from 'lodash'
|
|
|
|
let loaded = false
|
|
|
|
const defaultReducer = (state, paths) => (
|
|
paths.length === 0 ? state : paths.reduce((substate, path) => {
|
|
objectPath.set(substate, path, objectPath.get(state, path))
|
|
return substate
|
|
}, {})
|
|
)
|
|
|
|
const saveImmedeatelyActions = [
|
|
'markNotificationsAsSeen',
|
|
'clearCurrentUser',
|
|
'setCurrentUser',
|
|
'setHighlight',
|
|
'setOption',
|
|
'setClientData',
|
|
'setToken'
|
|
]
|
|
|
|
const defaultStorage = (() => {
|
|
return localforage
|
|
})()
|
|
|
|
export default function createPersistedState ({
|
|
key = 'vuex-lz',
|
|
paths = [],
|
|
getState = (key, storage) => {
|
|
let value = storage.getItem(key)
|
|
return value
|
|
},
|
|
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)
|
|
} = {}) {
|
|
return getState(key, storage).then((savedState) => {
|
|
return store => {
|
|
try {
|
|
if (typeof savedState === 'object') {
|
|
// build user cache
|
|
const usersState = savedState.users || {}
|
|
usersState.usersObject = {}
|
|
const users = usersState.users || []
|
|
each(users, (user) => { usersState.usersObject[user.id] = user })
|
|
savedState.users = usersState
|
|
|
|
store.replaceState(
|
|
merge({}, store.state, savedState)
|
|
)
|
|
}
|
|
if (store.state.config.customTheme) {
|
|
// This is a hack to deal with async loading of config.json and themes
|
|
// See: style_setter.js, setPreset()
|
|
window.themeLoaded = true
|
|
store.dispatch('setOption', {
|
|
name: 'customTheme',
|
|
value: store.state.config.customTheme
|
|
})
|
|
}
|
|
if (store.state.oauth.token) {
|
|
store.dispatch('loginUser', store.state.oauth.token)
|
|
}
|
|
loaded = true
|
|
} catch (e) {
|
|
console.log("Couldn't load state")
|
|
console.error(e)
|
|
loaded = true
|
|
}
|
|
subscriber(store)((mutation, state) => {
|
|
try {
|
|
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 })
|
|
}
|
|
})
|
|
}
|
|
} catch (e) {
|
|
console.log("Couldn't persist state:")
|
|
console.log(e)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|