Skip to content

Commit 4035d8b

Browse files
Algorithm5838github-actions[bot]
authored andcommitted
perf: rAF-debounce getContents with visibilitychange fallback
1 parent 395e18e commit 4035d8b

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
@@ -807,6 +807,20 @@
807807
};
808808
init();
809809
810+
const onVisibilityChange = () => {
811+
if (document.visibilityState === 'visible') {
812+
cancelAnimationFrame(contentsRAF);
813+
contentsRAF = null;
814+
getContents();
815+
const contents = get(artifactContents);
816+
if (contents && contents.length > 0 && !get(showControls) && !get(mobile)) {
817+
showArtifacts.set(true);
818+
showControls.set(true);
819+
}
820+
}
821+
};
822+
document.addEventListener('visibilitychange', onVisibilityChange);
823+
810824
return () => {
811825
try {
812826
if (chatIdProp && !$temporaryChatEnabled) {
@@ -816,6 +830,7 @@
816830
showControlsSubscribe();
817831
selectedFolderSubscribe();
818832
window.removeEventListener('message', onMessageHandler);
833+
document.removeEventListener('visibilitychange', onVisibilityChange);
819834
$socket?.off('events', chatEventHandler);
820835
audioQueueInstance?.destroy();
821836
audioQueue.set(null);
@@ -1011,11 +1026,11 @@
10111026
10121027
const onHistoryChange = (history) => {
10131028
if (history) {
1014-
clearTimeout(contentsRAF);
1015-
contentsRAF = setTimeout(() => {
1029+
cancelAnimationFrame(contentsRAF);
1030+
contentsRAF = requestAnimationFrame(() => {
10161031
getContents();
10171032
contentsRAF = null;
1018-
}, 0);
1033+
});
10191034
} else {
10201035
artifactContents.set([]);
10211036
}

0 commit comments

Comments
 (0)