Skip to content

Commit 11bedc9

Browse files
committed
perf: rAF-debounce getContents with visibilitychange fallback
1 parent 4c5bf99 commit 11bedc9

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
@@ -817,6 +817,20 @@
817817
};
818818
init();
819819
820+
const onVisibilityChange = () => {
821+
if (document.visibilityState === 'visible') {
822+
cancelAnimationFrame(contentsRAF);
823+
contentsRAF = null;
824+
getContents();
825+
const contents = get(artifactContents);
826+
if (contents && contents.length > 0 && !get(showControls) && !get(mobile)) {
827+
showArtifacts.set(true);
828+
showControls.set(true);
829+
}
830+
}
831+
};
832+
document.addEventListener('visibilitychange', onVisibilityChange);
833+
820834
return () => {
821835
try {
822836
clearTimeout(saveControlsTimer);
@@ -828,6 +842,7 @@
828842
showControlsSubscribe();
829843
selectedFolderSubscribe();
830844
window.removeEventListener('message', onMessageHandler);
845+
document.removeEventListener('visibilitychange', onVisibilityChange);
831846
$socket?.off('events', chatEventHandler);
832847
audioQueueInstance?.destroy();
833848
audioQueue.set(null);
@@ -1023,11 +1038,11 @@
10231038
10241039
const onHistoryChange = (history) => {
10251040
if (history) {
1026-
clearTimeout(contentsRAF);
1027-
contentsRAF = setTimeout(() => {
1041+
cancelAnimationFrame(contentsRAF);
1042+
contentsRAF = requestAnimationFrame(() => {
10281043
getContents();
10291044
contentsRAF = null;
1030-
}, 0);
1045+
});
10311046
} else {
10321047
artifactContents.set([]);
10331048
}

0 commit comments

Comments
 (0)