Skip to content

Commit 4816a2e

Browse files
committed
perf: rAF-debounce getContents with visibilitychange fallback
1 parent 0218ddf commit 4816a2e

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
@@ -805,6 +805,20 @@
805805
};
806806
init();
807807
808+
const onVisibilityChange = () => {
809+
if (document.visibilityState === 'visible') {
810+
cancelAnimationFrame(contentsRAF);
811+
contentsRAF = null;
812+
getContents();
813+
const contents = get(artifactContents);
814+
if (contents && contents.length > 0 && !get(showControls) && !get(mobile)) {
815+
showArtifacts.set(true);
816+
showControls.set(true);
817+
}
818+
}
819+
};
820+
document.addEventListener('visibilitychange', onVisibilityChange);
821+
808822
return () => {
809823
try {
810824
if (chatIdProp && !$temporaryChatEnabled) {
@@ -814,6 +828,7 @@
814828
showControlsSubscribe();
815829
selectedFolderSubscribe();
816830
window.removeEventListener('message', onMessageHandler);
831+
document.removeEventListener('visibilitychange', onVisibilityChange);
817832
$socket?.off('events', chatEventHandler);
818833
audioQueueInstance?.destroy();
819834
audioQueue.set(null);
@@ -1009,11 +1024,11 @@
10091024
10101025
const onHistoryChange = (history) => {
10111026
if (history) {
1012-
clearTimeout(contentsRAF);
1013-
contentsRAF = setTimeout(() => {
1027+
cancelAnimationFrame(contentsRAF);
1028+
contentsRAF = requestAnimationFrame(() => {
10141029
getContents();
10151030
contentsRAF = null;
1016-
}, 0);
1031+
});
10171032
} else {
10181033
artifactContents.set([]);
10191034
}

0 commit comments

Comments
 (0)