Commit b8db0839 by Sixong.Zhu

update

parent 530971fc
...@@ -152,8 +152,12 @@ class ChatCacheDatabaseController { ...@@ -152,8 +152,12 @@ class ChatCacheDatabaseController {
public saveChatList(items: Chat[]) { public saveChatList(items: Chat[]) {
if (this.db) { if (this.db) {
const store = this.buildStore(this.chatListKey); const store = this.buildStore(this.chatListKey);
for (const item of items) { if (items && items.length) {
store.add(item, item.id); for (const item of items) {
store.add(item, item.id);
}
} else {
store.clear();
} }
} }
} }
...@@ -459,47 +463,6 @@ class ChatCacheDatabaseController { ...@@ -459,47 +463,6 @@ class ChatCacheDatabaseController {
}); });
}); });
} }
public syncChats(chats: number[]) {
return new Promise<void>((resolve) => {
if (this.db) {
this.getChatList().then((r) => {
const set = new Set<number>(chats);
let finished = 0;
let removing = [];
for (const item of r) {
if (!set.has(item.id)) {
removing.push(item.id);
}
}
for (const item of removing) {
this.removeChatAndMessages(item).finally(() => {
finished++;
if (finished === removing.length) {
resolve();
}
});
}
!removing.length && resolve();
});
}
});
}
private removeChatAndMessages(chat: number) {
this.setupChatMessageDatabase(chat).finally(() => {
const store = this.buildChatMessageStore(chat);
const r = store.clear();
r.onsuccess = () => {
indexedDB.deleteDatabase(this.buildChatMessageKey(chat));
};
});
return this.removeChatFromList(chat);
}
} }
export const dbController = new ChatCacheDatabaseController(); export const dbController = new ChatCacheDatabaseController();
...@@ -140,8 +140,6 @@ function clearAction(value: ChatType[]) { ...@@ -140,8 +140,6 @@ function clearAction(value: ChatType[]) {
return value; return value;
} }
let synced = false;
export default { export default {
namespaced: true, namespaced: true,
state: () => ({ state: () => ({
...@@ -436,8 +434,6 @@ export default { ...@@ -436,8 +434,6 @@ export default {
async [ChatStore.ACTION_GET_MY_CHAT_LIST]({ commit, dispatch }) { async [ChatStore.ACTION_GET_MY_CHAT_LIST]({ commit, dispatch }) {
commit(ChatStore.MUTATION_SAVE_MYSELF_ID); commit(ChatStore.MUTATION_SAVE_MYSELF_ID);
await dispatch(ChatStore.ACTION_SYNC_MY_CHAT_LIST);
if (loadingChatList) { if (loadingChatList) {
return new Promise<ChatType[]>((resolve) => return new Promise<ChatType[]>((resolve) =>
cachedLoadingChatListAction.push(resolve) cachedLoadingChatListAction.push(resolve)
...@@ -446,62 +442,6 @@ export default { ...@@ -446,62 +442,6 @@ export default {
loadingChatList = true; loadingChatList = true;
let cache = await dbController.getChatList();
cache.sort((x, y) => (x.last_msg_ts < y.last_msg_ts ? 1 : -1));
for (const item of cache) {
if (item.business_data && !item.detail_name) {
const d = JSON.parse(
item.business_data
) as BaseChatItemBusinessData;
if (d) {
if (d.detail_name) {
item.detail_name = d.detail_name;
}
if (!item.obj_id && d.obj_id) {
item.obj_id = d.obj_id;
}
if (!item.model_name && d.model_name) {
item.model_name = d.model_name;
}
}
}
}
if (cache && cache.length) {
const execute = () =>
new Promise<ChatType[]>((resolve, reject) => {
Chat.onReady(() => {
xim.fetchChatListAfter(0)
.then((r) => {
const list = filterActiveChats(
r.args[0] as RawChatItem[]
);
const items = list.map((i) =>
buildChatItem(i)
);
if (items && items.length) {
cache = dbController.mergeChatList(
cache,
items
);
}
commit(
ChatStore.MUTATION_SAVE_CHAT_LIST,
cache
);
dispatch(
ChatStore.ACTION_REBUILD_UNREAD_MESSAGE_COUNT
).finally(resolve);
})
.catch(reject);
});
}).finally(() => (loadingChatList = false));
return await execute().then((d) => clearAction(d));
}
const execute = () => const execute = () =>
new Promise<ChatType[]>((resolve, reject) => { new Promise<ChatType[]>((resolve, reject) => {
Chat.onReady(() => { Chat.onReady(() => {
...@@ -531,27 +471,6 @@ export default { ...@@ -531,27 +471,6 @@ export default {
return await execute().then((d) => clearAction(d)); return await execute().then((d) => clearAction(d));
}, },
async [ChatStore.ACTION_SYNC_MY_CHAT_LIST]() {
if (synced) {
return Promise.resolve();
}
return new Promise<void>((resolve) =>
Chat.onReady(() => {
synced = true;
xim.fetchChatList()
.then((data) => {
dbController
.syncChats(
(data.args[0] as RawChatItem[]).map(
(i) => i.id
)
)
.finally(resolve);
})
.catch(resolve);
})
);
},
async [ChatStore.ACTION_FORCE_RELOAD_CHAT_LIST]({ commit }) { async [ChatStore.ACTION_FORCE_RELOAD_CHAT_LIST]({ commit }) {
return new Promise<ChatType[]>((resolve) => { return new Promise<ChatType[]>((resolve) => {
Chat.onReady(() => { Chat.onReady(() => {
......
...@@ -252,9 +252,6 @@ export namespace ChatStore { ...@@ -252,9 +252,6 @@ export namespace ChatStore {
keyword?: string keyword?: string
) => Promise<ChatType[]>; ) => Promise<ChatType[]>;
export const ACTION_SYNC_MY_CHAT_LIST = "同步我的会话列表";
export type ACTION_SYNC_MY_CHAT_LIST = () => void;
export const ACTION_FORCE_RELOAD_CHAT_LIST = "重新获取我的会话列表"; export const ACTION_FORCE_RELOAD_CHAT_LIST = "重新获取我的会话列表";
export type ACTION_FORCE_RELOAD_CHAT_LIST = () => Promise<ChatType[]>; export type ACTION_FORCE_RELOAD_CHAT_LIST = () => Promise<ChatType[]>;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment