Always wait for buffered ops before compiling (#23336)

GitOrigin-RevId: 1e586979897fb01378e449fe9dc9c7d269de83bb
This commit is contained in:
Alf Eaton
2025-02-04 09:23:45 +00:00
committed by Copybot
parent 637f12675c
commit bb1745cf2d
2 changed files with 29 additions and 11 deletions

View File

@@ -5,6 +5,7 @@ import { debounce } from 'lodash'
import { trackPdfDownload } from './metrics'
import { enablePdfCaching } from './pdf-caching-flags'
import { debugConsole } from '@/utils/debugging'
import { signalWithTimeout } from '@/utils/abort-signal'
const AUTO_COMPILE_MAX_WAIT = 5000
// We add a 2 second debounce to sending user changes to server if they aren't
@@ -86,17 +87,9 @@ export default class DocumentCompiler {
}
try {
if (
typeof AbortSignal.any === 'function' &&
typeof AbortSignal.timeout === 'function'
) {
await this.openDocs.awaitBufferedOps(
AbortSignal.any([
this.signal,
AbortSignal.timeout(PENDING_OP_MAX_WAIT),
])
)
}
await this.openDocs.awaitBufferedOps(
signalWithTimeout(this.signal, PENDING_OP_MAX_WAIT)
)
// reset values
this.setChangedAt(0) // TODO: wait for doc:saved?

View File

@@ -0,0 +1,25 @@
export const supportsModernAbortSignal =
typeof AbortSignal.any === 'function' &&
typeof AbortSignal.timeout === 'function'
export const signalWithTimeout = (signal: AbortSignal, timeout: number) => {
if (supportsModernAbortSignal) {
return AbortSignal.any([signal, AbortSignal.timeout(timeout)])
}
const abortController = new AbortController()
const abort = () => {
window.clearTimeout(timer)
signal.removeEventListener('abort', abort)
abortController.abort()
}
// abort after timeout has expired
const timer = window.setTimeout(abort, timeout)
// abort when the original signal is aborted
signal.addEventListener('abort', abort)
return abortController.signal
}