forked from AkkomaGang/akkoma-fe
Use cache to quickly access users.
This commit is contained in:
parent
bde1241843
commit
480a1ba253
2 changed files with 14 additions and 5 deletions
|
@ -1,7 +1,7 @@
|
||||||
import merge from 'lodash.merge'
|
import merge from 'lodash.merge'
|
||||||
import objectPath from 'object-path'
|
import objectPath from 'object-path'
|
||||||
import localforage from 'localforage'
|
import localforage from 'localforage'
|
||||||
import { throttle } from 'lodash'
|
import { throttle, each } from 'lodash'
|
||||||
|
|
||||||
const defaultReducer = (state, paths) => (
|
const defaultReducer = (state, paths) => (
|
||||||
paths.length === 0 ? state : paths.reduce((substate, path) => {
|
paths.length === 0 ? state : paths.reduce((substate, path) => {
|
||||||
|
@ -33,6 +33,13 @@ export default function createPersistedState ({
|
||||||
return store => {
|
return store => {
|
||||||
getState(key, storage).then((savedState) => {
|
getState(key, storage).then((savedState) => {
|
||||||
if (typeof savedState === 'object') {
|
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(
|
store.replaceState(
|
||||||
merge({}, store.state, savedState)
|
merge({}, store.state, savedState)
|
||||||
)
|
)
|
||||||
|
|
|
@ -3,9 +3,9 @@ import { compact, map, each, find, merge } from 'lodash'
|
||||||
import { set } from 'vue'
|
import { set } from 'vue'
|
||||||
|
|
||||||
// TODO: Unify with mergeOrAdd in statuses.js
|
// TODO: Unify with mergeOrAdd in statuses.js
|
||||||
export const mergeOrAdd = (arr, item) => {
|
export const mergeOrAdd = (arr, obj, item) => {
|
||||||
if (!item) { return false }
|
if (!item) { return false }
|
||||||
const oldItem = find(arr, {id: item.id})
|
const oldItem = obj[item.id]
|
||||||
if (oldItem) {
|
if (oldItem) {
|
||||||
// We already have this, so only merge the new info.
|
// We already have this, so only merge the new info.
|
||||||
merge(oldItem, item)
|
merge(oldItem, item)
|
||||||
|
@ -13,6 +13,7 @@ export const mergeOrAdd = (arr, item) => {
|
||||||
} else {
|
} else {
|
||||||
// This is a new item, prepare it
|
// This is a new item, prepare it
|
||||||
arr.push(item)
|
arr.push(item)
|
||||||
|
obj[item.id] = item
|
||||||
return {item, new: true}
|
return {item, new: true}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,7 +33,7 @@ export const mutations = {
|
||||||
state.loggingIn = false
|
state.loggingIn = false
|
||||||
},
|
},
|
||||||
addNewUsers (state, users) {
|
addNewUsers (state, users) {
|
||||||
each(users, (user) => mergeOrAdd(state.users, user))
|
each(users, (user) => mergeOrAdd(state.users, state.usersObject, user))
|
||||||
},
|
},
|
||||||
setUserForStatus (state, status) {
|
setUserForStatus (state, status) {
|
||||||
status.user = find(state.users, status.user)
|
status.user = find(state.users, status.user)
|
||||||
|
@ -42,7 +43,8 @@ export const mutations = {
|
||||||
export const defaultState = {
|
export const defaultState = {
|
||||||
currentUser: false,
|
currentUser: false,
|
||||||
loggingIn: false,
|
loggingIn: false,
|
||||||
users: []
|
users: [],
|
||||||
|
usersObject: {}
|
||||||
}
|
}
|
||||||
|
|
||||||
const users = {
|
const users = {
|
||||||
|
|
Loading…
Reference in a new issue