Skip to content

Commit 3c8bd97

Browse files
Algorithm5838github-actions[bot]
authored andcommitted
perf: rAF-debounce getContents with visibilitychange fallback
1 parent 18294b2 commit 3c8bd97

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
@@ -785,6 +785,20 @@
785785
};
786786
init();
787787
788+
const onVisibilityChange = () => {
789+
if (document.visibilityState === 'visible') {
790+
cancelAnimationFrame(contentsRAF);
791+
contentsRAF = null;
792+
getContents();
793+
const contents = get(artifactContents);
794+
if (contents && contents.length > 0 && !get(showControls) && !get(mobile)) {
795+
showArtifacts.set(true);
796+
showControls.set(true);
797+
}
798+
}
799+
};
800+
document.addEventListener('visibilitychange', onVisibilityChange);
801+
788802
return () => {
789803
try {
790804
if (chatIdProp && !$temporaryChatEnabled) {
@@ -794,6 +808,7 @@
794808
showControlsSubscribe();
795809
selectedFolderSubscribe();
796810
window.removeEventListener('message', onMessageHandler);
811+
document.removeEventListener('visibilitychange', onVisibilityChange);
797812
$socket?.off('events', chatEventHandler);
798813
audioQueueInstance?.destroy();
799814
audioQueue.set(null);
@@ -989,11 +1004,11 @@
9891004
9901005
const onHistoryChange = (history) => {
9911006
if (history) {
992-
clearTimeout(contentsRAF);
993-
contentsRAF = setTimeout(() => {
1007+
cancelAnimationFrame(contentsRAF);
1008+
contentsRAF = requestAnimationFrame(() => {
9941009
getContents();
9951010
contentsRAF = null;
996-
}, 0);
1011+
});
9971012
} else {
9981013
artifactContents.set([]);
9991014
}

0 commit comments

Comments
 (0)