Commit 27fae3ae by Sixong.Zhu

update

parent 8cc8f134
Showing with 64 additions and 17 deletions
......@@ -31,7 +31,7 @@ function uniqueMessages(
messages: NonNullable<ChatStore.STATE_CHAT_MSG_HISTORY>
) {
const arr = [...messages];
return unique(arr, function(item, all) {
return unique(arr, function (item, all) {
return all.findIndex((k) => k.id === item.id);
});
}
......@@ -327,7 +327,7 @@ export default {
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 } = {};
return (
state: ChatStoreState,
......@@ -404,26 +404,52 @@ export default {
.map((i) => Math.max(i.last_msg_ts, i.update_time))
.sort();
const last = ts[ts.length - 1];
await xim.fetchChatListAfter(last)!.then((r) => {
const list = filterActiveChats(r.args[0] as RawChatItem[]);
const execute = () =>
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));
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();
if (data == null) return;
const chatList = filterActiveChats(data.args[0] as RawChatItem[]);
const items = chatList.map((chat) => buildChatItem(chat));
const execute = () =>
new Promise<ChatType[]>((resolve) => {
Chat.onReady(() => {
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);
commit(ChatStore.MUTATION_SAVE_CHAT_LIST, {
list: items,
total: 9999,
});
return buildUnreadMessage(items);
resolve(buildUnreadMessage(items));
});
});
});
return await execute().then((d) => d);
},
async [ChatStore.ACTION_REBUILD_UNREAD_MESSAGE_COUNT]({ state }) {
let items = await dbController.getChatList();
......@@ -734,7 +760,7 @@ export default {
}
commit(
ChatStore.MUTATION_SAVE_CURRENT_CHAT_MEMBERS,
unique(newChatMembers, function(item, all) {
unique(newChatMembers, function (item, all) {
return all.findIndex((k) => k.eid === item.eid);
})
);
......
......@@ -22,10 +22,20 @@ class Chat {
private eventHub: Vue | null = null;
private keywords: string[] = [];
private ws = "";
private connectedActions: (() => void)[] = [];
private connected = false;
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) {
if (!option) {
throw new Error(`You must specify a chat option for chat service`);
......@@ -63,7 +73,12 @@ class Chat {
// 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() {
......@@ -123,11 +138,17 @@ class Chat {
private async initChatSdk(uri: string) {
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