refactor(chat): introduce new stream handling utilities and restructure event processing for improved performance and maintainability

This commit is contained in:
Anish Sarkar 2026-04-30 18:09:34 +05:30
parent 6465ea181a
commit 86f6b285ce
3 changed files with 217 additions and 198 deletions

View file

@ -0,0 +1,19 @@
import { FrameBatchedUpdater } from "@/lib/chat/streaming-state";
export function createStreamFlushHelpers(flushMessages: () => void): {
batcher: FrameBatchedUpdater;
scheduleFlush: () => void;
forceFlush: () => void;
} {
const batcher = new FrameBatchedUpdater();
const scheduleFlush = () => batcher.schedule(flushMessages);
// Force-flush helper: ``batcher.flush()`` is a no-op when
// ``dirty=false`` (e.g. a tool starts before any text streamed).
// ``scheduleFlush(); batcher.flush()`` sets the dirty bit first so
// terminal events render promptly without the throttle delay.
const forceFlush = () => {
scheduleFlush();
batcher.flush();
};
return { batcher, scheduleFlush, forceFlush };
}