Commit 06f2486e by zhousil
parents c4a61c93 24ca3954
...@@ -16,7 +16,7 @@ class ChatCacheDatabaseController { ...@@ -16,7 +16,7 @@ class ChatCacheDatabaseController {
private setuping = false; private setuping = false;
private setupError = false; private setupError = false;
public readonly historyOrderModelName = 'order_info'; public readonly historyOrderModelName = "order_info";
private waitSetupCompleted() { private waitSetupCompleted() {
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
...@@ -102,17 +102,17 @@ class ChatCacheDatabaseController { ...@@ -102,17 +102,17 @@ class ChatCacheDatabaseController {
} }
setTimeout(() => resolve(), 200); setTimeout(() => resolve(), 200);
}; };
r.onsuccess = function(e) { r.onsuccess = function (e) {
const db = (e.target as any).result; const db = (e.target as any).result;
that.messageDatabases.set(k, db); that.messageDatabases.set(k, db);
setupDb(); setupDb();
}; };
r.onupgradeneeded = function(e) { r.onupgradeneeded = function (e) {
const db = (e.target as any).result; const db = (e.target as any).result;
that.messageDatabases.set(k, db); that.messageDatabases.set(k, db);
setupDb(); setupDb();
}; };
r.onerror = function(e) { r.onerror = function (e) {
console.log( console.log(
`chat message index database init failed, ${e}` `chat message index database init failed, ${e}`
); );
...@@ -459,6 +459,47 @@ class ChatCacheDatabaseController { ...@@ -459,6 +459,47 @@ 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();
...@@ -32,7 +32,7 @@ function uniqueMessages( ...@@ -32,7 +32,7 @@ function uniqueMessages(
messages: NonNullable<ChatStore.STATE_CHAT_MSG_HISTORY> messages: NonNullable<ChatStore.STATE_CHAT_MSG_HISTORY>
) { ) {
const arr = [...messages]; const arr = [...messages];
return unique(arr, function(item, all) { return unique(arr, function (item, all) {
return all.findIndex((k) => k.id === item.id); return all.findIndex((k) => k.id === item.id);
}); });
} }
...@@ -138,6 +138,8 @@ function clearAction(value: ChatType[]) { ...@@ -138,6 +138,8 @@ function clearAction(value: ChatType[]) {
return value; return value;
} }
let synced = false;
export default { export default {
namespaced: true, namespaced: true,
state: () => ({ state: () => ({
...@@ -366,7 +368,7 @@ export default { ...@@ -366,7 +368,7 @@ export default {
state[ChatStore.STATE_CHAT_SENDING_MESSAGES] = [...current]; state[ChatStore.STATE_CHAT_SENDING_MESSAGES] = [...current];
} }
}, },
[ChatStore.MUTATION_SAVE_CURRENT_CHAT_INPUTING]: (function() { [ChatStore.MUTATION_SAVE_CURRENT_CHAT_INPUTING]: (function () {
const setTimeoutId: { [key: string]: number } = {}; const setTimeoutId: { [key: string]: number } = {};
return ( return (
state: ChatStoreState, state: ChatStoreState,
...@@ -426,6 +428,8 @@ export default { ...@@ -426,6 +428,8 @@ 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)
...@@ -520,6 +524,27 @@ export default { ...@@ -520,6 +524,27 @@ 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(() => {
...@@ -940,7 +965,7 @@ export default { ...@@ -940,7 +965,7 @@ export default {
} }
commit( commit(
ChatStore.MUTATION_SAVE_CURRENT_CHAT_MEMBERS, ChatStore.MUTATION_SAVE_CURRENT_CHAT_MEMBERS,
unique(newChatMembers, function(item, all) { unique(newChatMembers, function (item, all) {
return all.findIndex((k) => k.eid === item.eid); return all.findIndex((k) => k.eid === item.eid);
}) })
); );
......
...@@ -247,6 +247,9 @@ export namespace ChatStore { ...@@ -247,6 +247,9 @@ 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