Skip to content

Commit ac5694e

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

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

src/lib/components/chat/Chat.svelte

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -817,17 +817,36 @@
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);
823837
saveControls();
838+
cancelAnimationFrame(scrollRAF);
839+
cancelAnimationFrame(contentsRAF);
840+
scrollRAF = null;
841+
contentsRAF = null;
824842
if (chatIdProp && !$temporaryChatEnabled) {
825843
updateLastReadAt(chatIdProp);
826844
}
827845
pageSubscribe();
828846
showControlsSubscribe();
829847
selectedFolderSubscribe();
830848
window.removeEventListener('message', onMessageHandler);
849+
document.removeEventListener('visibilitychange', onVisibilityChange);
831850
$socket?.off('events', chatEventHandler);
832851
audioQueueInstance?.destroy();
833852
audioQueue.set(null);
@@ -1023,11 +1042,11 @@
10231042
10241043
const onHistoryChange = (history) => {
10251044
if (history) {
1026-
clearTimeout(contentsRAF);
1027-
contentsRAF = setTimeout(() => {
1045+
cancelAnimationFrame(contentsRAF);
1046+
contentsRAF = requestAnimationFrame(() => {
10281047
getContents();
10291048
contentsRAF = null;
1030-
}, 0);
1049+
});
10311050
} else {
10321051
artifactContents.set([]);
10331052
}

0 commit comments

Comments
 (0)