Skip to content

Commit

Permalink
Throttle thread update frequency to prevent app freeze
Browse files Browse the repository at this point in the history
  • Loading branch information
tanchekwei authored and sunner committed Aug 2, 2023
1 parent c329e14 commit 22a7da2
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import messagesPersist from "./messagesPersist";
import promptsPersist from "./promptsPersist";
import { getMatomo } from "@/composables/matomo";

let isThrottle = false;
let isThrottleMessage = false;
let isThrottleThreadMessage = false;
let messageBuffer = [];
let threadMessageBuffer = [];
// 初始化 VuexPersist 实例
const vuexPersist = new VuexPersist({
key: "chatall-app", // 用于存储的键名,可以根据你的应用更改
Expand Down Expand Up @@ -196,16 +198,21 @@ export default createStore({
};
});
messageBuffer = [];
isThrottle = false;
},
updateThreadMessage(state, { indexes, message }) {
const { chatIndex, messageIndex, threadIndex } = indexes;
const i = chatIndex == -1 ? state.currentChatIndex : chatIndex;
const chat = state.chats[i];
chat.threads[threadIndex].messages[messageIndex] = {
...chat.threads[threadIndex].messages[messageIndex],
...message,
};
isThrottleMessage = false;
},
updateThreadMessage(state) {
threadMessageBuffer.forEach((update) => {
const { indexes, message } = update;
const { chatIndex, messageIndex, threadIndex } = indexes;
const i = chatIndex == -1 ? state.currentChatIndex : chatIndex;
const chat = state.chats[i];
chat.threads[threadIndex].messages[messageIndex] = {
...chat.threads[threadIndex].messages[messageIndex],
...message,
};
});
threadMessageBuffer = [];
isThrottleThreadMessage = false;
},
setMessages(state, messages) {
const currentChat = state.chats[state.currentChatIndex];
Expand Down Expand Up @@ -326,7 +333,7 @@ export default createStore({
},
actions: {
sendPrompt({ commit, state, dispatch }, { prompt, bots, promptIndex }) {
isThrottle = false;
isThrottleMessage = false;
const currentChat = state.chats[state.currentChatIndex];
if (promptIndex === undefined) {
// if promptIndex not found, not resend, push to messages array
Expand Down Expand Up @@ -378,6 +385,7 @@ export default createStore({
{ commit, state, dispatch },
{ prompt, bot, responseIndex, threadIndex, promptIndex },
) {
isThrottleThreadMessage = false;
const currentChat = state.chats[state.currentChatIndex];
let thread;
if (threadIndex !== undefined) {
Expand Down Expand Up @@ -452,8 +460,8 @@ export default createStore({
},
updateMessage({ commit, state }, { indexes, message: values }) {
messageBuffer.push({ indexes, message: values });
if (!isThrottle) {
isThrottle = true;
if (!isThrottleMessage) {
isThrottleMessage = true;
setTimeout(() => {
commit("updateMessage");
commit("incrementUpdateCounter");
Expand All @@ -473,11 +481,14 @@ export default createStore({
}
},
updateThreadMessage({ commit, state }, { indexes, message: values }) {
commit("updateThreadMessage", { indexes, message: values });

// workaround for notifing the message window to scroll to bottom
commit("incrementUpdateCounter");

threadMessageBuffer.push({ indexes, message: values });
if (!isThrottleThreadMessage) {
isThrottleThreadMessage = true;
setTimeout(() => {
commit("updateThreadMessage");
commit("incrementUpdateCounter");
}, 200); // save every 0.2 seconds
}
if (values.done) {
const i =
indexes.chatIndex == -1 ? state.currentChatIndex : indexes.chatIndex;
Expand Down

1 comment on commit 22a7da2

@vercel
Copy link

@vercel vercel bot commented on 22a7da2 Aug 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

chatall – ./

chatall-llm.vercel.app
chatall-git-main-sunner.vercel.app
chatall-sunner.vercel.app

Please sign in to comment.