compiler.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { isMainFile } from './editor-files'
  2. import getMeta from '../../../utils/meta'
  3. import { sendMBSampled } from '../../../infrastructure/event-tracking'
  4. import { deleteJSON, postJSON } from '../../../infrastructure/fetch-json'
  5. import { debounce } from 'lodash'
  6. const AUTO_COMPILE_MAX_WAIT = 5000
  7. // We add a 1 second debounce to sending user changes to server if they aren't
  8. // collaborating with anyone. This needs to be higher than that, and allow for
  9. // client to server latency, otherwise we compile before the op reaches the server
  10. // and then again on ack.
  11. const AUTO_COMPILE_DEBOUNCE = 2000
  12. const searchParams = new URLSearchParams(window.location.search)
  13. export default class DocumentCompiler {
  14. constructor({
  15. project,
  16. setChangedAt,
  17. setCompiling,
  18. setData,
  19. setError,
  20. signal,
  21. }) {
  22. this.project = project
  23. this.setChangedAt = setChangedAt
  24. this.setCompiling = setCompiling
  25. this.setData = setData
  26. this.setError = setError
  27. this.signal = signal
  28. this.clsiServerId = null
  29. this.currentDoc = null
  30. this.error = undefined
  31. this.timer = 0
  32. this.debouncedAutoCompile = debounce(
  33. () => {
  34. this.compile({ isAutoCompileOnChange: true })
  35. },
  36. AUTO_COMPILE_DEBOUNCE,
  37. {
  38. maxWait: AUTO_COMPILE_MAX_WAIT,
  39. }
  40. )
  41. }
  42. destroy() {
  43. this.signal.abort()
  44. this.debouncedAutoCompile.cancel()
  45. }
  46. // The main "compile" function.
  47. // Call this directly to run a compile now, otherwise call debouncedAutoCompile.
  48. async compile(options = {}) {
  49. // set "compiling" to true (in the React component's state), and return if it was already true
  50. let wasCompiling
  51. this.setCompiling(oldValue => {
  52. wasCompiling = oldValue
  53. return true
  54. })
  55. if (wasCompiling) {
  56. return
  57. }
  58. try {
  59. // log a sample of the compile requests
  60. sendMBSampled('editor-recompile-sampled', options)
  61. // reset values
  62. this.setChangedAt(0)
  63. this.validationIssues = undefined
  64. window.dispatchEvent(new CustomEvent('flush-changes')) // TODO: wait for this?
  65. const params = this.buildQueryParams()
  66. this.addCompileParams(params, options)
  67. const data = await postJSON(
  68. `/project/${this.project._id}/compile?${params}`,
  69. {
  70. body: {
  71. rootDoc_id: this.getRootDocOverrideId(),
  72. draft: this.draft,
  73. check: 'silent', // NOTE: 'error' and 'validate' are possible, but unused
  74. // use incremental compile for all users but revert to a full compile
  75. // if there was previously a server error
  76. incrementalCompilesEnabled: !this.error,
  77. },
  78. signal: this.signal,
  79. }
  80. )
  81. data.options = options
  82. this.setData(data)
  83. } catch (error) {
  84. console.error(error)
  85. this.setError(error.info?.statusCode === 429 ? 'rate-limited' : 'error')
  86. } finally {
  87. this.setCompiling(false)
  88. }
  89. }
  90. // parse the text of the current doc in the editor
  91. // if it contains "\documentclass" then use this as the root doc
  92. getRootDocOverrideId() {
  93. // only override when not in the root doc itself
  94. if (this.currentDoc.doc_id !== this.project.rootDoc_id) {
  95. const snapshot = this.currentDoc.getSnapshot()
  96. if (snapshot && isMainFile(snapshot)) {
  97. return this.currentDoc.doc_id
  98. }
  99. }
  100. return null
  101. }
  102. // build the query parameters added to all requests
  103. buildQueryParams() {
  104. const params = new URLSearchParams()
  105. // the id of the CLSI server that processed the previous compile request
  106. if (this.clsiServerId) {
  107. params.set('clsiserverid', this.clsiServerId)
  108. }
  109. return params
  110. }
  111. // add extra query parameters to the compile request
  112. addCompileParams(params, options) {
  113. // tell the server whether this is an automatic or manual compile request
  114. if (options.isAutoCompileOnLoad || options.isAutoCompileOnChange) {
  115. params.set('auto_compile', 'true')
  116. }
  117. // use the feature flag to enable PDF caching in a ServiceWorker
  118. if (getMeta('ol-enablePdfCaching')) {
  119. params.set('enable_pdf_caching', 'true')
  120. }
  121. // use the feature flag to enable "file line errors"
  122. if (searchParams.get('file_line_errors') === 'true') {
  123. params.file_line_errors = 'true'
  124. }
  125. }
  126. // send a request to stop the current compile
  127. stopCompile() {
  128. // NOTE: no stoppingCompile state, as this should happen fairly quickly
  129. // and doesn't matter if it runs twice.
  130. const params = this.buildQueryParams()
  131. return postJSON(`/project/${this.project._id}/compile/stop?${params}`, {
  132. signal: this.signal,
  133. })
  134. .catch(error => {
  135. console.error(error)
  136. this.setError('error')
  137. })
  138. .finally(() => {
  139. this.setCompiling(false)
  140. })
  141. }
  142. clearCache() {
  143. const params = this.buildQueryParams()
  144. return deleteJSON(`/project/${this.project._id}/output?${params}`, {
  145. signal: this.signal,
  146. }).catch(error => {
  147. console.error(error)
  148. this.setError('clear-cache')
  149. })
  150. }
  151. }