mirror of
https://github.com/anomalyco/opencode.git
synced 2026-04-24 23:04:55 +00:00
28 lines
561 B
TypeScript
28 lines
561 B
TypeScript
export function terminalWriter(
|
|
write: (data: string) => void,
|
|
schedule: (flush: VoidFunction) => void = queueMicrotask,
|
|
) {
|
|
let chunks: string[] | undefined
|
|
let scheduled = false
|
|
|
|
const flush = () => {
|
|
scheduled = false
|
|
const items = chunks
|
|
if (!items?.length) return
|
|
chunks = undefined
|
|
write(items.join(""))
|
|
}
|
|
|
|
const push = (data: string) => {
|
|
if (!data) return
|
|
if (chunks) chunks.push(data)
|
|
else chunks = [data]
|
|
|
|
if (scheduled) return
|
|
scheduled = true
|
|
schedule(flush)
|
|
}
|
|
|
|
return { push, flush }
|
|
}
|