Merge pull request #81 from nesquena/feat/raf-streaming-throttle

perf: rAF-throttled token streaming for smoother rendering
This commit is contained in:
Nathan Esquenazi
2026-04-04 14:22:55 -07:00
committed by GitHub

View File

@@ -103,14 +103,25 @@ async function send(){
// ── Shared SSE handler wiring (used for initial connection and reconnect) ──
let _reconnectAttempted=false;
// rAF-throttled rendering: buffer tokens, render at most once per frame
let _renderPending=false;
function _scheduleRender(){
if(_renderPending) return;
_renderPending=true;
requestAnimationFrame(()=>{
_renderPending=false;
if(assistantBody) assistantBody.innerHTML=renderMd(assistantText);
scrollIfPinned();
});
}
function _wireSSE(source){
source.addEventListener('token',e=>{
if(!S.session||S.session.session_id!==activeSid) return;
const d=JSON.parse(e.data);
assistantText+=d.text;
ensureAssistantRow();
assistantBody.innerHTML=renderMd(assistantText);
scrollIfPinned();
_scheduleRender();
});
source.addEventListener('tool',e=>{