Skip to content

Commit e6b8fa3

Browse files
committed
fix: throttle markdown lexer during streaming to prevent render backlog
1 parent d0bf015 commit e6b8fa3

1 file changed

Lines changed: 31 additions & 6 deletions

File tree

src/lib/components/chat/Messages/Markdown.svelte

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<script>
2+
import { onDestroy } from 'svelte';
23
import { marked } from 'marked';
34
import { replaceTokens, processResponseContent } from '$lib/utils';
45
import { user } from '$lib/stores';
@@ -34,6 +35,7 @@
3435
export let onTaskClick = () => {};
3536
3637
let tokens = [];
38+
let rafId = null;
3739
3840
const options = {
3941
throwOnError: false,
@@ -53,13 +55,36 @@
5355
]
5456
});
5557
56-
$: (async () => {
57-
if (content) {
58-
tokens = marked.lexer(
59-
replaceTokens(processResponseContent(content), model?.name, $user?.name)
60-
);
58+
const lexContent = () => {
59+
tokens = marked.lexer(
60+
replaceTokens(processResponseContent(content), model?.name, $user?.name)
61+
);
62+
};
63+
64+
$: if (content) {
65+
if (done) {
66+
// When done, lex immediately to ensure final render is accurate
67+
if (rafId) {
68+
cancelAnimationFrame(rafId);
69+
rafId = null;
70+
}
71+
lexContent();
72+
} else {
73+
// While streaming, throttle to once per animation frame
74+
if (!rafId) {
75+
rafId = requestAnimationFrame(() => {
76+
rafId = null;
77+
lexContent();
78+
});
79+
}
6180
}
62-
})();
81+
}
82+
83+
onDestroy(() => {
84+
if (rafId) {
85+
cancelAnimationFrame(rafId);
86+
}
87+
});
6388
</script>
6489
6590
{#key id}

0 commit comments

Comments
 (0)