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'
|
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
|
|
|
|
|
|
|
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,
|
|
|
|
rootDocId,
|
2021-10-08 05:23:33 -04:00
|
|
|
setChangedAt,
|
|
|
|
setCompiling,
|
|
|
|
setData,
|
2021-10-20 04:45:10 -04:00
|
|
|
setFirstRenderDone,
|
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
|
|
|
|
this.rootDocId = rootDocId
|
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
|
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
|
|
|
|
|
|
|
|
this.clsiServerId = null
|
|
|
|
this.currentDoc = null
|
|
|
|
this.error = undefined
|
|
|
|
this.timer = 0
|
|
|
|
|
|
|
|
this.debouncedAutoCompile = debounce(
|
|
|
|
() => {
|
|
|
|
this.compile({ isAutoCompileOnChange: true })
|
|
|
|
},
|
|
|
|
AUTO_COMPILE_DEBOUNCE,
|
|
|
|
{
|
|
|
|
maxWait: AUTO_COMPILE_MAX_WAIT,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The main "compile" function.
|
|
|
|
// Call this directly to run a compile now, otherwise call debouncedAutoCompile.
|
|
|
|
async compile(options = {}) {
|
2022-03-03 11:28:12 -05:00
|
|
|
if (!options) {
|
|
|
|
options = {}
|
|
|
|
}
|
2021-10-21 04:10:25 -04:00
|
|
|
|
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 {
|
|
|
|
// reset values
|
|
|
|
this.setChangedAt(0)
|
|
|
|
this.validationIssues = undefined
|
|
|
|
|
|
|
|
window.dispatchEvent(new CustomEvent('flush-changes')) // TODO: wait for this?
|
|
|
|
|
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()
|
|
|
|
|
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}`,
|
2021-10-08 05:23:33 -04:00
|
|
|
{
|
|
|
|
body: {
|
|
|
|
rootDoc_id: this.getRootDocOverrideId(),
|
|
|
|
draft: this.draft,
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
signal: this.signal,
|
|
|
|
}
|
|
|
|
)
|
2021-10-20 04:45:10 -04:00
|
|
|
const compileTimeClientE2E = performance.now() - t0
|
|
|
|
const { firstRenderDone } = trackPdfDownload(data, compileTimeClientE2E)
|
|
|
|
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
|
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) {
|
|
|
|
console.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
|
2022-01-10 10:47:10 -05:00
|
|
|
if (this.currentDoc.doc_id !== this.rootDocId) {
|
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')
|
|
|
|
}
|
|
|
|
|
|
|
|
// use the feature flag to enable PDF caching in a ServiceWorker
|
|
|
|
if (getMeta('ol-enablePdfCaching')) {
|
|
|
|
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 => {
|
|
|
|
console.error(error)
|
|
|
|
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 => {
|
|
|
|
console.error(error)
|
|
|
|
this.setError('clear-cache')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|