Commit 27fae3ae by Sixong.Zhu

update

parent 8cc8f134
Showing with 64 additions and 17 deletions
...@@ -31,7 +31,7 @@ function uniqueMessages( ...@@ -31,7 +31,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);
}); });
} }
...@@ -327,7 +327,7 @@ export default { ...@@ -327,7 +327,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,
...@@ -404,26 +404,52 @@ export default { ...@@ -404,26 +404,52 @@ export default {
.map((i) => Math.max(i.last_msg_ts, i.update_time)) .map((i) => Math.max(i.last_msg_ts, i.update_time))
.sort(); .sort();
const last = ts[ts.length - 1]; const last = ts[ts.length - 1];
await xim.fetchChatListAfter(last)!.then((r) => { const execute = () =>
const list = filterActiveChats(r.args[0] as RawChatItem[]); new Promise<ChatType[]>((resolve) => {
Chat.onReady(() => {
xim.fetchChatListAfter(last)!.then((r) => {
const list = filterActiveChats(
r.args[0] as RawChatItem[]
);
const items = list.map((i) => buildChatItem(i)); const items = list.map((i) => buildChatItem(i));
if (items && items.length) { if (items && items.length) {
cache = dbController.mergeChatList(cache, items); cache = dbController.mergeChatList(
cache,
items
);
} }
resolve(buildUnreadMessage(cache));
});
});
}); });
return buildUnreadMessage(cache);
return await execute().then((d) => d);
} }
const data = await xim.fetchChatList(); const execute = () =>
if (data == null) return; new Promise<ChatType[]>((resolve) => {
const chatList = filterActiveChats(data.args[0] as RawChatItem[]); Chat.onReady(() => {
const items = chatList.map((chat) => buildChatItem(chat)); xim.fetchChatList().then((data) => {
if (!data) {
return resolve([]);
}
const chatList = filterActiveChats(
data.args[0] as RawChatItem[]
);
const items = chatList.map((chat) =>
buildChatItem(chat)
);
dbController.saveChatList(items); dbController.saveChatList(items);
commit(ChatStore.MUTATION_SAVE_CHAT_LIST, { commit(ChatStore.MUTATION_SAVE_CHAT_LIST, {
list: items, list: items,
total: 9999, total: 9999,
}); });
return buildUnreadMessage(items); resolve(buildUnreadMessage(items));
});
});
});
return await execute().then((d) => d);
}, },
async [ChatStore.ACTION_REBUILD_UNREAD_MESSAGE_COUNT]({ state }) { async [ChatStore.ACTION_REBUILD_UNREAD_MESSAGE_COUNT]({ state }) {
let items = await dbController.getChatList(); let items = await dbController.getChatList();
...@@ -734,7 +760,7 @@ export default { ...@@ -734,7 +760,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);
}) })
); );
......
...@@ -22,10 +22,20 @@ class Chat { ...@@ -22,10 +22,20 @@ class Chat {
private eventHub: Vue | null = null; private eventHub: Vue | null = null;
private keywords: string[] = []; private keywords: string[] = [];
private ws = ""; private ws = "";
private connectedActions: (() => void)[] = [];
private connected = false;
private userMapping: { [key: string]: { name: string; avatar: string } } = private userMapping: { [key: string]: { name: string; avatar: string } } =
{}; {};
public onReady(action: () => void) {
if (this.connected) {
action();
} else {
this.connectedActions.push(action);
}
}
public async setup(option: ChatOption) { public async setup(option: ChatOption) {
if (!option) { if (!option) {
throw new Error(`You must specify a chat option for chat service`); throw new Error(`You must specify a chat option for chat service`);
...@@ -63,7 +73,12 @@ class Chat { ...@@ -63,7 +73,12 @@ class Chat {
// this.keywords = ["社保"]; // this.keywords = ["社保"];
return this.initChatSdk((this.ws = option.webSocketUri)); return this.initChatSdk((this.ws = option.webSocketUri)).finally(() => {
this.connected = true;
for (const item of this.connectedActions) {
item();
}
});
} }
private setupIndexDb() { private setupIndexDb() {
...@@ -123,11 +138,17 @@ class Chat { ...@@ -123,11 +138,17 @@ class Chat {
private async initChatSdk(uri: string) { private async initChatSdk(uri: string) {
if (xim.isConnected()) { if (xim.isConnected()) {
return uri; return Promise.resolve(uri);
}
return new Promise((resolve) => {
xim.open(uri, this.token).finally(() => {
this.registerXimEvent();
if (xim.isConnected()) {
resolve();
} else {
setTimeout(() => resolve(), 2000);
} }
return new Promise((resolve: (p?: unknown) => void) => { });
xim.open(uri, this.token);
this.registerXimEvent(resolve);
}); });
} }
......
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