Skip to content

Commit 8c5cbc3

Browse files
committed
perf: rAF-debounce getContents with visibilitychange fallback
1 parent 9dcb6e8 commit 8c5cbc3

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
@@ -833,17 +833,36 @@
833833
};
834834
init();
835835
836+
const onVisibilityChange = () => {
837+
if (document.visibilityState === 'visible') {
838+
cancelAnimationFrame(contentsRAF);
839+
contentsRAF = null;
840+
getContents();
841+
const contents = get(artifactContents);
842+
if (contents && contents.length > 0 && !get(showControls) && !get(mobile)) {
843+
showArtifacts.set(true);
844+
showControls.set(true);
845+
}
846+
}
847+
};
848+
document.addEventListener('visibilitychange', onVisibilityChange);
849+
836850
return () => {
837851
try {
838852
clearTimeout(saveControlsTimer);
839853
saveControls();
854+
cancelAnimationFrame(scrollRAF);
855+
cancelAnimationFrame(contentsRAF);
856+
scrollRAF = null;
857+
contentsRAF = null;
840858
if (chatIdProp && !$temporaryChatEnabled) {
841859
updateLastReadAt(chatIdProp);
842860
}
843861
pageSubscribe();
844862
showControlsSubscribe();
845863
selectedFolderSubscribe();
846864
window.removeEventListener('message', onMessageHandler);
865+
document.removeEventListener('visibilitychange', onVisibilityChange);
847866
$socket?.off('events', chatEventHandler);
848867
audioQueueInstance?.destroy();
849868
audioQueue.set(null);
@@ -1039,11 +1058,11 @@
10391058
10401059
const onHistoryChange = (history) => {
10411060
if (history) {
1042-
clearTimeout(contentsRAF);
1043-
contentsRAF = setTimeout(() => {
1061+
cancelAnimationFrame(contentsRAF);
1062+
contentsRAF = requestAnimationFrame(() => {
10441063
getContents();
10451064
contentsRAF = null;
1046-
}, 0);
1065+
});
10471066
} else {
10481067
artifactContents.set([]);
10491068
}

0 commit comments

Comments
 (0)