Skip to content

Commit 0ff4c97

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

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
@@ -854,17 +854,36 @@
854854
};
855855
init();
856856
857+
const onVisibilityChange = () => {
858+
if (document.visibilityState === 'visible') {
859+
cancelAnimationFrame(contentsRAF);
860+
contentsRAF = null;
861+
getContents();
862+
const contents = get(artifactContents);
863+
if (contents && contents.length > 0 && !get(showControls) && !get(mobile)) {
864+
showArtifacts.set(true);
865+
showControls.set(true);
866+
}
867+
}
868+
};
869+
document.addEventListener('visibilitychange', onVisibilityChange);
870+
857871
return () => {
858872
try {
859873
clearTimeout(saveControlsTimer);
860874
saveControls();
875+
cancelAnimationFrame(scrollRAF);
876+
cancelAnimationFrame(contentsRAF);
877+
scrollRAF = null;
878+
contentsRAF = null;
861879
if (chatIdProp && !$temporaryChatEnabled) {
862880
updateLastReadAt(chatIdProp);
863881
}
864882
pageSubscribe();
865883
showControlsSubscribe();
866884
selectedFolderSubscribe();
867885
window.removeEventListener('message', onMessageHandler);
886+
document.removeEventListener('visibilitychange', onVisibilityChange);
868887
$socket?.off('events', chatEventHandler);
869888
audioQueueInstance?.destroy();
870889
audioQueue.set(null);
@@ -1060,11 +1079,13 @@
10601079
10611080
const onHistoryChange = (history) => {
10621081
if (history) {
1063-
clearTimeout(contentsRAF);
1064-
contentsRAF = setTimeout(() => {
1082+
if (createMessagesList(history, history.currentId).some(m => m.role !== 'user' && m.done === false)) return;
1083+
1084+
cancelAnimationFrame(contentsRAF);
1085+
contentsRAF = requestAnimationFrame(() => {
10651086
getContents();
10661087
contentsRAF = null;
1067-
}, 0);
1088+
});
10681089
} else {
10691090
artifactContents.set([]);
10701091
}

0 commit comments

Comments
 (0)