Skip to content

Commit 97899b3

Browse files
Algorithm5838github-actions[bot]
authored andcommitted
perf: rAF-debounce getContents with visibilitychange fallback
1 parent 156efb2 commit 97899b3

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

src/lib/components/chat/Chat.svelte

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,20 @@
806806
};
807807
init();
808808
809+
const onVisibilityChange = () => {
810+
if (document.visibilityState === 'visible') {
811+
cancelAnimationFrame(contentsRAF);
812+
contentsRAF = null;
813+
getContents();
814+
const contents = get(artifactContents);
815+
if (contents && contents.length > 0 && !get(showControls) && !get(mobile)) {
816+
showArtifacts.set(true);
817+
showControls.set(true);
818+
}
819+
}
820+
};
821+
document.addEventListener('visibilitychange', onVisibilityChange);
822+
809823
return () => {
810824
try {
811825
if (chatIdProp && !$temporaryChatEnabled) {
@@ -815,6 +829,7 @@
815829
showControlsSubscribe();
816830
selectedFolderSubscribe();
817831
window.removeEventListener('message', onMessageHandler);
832+
document.removeEventListener('visibilitychange', onVisibilityChange);
818833
$socket?.off('events', chatEventHandler);
819834
audioQueueInstance?.destroy();
820835
audioQueue.set(null);
@@ -1010,11 +1025,11 @@
10101025
10111026
const onHistoryChange = (history) => {
10121027
if (history) {
1013-
clearTimeout(contentsRAF);
1014-
contentsRAF = setTimeout(() => {
1028+
cancelAnimationFrame(contentsRAF);
1029+
contentsRAF = requestAnimationFrame(() => {
10151030
getContents();
10161031
contentsRAF = null;
1017-
}, 0);
1032+
});
10181033
} else {
10191034
artifactContents.set([]);
10201035
}

0 commit comments

Comments
 (0)