From 7ce1e9415a3dd9b9df10c81e5d68c3369d353b7f Mon Sep 17 00:00:00 2001 From: Algorithm5838 <108630393+Algorithm5838@users.noreply.github.com> Date: Tue, 24 Mar 2026 14:12:37 +0300 Subject: [PATCH] perf: batch history reactive triggers to rAF in chatEventHandler (#22947) --- src/lib/components/chat/Chat.svelte | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index daa6375c4..69e5debfa 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -536,7 +536,13 @@ console.log('Unknown message type', data); } - history.messages[event.message_id] = message; + if (type === 'chat:message:delta' || type === 'message' || type === 'status') { + scheduleHistoryFlush(); + } else { + cancelAnimationFrame(historyRAF); + historyRAF = null; + history.messages[event.message_id] = message; + } } } else { // Non-active chat completion: queue stays in the global store. @@ -1279,6 +1285,15 @@ let scrollRAF = null; let contentsRAF = null; + let historyRAF = null; + const scheduleHistoryFlush = () => { + if (!historyRAF) { + historyRAF = requestAnimationFrame(() => { + historyRAF = null; + history = history; + }); + } + }; const scheduleScrollToBottom = () => { if (!scrollRAF) { scrollRAF = requestAnimationFrame(async () => {