2021-10-08 05:23:33 -04:00
|
|
|
import { isMainFile } from './editor-files'
|
|
|
|
import getMeta from '../../../utils/meta'
|
|
|
|
import { deleteJSON, postJSON } from '../../../infrastructure/fetch-json'
|
|
|
|
import { debounce } from 'lodash'
|
2022-03-15 08:47:01 -04:00
|
|
|
import { trackPdfDownload } from './metrics'
|
2022-07-20 04:32:05 -04:00
|
|
|
import { enablePdfCaching } from './pdf-caching-flags'
|
2023-09-27 05:45:49 -04:00
|
|
|
import { debugConsole } from '@/utils/debugging'
|
2021-10-08 05:23:33 -04:00
|
|
|
|
|
|
|
const AUTO_COMPILE_MAX_WAIT = 5000
|
2021-11-25 05:25:33 -05:00
|
|
|
// We add a 2 second debounce to sending user changes to server if they aren't
|
|
|
|
// collaborating with anyone. This needs to be higher than SINGLE_USER_FLUSH_DELAY, and allow for
|
2021-10-08 05:23:33 -04:00
|
|
|
// client to server latency, otherwise we compile before the op reaches the server
|
|
|
|
// and then again on ack.
|
2021-11-25 05:25:33 -05:00
|
|
|
const AUTO_COMPILE_DEBOUNCE = 2500
|
2021-10-08 05:23:33 -04:00
|
|
|
|
2024-01-15 04:02:53 -05:00
|
|
|
// If there is a pending op, wait for it to be saved before compiling
|
|
|
|
const PENDING_OP_MAX_WAIT = 10000
|
|
|
|
|
2021-10-08 05:23:33 -04:00
|
|
|
const searchParams = new URLSearchParams(window.location.search)
|
|
|
|
|
|
|
|
export default class DocumentCompiler {
|
|
|
|
constructor({
|
2022-03-03 11:28:12 -05:00
|
|
|
compilingRef,
|
2022-01-10 10:47:10 -05:00
|
|
|
projectId,
|
2021-10-08 05:23:33 -04:00
|
|
|
setChangedAt,
|
|
|
|
setCompiling,
|
|
|
|
setData,
|
2021-10-20 04:45:10 -04:00
|
|
|
setFirstRenderDone,
|
2022-06-21 08:15:26 -04:00
|
|
|
setDeliveryLatencies,
|
2021-10-08 05:23:33 -04:00
|
|
|
setError,
|
2021-11-03 10:30:41 -04:00
|
|
|
cleanupCompileResult,
|
2021-10-08 05:23:33 -04:00
|
|
|
signal,
|
|
|
|
}) {
|
2022-03-03 11:28:12 -05:00
|
|
|
this.compilingRef = compilingRef
|
2022-01-10 10:47:10 -05:00
|
|
|
this.projectId = projectId
|
2021-10-08 05:23:33 -04:00
|
|
|
this.setChangedAt = setChangedAt
|
|
|
|
this.setCompiling = setCompiling
|
|
|
|
this.setData = setData
|
2021-10-20 04:45:10 -04:00
|
|
|
this.setFirstRenderDone = setFirstRenderDone
|
2022-06-21 08:15:26 -04:00
|
|
|
this.setDeliveryLatencies = setDeliveryLatencies
|
2021-10-08 05:23:33 -04:00
|
|
|
this.setError = setError
|
2021-11-03 10:30:41 -04:00
|
|
|
this.cleanupCompileResult = cleanupCompileResult
|
2021-10-08 05:23:33 -04:00
|
|
|
this.signal = signal
|
|
|
|
|
2023-11-21 09:12:41 -05:00
|
|
|
this.projectRootDocId = null
|
2021-10-08 05:23:33 -04:00
|
|
|
this.clsiServerId = null
|
|
|
|
this.currentDoc = null
|
|
|
|
this.error = undefined
|
|
|
|
this.timer = 0
|
2022-06-07 07:55:48 -04:00
|
|
|
this.defaultOptions = {
|
|
|
|
draft: false,
|
|
|
|
stopOnFirstError: false,
|
|
|
|
}
|
2021-10-08 05:23:33 -04:00
|
|
|
|
|
|
|
this.debouncedAutoCompile = debounce(
|
|
|
|
() => {
|
|
|
|
this.compile({ isAutoCompileOnChange: true })
|
|
|
|
},
|
|
|
|
AUTO_COMPILE_DEBOUNCE,
|
|
|
|
{
|
|
|
|
maxWait: AUTO_COMPILE_MAX_WAIT,
|
|
|
|
}
|
|
|
|
)
|
2024-01-15 04:02:53 -05:00
|
|
|
|
|
|
|
this._onDocSavedCallback = null
|
|
|
|
}
|
|
|
|
|
|
|
|
async _awaitBufferedOps() {
|
|
|
|
const removeEventListener = () => {
|
|
|
|
clearTimeout(this.pendingOpTimeout)
|
|
|
|
if (this._onDocSavedCallback) {
|
|
|
|
window.removeEventListener('doc:saved', this._onDocSavedCallback)
|
|
|
|
this._onDocSavedCallback = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
removeEventListener()
|
|
|
|
return new Promise(resolve => {
|
|
|
|
if (!this.currentDoc?.hasBufferedOps?.()) {
|
|
|
|
return resolve()
|
|
|
|
}
|
|
|
|
|
2024-01-26 04:26:31 -05:00
|
|
|
this._onDocSavedCallback = () => {
|
2024-01-15 04:02:53 -05:00
|
|
|
// TODO: it's possible that there's more than one doc open with buffered ops, and ideally we'd wait for all docs to be flushed
|
|
|
|
removeEventListener()
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
|
|
|
|
clearTimeout(this.pendingOpTimeout)
|
|
|
|
this.pendingOpTimeout = setTimeout(() => {
|
|
|
|
removeEventListener()
|
|
|
|
resolve()
|
|
|
|
}, PENDING_OP_MAX_WAIT)
|
|
|
|
|
|
|
|
window.addEventListener('doc:saved', this._onDocSavedCallback)
|
|
|
|
window.dispatchEvent(new CustomEvent('flush-changes'))
|
|
|
|
})
|
2021-10-08 05:23:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// The main "compile" function.
|
|
|
|
// Call this directly to run a compile now, otherwise call debouncedAutoCompile.
|
|
|
|
async compile(options = {}) {
|
2022-06-07 07:55:48 -04:00
|
|
|
options = { ...this.defaultOptions, ...options }
|
2021-10-21 04:10:25 -04:00
|
|
|
|
2022-08-09 09:49:27 -04:00
|
|
|
if (options.isAutoCompileOnLoad && getMeta('ol-preventCompileOnLoad')) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-08 05:23:33 -04:00
|
|
|
// set "compiling" to true (in the React component's state), and return if it was already true
|
2022-03-03 11:28:12 -05:00
|
|
|
const wasCompiling = this.compilingRef.current
|
|
|
|
this.setCompiling(true)
|
2021-10-08 05:23:33 -04:00
|
|
|
|
|
|
|
if (wasCompiling) {
|
2021-11-25 05:25:21 -05:00
|
|
|
if (options.isAutoCompileOnChange) {
|
|
|
|
this.debouncedAutoCompile()
|
|
|
|
}
|
2021-10-08 05:23:33 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2024-01-15 04:02:53 -05:00
|
|
|
await this._awaitBufferedOps()
|
|
|
|
|
2021-10-08 05:23:33 -04:00
|
|
|
// reset values
|
2022-06-21 05:58:56 -04:00
|
|
|
this.setChangedAt(0) // TODO: wait for doc:saved?
|
2021-10-08 05:23:33 -04:00
|
|
|
this.validationIssues = undefined
|
|
|
|
|
2021-10-12 04:48:48 -04:00
|
|
|
const params = this.buildCompileParams(options)
|
2021-10-08 05:23:33 -04:00
|
|
|
|
2021-10-20 04:45:10 -04:00
|
|
|
const t0 = performance.now()
|
|
|
|
|
2023-11-21 09:12:41 -05:00
|
|
|
const rootDocId = this.getRootDocOverrideId()
|
|
|
|
|
2022-06-06 07:41:36 -04:00
|
|
|
const body = {
|
2023-11-21 09:12:41 -05:00
|
|
|
rootDoc_id: rootDocId,
|
2022-06-07 07:55:48 -04:00
|
|
|
draft: options.draft,
|
2022-06-06 07:41:36 -04:00
|
|
|
check: 'silent', // NOTE: 'error' and 'validate' are possible, but unused
|
|
|
|
// use incremental compile for all users but revert to a full compile
|
|
|
|
// if there was previously a server error
|
|
|
|
incrementalCompilesEnabled: !this.error,
|
2022-10-03 11:22:13 -04:00
|
|
|
stopOnFirstError: options.stopOnFirstError,
|
2022-06-06 07:41:36 -04:00
|
|
|
}
|
2022-10-03 11:22:13 -04:00
|
|
|
|
2021-10-08 05:23:33 -04:00
|
|
|
const data = await postJSON(
|
2022-01-10 10:47:10 -05:00
|
|
|
`/project/${this.projectId}/compile?${params}`,
|
2022-06-06 07:41:36 -04:00
|
|
|
{ body, signal: this.signal }
|
2021-10-08 05:23:33 -04:00
|
|
|
)
|
2022-06-06 07:41:36 -04:00
|
|
|
|
2022-06-21 08:15:26 -04:00
|
|
|
const compileTimeClientE2E = Math.ceil(performance.now() - t0)
|
|
|
|
const { deliveryLatencies, firstRenderDone } = trackPdfDownload(
|
|
|
|
data,
|
2022-07-18 06:24:31 -04:00
|
|
|
compileTimeClientE2E,
|
|
|
|
t0
|
2022-06-21 08:15:26 -04:00
|
|
|
)
|
|
|
|
this.setDeliveryLatencies(() => deliveryLatencies)
|
2021-10-20 04:45:10 -04:00
|
|
|
this.setFirstRenderDone(() => firstRenderDone)
|
2022-01-10 08:59:22 -05:00
|
|
|
|
|
|
|
// unset the error before it's set again later, so that components are recreated and events are tracked
|
|
|
|
this.setError(undefined)
|
|
|
|
|
2021-10-08 05:23:33 -04:00
|
|
|
data.options = options
|
2023-11-21 09:12:41 -05:00
|
|
|
data.rootDocId = rootDocId
|
2021-11-03 10:30:41 -04:00
|
|
|
if (data.clsiServerId) {
|
|
|
|
this.clsiServerId = data.clsiServerId
|
|
|
|
}
|
2021-10-08 05:23:33 -04:00
|
|
|
this.setData(data)
|
|
|
|
} catch (error) {
|
2023-09-27 05:45:49 -04:00
|
|
|
debugConsole.error(error)
|
2021-11-03 10:30:41 -04:00
|
|
|
this.cleanupCompileResult()
|
2021-10-08 05:23:33 -04:00
|
|
|
this.setError(error.info?.statusCode === 429 ? 'rate-limited' : 'error')
|
|
|
|
} finally {
|
|
|
|
this.setCompiling(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the text of the current doc in the editor
|
|
|
|
// if it contains "\documentclass" then use this as the root doc
|
|
|
|
getRootDocOverrideId() {
|
|
|
|
// only override when not in the root doc itself
|
2023-11-21 09:12:41 -05:00
|
|
|
if (this.currentDoc.doc_id !== this.projectRootDocId) {
|
2021-10-08 05:23:33 -04:00
|
|
|
const snapshot = this.currentDoc.getSnapshot()
|
|
|
|
|
|
|
|
if (snapshot && isMainFile(snapshot)) {
|
|
|
|
return this.currentDoc.doc_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2021-10-12 04:48:48 -04:00
|
|
|
// build the query parameters added to post-compile requests
|
|
|
|
buildPostCompileParams() {
|
2021-10-08 05:23:33 -04:00
|
|
|
const params = new URLSearchParams()
|
|
|
|
|
|
|
|
// the id of the CLSI server that processed the previous compile request
|
|
|
|
if (this.clsiServerId) {
|
|
|
|
params.set('clsiserverid', this.clsiServerId)
|
|
|
|
}
|
|
|
|
|
|
|
|
return params
|
|
|
|
}
|
|
|
|
|
2021-10-12 04:48:48 -04:00
|
|
|
// build the query parameters for the compile request
|
|
|
|
buildCompileParams(options) {
|
|
|
|
const params = new URLSearchParams()
|
|
|
|
|
|
|
|
// note: no clsiserverid query param is set on "compile" requests,
|
|
|
|
// as this is added in the backend by the web api
|
|
|
|
|
2021-10-08 05:23:33 -04:00
|
|
|
// tell the server whether this is an automatic or manual compile request
|
|
|
|
if (options.isAutoCompileOnLoad || options.isAutoCompileOnChange) {
|
|
|
|
params.set('auto_compile', 'true')
|
|
|
|
}
|
|
|
|
|
2022-07-08 04:15:13 -04:00
|
|
|
// use the feature flag to enable PDF caching
|
2022-07-20 04:32:05 -04:00
|
|
|
if (enablePdfCaching) {
|
2021-10-08 05:23:33 -04:00
|
|
|
params.set('enable_pdf_caching', 'true')
|
|
|
|
}
|
|
|
|
|
|
|
|
// use the feature flag to enable "file line errors"
|
|
|
|
if (searchParams.get('file_line_errors') === 'true') {
|
|
|
|
params.file_line_errors = 'true'
|
|
|
|
}
|
2021-10-12 04:48:48 -04:00
|
|
|
|
|
|
|
return params
|
2021-10-08 05:23:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// send a request to stop the current compile
|
|
|
|
stopCompile() {
|
|
|
|
// NOTE: no stoppingCompile state, as this should happen fairly quickly
|
|
|
|
// and doesn't matter if it runs twice.
|
|
|
|
|
2021-10-12 04:48:48 -04:00
|
|
|
const params = this.buildPostCompileParams()
|
2021-10-08 05:23:33 -04:00
|
|
|
|
2022-01-10 10:47:10 -05:00
|
|
|
return postJSON(`/project/${this.projectId}/compile/stop?${params}`, {
|
2021-10-08 05:23:33 -04:00
|
|
|
signal: this.signal,
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2023-09-27 05:45:49 -04:00
|
|
|
debugConsole.error(error)
|
2021-10-08 05:23:33 -04:00
|
|
|
this.setError('error')
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
this.setCompiling(false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-12 04:48:48 -04:00
|
|
|
// send a request to clear the cache
|
2021-10-08 05:23:33 -04:00
|
|
|
clearCache() {
|
2021-10-12 04:48:48 -04:00
|
|
|
const params = this.buildPostCompileParams()
|
2021-10-08 05:23:33 -04:00
|
|
|
|
2022-01-10 10:47:10 -05:00
|
|
|
return deleteJSON(`/project/${this.projectId}/output?${params}`, {
|
2021-10-08 05:23:33 -04:00
|
|
|
signal: this.signal,
|
|
|
|
}).catch(error => {
|
2023-09-27 05:45:49 -04:00
|
|
|
debugConsole.error(error)
|
2021-10-08 05:23:33 -04:00
|
|
|
this.setError('clear-cache')
|
|
|
|
})
|
|
|
|
}
|
2022-06-07 07:55:48 -04:00
|
|
|
|
|
|
|
setOption(option, value) {
|
|
|
|
this.defaultOptions[option] = value
|
|
|
|
}
|
2021-10-08 05:23:33 -04:00
|
|
|
}
|