feat(code-runner): simplify buffer flushing by removing debounce logic and using setInterval

This commit is contained in:
purocean
2025-01-10 17:53:03 +08:00
parent f300c3493b
commit 79931c398e
+3 -19
View File
@@ -108,39 +108,21 @@ class JavascriptWorkerExecutor {
const maxFlushInterval = 100
let buffer = ''
let lastFlushedAt = 0
let timer = 0
const clearTimer = () => {
if (timer) {
clearTimeout(timer)
timer = 0
}
}
const flushBuffer = () => {
clearTimer()
if (buffer) {
Atomics.wait(isb, 0, 1)
self.postMessage({ type: 'output', value: buffer })
lastFlushedAt = performance.now()
buffer = ''
}
}
const debounceFlushBuffer = () => {
clearTimer()
timer = setTimeout(flushBuffer, maxFlushInterval)
}
const flush = (type, val) => {
if (type === 'output') {
buffer += val
if (buffer.length > maxBuffer || performance.now() - lastFlushedAt > maxFlushInterval) {
if (buffer.length > maxBuffer) {
flushBuffer()
} else {
debounceFlushBuffer()
}
} else {
flushBuffer()
@@ -149,6 +131,8 @@ class JavascriptWorkerExecutor {
}
}
setInterval(flushBuffer, 100)
const xConsole = getConsole(console, val => flush('output', val))
const AsyncFunction = eval('(async function(){}).constructor')