GHHG10/CodeServer / opencode /packages /app /src /utils /terminal-writer.ts
GHHG10's picture
download
raw
1.34 kB
export function terminalWriter(
write: (data: string, done?: VoidFunction) => void,
schedule: (flush: VoidFunction) => void = queueMicrotask,
) {
let chunks: string[] | undefined
let waits: VoidFunction[] | undefined
let scheduled = false
let writing = false
const settle = () => {
if (scheduled || writing || chunks?.length) return
const list = waits
if (!list?.length) return
waits = undefined
for (const fn of list) {
fn()
}
}
const run = () => {
if (writing) return
scheduled = false
const items = chunks
if (!items?.length) {
settle()
return
}
chunks = undefined
writing = true
write(items.join(""), () => {
writing = false
if (chunks?.length) {
if (scheduled) return
scheduled = true
schedule(run)
return
}
settle()
})
}
const push = (data: string) => {
if (!data) return
if (chunks) chunks.push(data)
else chunks = [data]
if (scheduled || writing) return
scheduled = true
schedule(run)
}
const flush = (done?: VoidFunction) => {
if (!scheduled && !writing && !chunks?.length) {
done?.()
return
}
if (done) {
if (waits) waits.push(done)
else waits = [done]
}
run()
}
return { push, flush }
}

Xet Storage Details

Size:
1.34 kB
·
Xet hash:
e5d8aba63df5227fee4e3e671e198d9d588492049cde3c8c932ee96f45210069

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.