perf(ckchat): 优化getChatSessions缓存机制避免无限循环
添加缓存机制来避免getChatSessions方法的重复计算,通过检查kfSelected和chatSessions的变化来决定是否重新计算缓存。同时在相关操作后主动清除缓存以确保数据一致性。
This commit is contained in:
@@ -7,7 +7,12 @@ import {
|
|||||||
KfUserListData,
|
KfUserListData,
|
||||||
ContactGroupByLabel,
|
ContactGroupByLabel,
|
||||||
} from "@/pages/pc/ckbox/data";
|
} from "@/pages/pc/ckbox/data";
|
||||||
import { kfUserService, weChatGroupService, contractService } from "@/utils/db";
|
import {
|
||||||
|
kfUserService,
|
||||||
|
weChatGroupService,
|
||||||
|
contractService,
|
||||||
|
messageListService,
|
||||||
|
} from "@/utils/db";
|
||||||
export const useCkChatStore = createPersistStore<CkChatState>(
|
export const useCkChatStore = createPersistStore<CkChatState>(
|
||||||
set => ({
|
set => ({
|
||||||
userInfo: null,
|
userInfo: null,
|
||||||
@@ -28,6 +33,15 @@ export const useCkChatStore = createPersistStore<CkChatState>(
|
|||||||
},
|
},
|
||||||
asyncKfSelected: (data: number) => {
|
asyncKfSelected: (data: number) => {
|
||||||
set({ kfSelected: data });
|
set({ kfSelected: data });
|
||||||
|
// 清除getChatSessions缓存
|
||||||
|
const state = useCkChatStore.getState();
|
||||||
|
if (
|
||||||
|
state.getChatSessions &&
|
||||||
|
typeof state.getChatSessions === "function"
|
||||||
|
) {
|
||||||
|
// 触发缓存重新计算
|
||||||
|
state.getChatSessions();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
// 异步设置会话列表
|
// 异步设置会话列表
|
||||||
asyncNewContractList: (data: ContactGroupByLabel[]) => {
|
asyncNewContractList: (data: ContactGroupByLabel[]) => {
|
||||||
@@ -40,6 +54,15 @@ export const useCkChatStore = createPersistStore<CkChatState>(
|
|||||||
// 异步设置会话列表
|
// 异步设置会话列表
|
||||||
asyncChatSessions: data => {
|
asyncChatSessions: data => {
|
||||||
set({ chatSessions: data });
|
set({ chatSessions: data });
|
||||||
|
// 清除getChatSessions缓存
|
||||||
|
const state = useCkChatStore.getState();
|
||||||
|
if (
|
||||||
|
state.getChatSessions &&
|
||||||
|
typeof state.getChatSessions === "function"
|
||||||
|
) {
|
||||||
|
// 触发缓存重新计算
|
||||||
|
state.getChatSessions();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
// 异步设置联系人列表
|
// 异步设置联系人列表
|
||||||
asyncContractList: async (data: ContractData[]) => {
|
asyncContractList: async (data: ContractData[]) => {
|
||||||
@@ -77,17 +100,35 @@ export const useCkChatStore = createPersistStore<CkChatState>(
|
|||||||
clearkfUserList: () => {
|
clearkfUserList: () => {
|
||||||
set({ kfUserList: [] });
|
set({ kfUserList: [] });
|
||||||
},
|
},
|
||||||
// 获取聊天会话
|
// 获取聊天会话 - 使用缓存避免无限循环
|
||||||
getChatSessions: () => {
|
getChatSessions: (() => {
|
||||||
const state = useCkChatStore.getState();
|
let cachedResult: any = null;
|
||||||
if (state.kfSelected != 0) {
|
let lastKfSelected: number | null = null;
|
||||||
return state.chatSessions.filter(
|
let lastChatSessionsLength: number = 0;
|
||||||
item => item.wechatAccountId === state.kfSelected,
|
|
||||||
);
|
return () => {
|
||||||
} else {
|
const state = useCkChatStore.getState();
|
||||||
return state.chatSessions;
|
|
||||||
}
|
// 检查是否需要重新计算缓存
|
||||||
},
|
const shouldRecalculate =
|
||||||
|
cachedResult === null ||
|
||||||
|
lastKfSelected !== state.kfSelected ||
|
||||||
|
lastChatSessionsLength !== state.chatSessions.length;
|
||||||
|
|
||||||
|
if (shouldRecalculate) {
|
||||||
|
const filteredSessions = state.chatSessions.filter(
|
||||||
|
item => item.wechatAccountId === state.kfSelected,
|
||||||
|
);
|
||||||
|
|
||||||
|
cachedResult =
|
||||||
|
state.kfSelected !== 0 ? filteredSessions : state.chatSessions;
|
||||||
|
lastKfSelected = state.kfSelected;
|
||||||
|
lastChatSessionsLength = state.chatSessions.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
return cachedResult;
|
||||||
|
};
|
||||||
|
})(),
|
||||||
// 添加聊天会话
|
// 添加聊天会话
|
||||||
addChatSession: (session: ContractData | weChatGroup) => {
|
addChatSession: (session: ContractData | weChatGroup) => {
|
||||||
set(state => {
|
set(state => {
|
||||||
@@ -100,6 +141,15 @@ export const useCkChatStore = createPersistStore<CkChatState>(
|
|||||||
: [...state.chatSessions, session as ContractData | weChatGroup],
|
: [...state.chatSessions, session as ContractData | weChatGroup],
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
// 清除getChatSessions缓存
|
||||||
|
const state = useCkChatStore.getState();
|
||||||
|
if (
|
||||||
|
state.getChatSessions &&
|
||||||
|
typeof state.getChatSessions === "function"
|
||||||
|
) {
|
||||||
|
// 触发缓存重新计算
|
||||||
|
state.getChatSessions();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
// 更新聊天会话
|
// 更新聊天会话
|
||||||
updateChatSession: (session: ContractData | weChatGroup) => {
|
updateChatSession: (session: ContractData | weChatGroup) => {
|
||||||
@@ -108,12 +158,30 @@ export const useCkChatStore = createPersistStore<CkChatState>(
|
|||||||
item.id === session.id ? session : item,
|
item.id === session.id ? session : item,
|
||||||
),
|
),
|
||||||
}));
|
}));
|
||||||
|
// 清除getChatSessions缓存
|
||||||
|
const state = useCkChatStore.getState();
|
||||||
|
if (
|
||||||
|
state.getChatSessions &&
|
||||||
|
typeof state.getChatSessions === "function"
|
||||||
|
) {
|
||||||
|
// 触发缓存重新计算
|
||||||
|
state.getChatSessions();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
// 删除聊天会话
|
// 删除聊天会话
|
||||||
deleteChatSession: (sessionId: string) => {
|
deleteChatSession: (sessionId: string) => {
|
||||||
set(state => ({
|
set(state => ({
|
||||||
chatSessions: state.chatSessions.filter(item => item.id !== sessionId),
|
chatSessions: state.chatSessions.filter(item => item.id !== sessionId),
|
||||||
}));
|
}));
|
||||||
|
// 清除getChatSessions缓存
|
||||||
|
const state = useCkChatStore.getState();
|
||||||
|
if (
|
||||||
|
state.getChatSessions &&
|
||||||
|
typeof state.getChatSessions === "function"
|
||||||
|
) {
|
||||||
|
// 触发缓存重新计算
|
||||||
|
state.getChatSessions();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
// 设置用户信息
|
// 设置用户信息
|
||||||
setUserInfo: (userInfo: CkUserInfo) => {
|
setUserInfo: (userInfo: CkUserInfo) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user