Skip to content

Commit f2f7776

Browse files
committed
perf: debounce message sync in ResponseMessage during streaming
The JSON.stringify sync runs for every visible message on each token, not just the one streaming. Debounce to 50ms while streaming, sync immediately when done.
1 parent e346ea1 commit f2f7776

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

src/lib/components/chat/Messages/ResponseMessage.svelte

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,26 @@
120120
export let selectedModels = [];
121121
122122
let message: MessageType = JSON.parse(JSON.stringify(history.messages[messageId]));
123-
$: if (history.messages) {
123+
let messageSyncTimer: ReturnType<typeof setTimeout>;
124+
125+
function syncMessage() {
124126
if (JSON.stringify(message) !== JSON.stringify(history.messages[messageId])) {
125127
message = JSON.parse(JSON.stringify(history.messages[messageId]));
126128
}
127129
}
128130
131+
$: if (history.messages) {
132+
if (history.messages[messageId]?.done) {
133+
// Message done, sync now
134+
clearTimeout(messageSyncTimer);
135+
syncMessage();
136+
} else {
137+
// Debounce during streaming
138+
clearTimeout(messageSyncTimer);
139+
messageSyncTimer = setTimeout(syncMessage, 50);
140+
}
141+
}
142+
129143
export let siblings;
130144
131145
export let setInputText: Function = () => {};
@@ -589,6 +603,8 @@
589603
});
590604
591605
onDestroy(() => {
606+
clearTimeout(messageSyncTimer);
607+
592608
if (buttonsContainerElement) {
593609
buttonsContainerElement.removeEventListener('wheel', buttonsWheelHandler);
594610
}

0 commit comments

Comments
 (0)