Skip to content

Commit f63ffdc

Browse files
Algorithm5838github-actions[bot]
authored andcommitted
perf: rAF-debounce getContents with visibilitychange fallback
1 parent fb6130b commit f63ffdc

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

src/lib/components/chat/Chat.svelte

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -883,17 +883,36 @@
883883
};
884884
init();
885885
886+
const onVisibilityChange = () => {
887+
if (document.visibilityState === 'visible') {
888+
cancelAnimationFrame(contentsRAF);
889+
contentsRAF = null;
890+
getContents();
891+
const contents = get(artifactContents);
892+
if (contents && contents.length > 0 && !get(showControls) && !get(mobile)) {
893+
showArtifacts.set(true);
894+
showControls.set(true);
895+
}
896+
}
897+
};
898+
document.addEventListener('visibilitychange', onVisibilityChange);
899+
886900
return () => {
887901
try {
888902
clearTimeout(saveControlsTimer);
889903
saveControls();
904+
cancelAnimationFrame(scrollRAF);
905+
cancelAnimationFrame(contentsRAF);
906+
scrollRAF = null;
907+
contentsRAF = null;
890908
if (chatIdProp && !$temporaryChatEnabled) {
891909
updateLastReadAt(chatIdProp);
892910
}
893911
pageSubscribe();
894912
showControlsSubscribe();
895913
selectedFolderSubscribe();
896914
window.removeEventListener('message', onMessageHandler);
915+
document.removeEventListener('visibilitychange', onVisibilityChange);
897916
$socket?.off('events', chatEventHandler);
898917
audioQueueInstance?.destroy();
899918
audioQueue.set(null);
@@ -1089,11 +1108,13 @@
10891108
10901109
const onHistoryChange = (history) => {
10911110
if (history) {
1092-
clearTimeout(contentsRAF);
1093-
contentsRAF = setTimeout(() => {
1111+
if (createMessagesList(history, history.currentId).some(m => m.role !== 'user' && m.done === false)) return;
1112+
1113+
cancelAnimationFrame(contentsRAF);
1114+
contentsRAF = requestAnimationFrame(() => {
10941115
getContents();
10951116
contentsRAF = null;
1096-
}, 0);
1117+
});
10971118
} else {
10981119
artifactContents.set([]);
10991120
}

0 commit comments

Comments
 (0)