mirror of
https://github.com/purocean/yn.git
synced 2026-09-24 15:46:02 +08:00
feat(code-runner): simplify buffer flushing by removing debounce logic and using setInterval
This commit is contained in:
@@ -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')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user