Skip to content

Commit 7a5a78b

Browse files
committed
perf: rAF-debounce getContents with visibilitychange fallback
1 parent 1718582 commit 7a5a78b

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
@@ -778,6 +778,20 @@
778778
};
779779
init();
780780
781+
const onVisibilityChange = () => {
782+
if (document.visibilityState === 'visible') {
783+
cancelAnimationFrame(contentsRAF);
784+
contentsRAF = null;
785+
getContents();
786+
const contents = get(artifactContents);
787+
if (contents && contents.length > 0 && !get(showControls) && !get(mobile)) {
788+
showArtifacts.set(true);
789+
showControls.set(true);
790+
}
791+
}
792+
};
793+
document.addEventListener('visibilitychange', onVisibilityChange);
794+
781795
return () => {
782796
try {
783797
if (chatIdProp && !$temporaryChatEnabled) {
@@ -787,6 +801,7 @@
787801
showControlsSubscribe();
788802
selectedFolderSubscribe();
789803
window.removeEventListener('message', onMessageHandler);
804+
document.removeEventListener('visibilitychange', onVisibilityChange);
790805
$socket?.off('events', chatEventHandler);
791806
audioQueueInstance?.destroy();
792807
audioQueue.set(null);
@@ -982,11 +997,11 @@
982997
983998
const onHistoryChange = (history) => {
984999
if (history) {
985-
clearTimeout(contentsRAF);
986-
contentsRAF = setTimeout(() => {
1000+
cancelAnimationFrame(contentsRAF);
1001+
contentsRAF = requestAnimationFrame(() => {
9871002
getContents();
9881003
contentsRAF = null;
989-
}, 0);
1004+
});
9901005
} else {
9911006
artifactContents.set([]);
9921007
}

0 commit comments

Comments
 (0)