Skip to content

Commit 6d71d6c

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

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
@@ -924,17 +924,36 @@
924924
};
925925
init();
926926
927+
const onVisibilityChange = () => {
928+
if (document.visibilityState === 'visible') {
929+
cancelAnimationFrame(contentsRAF);
930+
contentsRAF = null;
931+
getContents();
932+
const contents = get(artifactContents);
933+
if (contents && contents.length > 0 && !get(showControls) && !get(mobile)) {
934+
showArtifacts.set(true);
935+
showControls.set(true);
936+
}
937+
}
938+
};
939+
document.addEventListener('visibilitychange', onVisibilityChange);
940+
927941
return () => {
928942
try {
929943
clearTimeout(saveControlsTimer);
930944
saveControls();
945+
cancelAnimationFrame(scrollRAF);
946+
cancelAnimationFrame(contentsRAF);
947+
scrollRAF = null;
948+
contentsRAF = null;
931949
if (chatIdProp && !$temporaryChatEnabled) {
932950
updateLastReadAt(chatIdProp);
933951
}
934952
pageSubscribe();
935953
showControlsSubscribe();
936954
selectedFolderSubscribe();
937955
window.removeEventListener('message', onMessageHandler);
956+
document.removeEventListener('visibilitychange', onVisibilityChange);
938957
$socket?.off('events', chatEventHandler);
939958
dismissContextCompactionToast();
940959
audioQueueInstance?.destroy();
@@ -1131,11 +1150,13 @@
11311150
11321151
const onHistoryChange = (history) => {
11331152
if (history) {
1134-
clearTimeout(contentsRAF);
1135-
contentsRAF = setTimeout(() => {
1153+
if (createMessagesList(history, history.currentId).some(m => m.role !== 'user' && m.done === false)) return;
1154+
1155+
cancelAnimationFrame(contentsRAF);
1156+
contentsRAF = requestAnimationFrame(() => {
11361157
getContents();
11371158
contentsRAF = null;
1138-
}, 0);
1159+
});
11391160
} else {
11401161
artifactContents.set([]);
11411162
}

0 commit comments

Comments
 (0)