Skip to content

Commit b419257

Browse files
Algorithm5838github-actions[bot]
authored andcommitted
perf: rAF-debounce getContents with visibilitychange fallback
1 parent 123ff97 commit b419257

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
@@ -779,6 +779,20 @@
779779
};
780780
init();
781781
782+
const onVisibilityChange = () => {
783+
if (document.visibilityState === 'visible') {
784+
cancelAnimationFrame(contentsRAF);
785+
contentsRAF = null;
786+
getContents();
787+
const contents = get(artifactContents);
788+
if (contents && contents.length > 0 && !get(showControls) && !get(mobile)) {
789+
showArtifacts.set(true);
790+
showControls.set(true);
791+
}
792+
}
793+
};
794+
document.addEventListener('visibilitychange', onVisibilityChange);
795+
782796
return () => {
783797
try {
784798
if (chatIdProp && !$temporaryChatEnabled) {
@@ -788,6 +802,7 @@
788802
showControlsSubscribe();
789803
selectedFolderSubscribe();
790804
window.removeEventListener('message', onMessageHandler);
805+
document.removeEventListener('visibilitychange', onVisibilityChange);
791806
$socket?.off('events', chatEventHandler);
792807
audioQueueInstance?.destroy();
793808
audioQueue.set(null);
@@ -983,11 +998,11 @@
983998
984999
const onHistoryChange = (history) => {
9851000
if (history) {
986-
clearTimeout(contentsRAF);
987-
contentsRAF = setTimeout(() => {
1001+
cancelAnimationFrame(contentsRAF);
1002+
contentsRAF = requestAnimationFrame(() => {
9881003
getContents();
9891004
contentsRAF = null;
990-
}, 0);
1005+
});
9911006
} else {
9921007
artifactContents.set([]);
9931008
}

0 commit comments

Comments
 (0)