fix(app): terminal pty isolation
This commit is contained in:
33
packages/app/src/utils/terminal-writer.test.ts
Normal file
33
packages/app/src/utils/terminal-writer.test.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { terminalWriter } from "./terminal-writer"
|
||||
|
||||
describe("terminalWriter", () => {
|
||||
test("buffers and flushes once per schedule", () => {
|
||||
const calls: string[] = []
|
||||
const scheduled: VoidFunction[] = []
|
||||
const writer = terminalWriter(
|
||||
(data) => calls.push(data),
|
||||
(flush) => scheduled.push(flush),
|
||||
)
|
||||
|
||||
writer.push("a")
|
||||
writer.push("b")
|
||||
writer.push("c")
|
||||
|
||||
expect(calls).toEqual([])
|
||||
expect(scheduled).toHaveLength(1)
|
||||
|
||||
scheduled[0]?.()
|
||||
expect(calls).toEqual(["abc"])
|
||||
})
|
||||
|
||||
test("flush is a no-op when empty", () => {
|
||||
const calls: string[] = []
|
||||
const writer = terminalWriter(
|
||||
(data) => calls.push(data),
|
||||
(flush) => flush(),
|
||||
)
|
||||
writer.flush()
|
||||
expect(calls).toEqual([])
|
||||
})
|
||||
})
|
||||
27
packages/app/src/utils/terminal-writer.ts
Normal file
27
packages/app/src/utils/terminal-writer.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
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 }
|
||||
}
|
||||
Reference in New Issue
Block a user