compiler.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.debouncedAutoCompile.cancel()
  44. }
  45. // The main "compile" function.
  46. // Call this directly to run a compile now, otherwise call debouncedAutoCompile.
  47. async compile(options = {}) {
  48. // set "compiling" to true (in the React component's state), and return if it was already true
  49. let wasCompiling
  50. this.setCompiling(oldValue => {
  51. wasCompiling = oldValue
  52. return true
  53. })
  54. if (wasCompiling) {
  55. return
  56. }
  57. try {
  58. // log a sample of the compile requests
  59. sendMBSampled('editor-recompile-sampled', options)
  60. // reset values
  61. this.setChangedAt(0)
  62. this.validationIssues = undefined
  63. window.dispatchEvent(new CustomEvent('flush-changes')) // TODO: wait for this?
  64. const params = this.buildCompileParams(options)
  65. const data = await postJSON(
  66. `/project/${this.project._id}/compile?${params}`,
  67. {
  68. body: {
  69. rootDoc_id: this.getRootDocOverrideId(),
  70. draft: this.draft,
  71. check: 'silent', // NOTE: 'error' and 'validate' are possible, but unused
  72. // use incremental compile for all users but revert to a full compile
  73. // if there was previously a server error
  74. incrementalCompilesEnabled: !this.error,
  75. },
  76. signal: this.signal,
  77. }
  78. )
  79. data.options = options
  80. this.setData(data)
  81. } catch (error) {
  82. console.error(error)
  83. this.setError(error.info?.statusCode === 429 ? 'rate-limited' : 'error')
  84. } finally {
  85. this.setCompiling(false)
  86. }
  87. }
  88. // parse the text of the current doc in the editor
  89. // if it contains "\documentclass" then use this as the root doc
  90. getRootDocOverrideId() {
  91. // only override when not in the root doc itself
  92. if (this.currentDoc.doc_id !== this.project.rootDoc_id) {
  93. const snapshot = this.currentDoc.getSnapshot()
  94. if (snapshot && isMainFile(snapshot)) {
  95. return this.currentDoc.doc_id
  96. }
  97. }
  98. return null
  99. }
  100. // build the query parameters added to post-compile requests
  101. buildPostCompileParams() {
  102. const params = new URLSearchParams()
  103. // the id of the CLSI server that processed the previous compile request
  104. if (this.clsiServerId) {
  105. params.set('clsiserverid', this.clsiServerId)
  106. }
  107. return params
  108. }
  109. // build the query parameters for the compile request
  110. buildCompileParams(options) {
  111. const params = new URLSearchParams()
  112. // note: no clsiserverid query param is set on "compile" requests,
  113. // as this is added in the backend by the web api
  114. // tell the server whether this is an automatic or manual compile request
  115. if (options.isAutoCompileOnLoad || options.isAutoCompileOnChange) {
  116. params.set('auto_compile', 'true')
  117. }
  118. // use the feature flag to enable PDF caching in a ServiceWorker
  119. if (getMeta('ol-enablePdfCaching')) {
  120. params.set('enable_pdf_caching', 'true')
  121. }
  122. // use the feature flag to enable "file line errors"
  123. if (searchParams.get('file_line_errors') === 'true') {
  124. params.file_line_errors = 'true'
  125. }
  126. return params
  127. }
  128. // send a request to stop the current compile
  129. stopCompile() {
  130. // NOTE: no stoppingCompile state, as this should happen fairly quickly
  131. // and doesn't matter if it runs twice.
  132. const params = this.buildPostCompileParams()
  133. return postJSON(`/project/${this.project._id}/compile/stop?${params}`, {
  134. signal: this.signal,
  135. })
  136. .catch(error => {
  137. console.error(error)
  138. this.setError('error')
  139. })
  140. .finally(() => {
  141. this.setCompiling(false)
  142. })
  143. }
  144. // send a request to clear the cache
  145. clearCache() {
  146. const params = this.buildPostCompileParams()
  147. return deleteJSON(`/project/${this.project._id}/output?${params}`, {
  148. signal: this.signal,
  149. }).catch(error => {
  150. console.error(error)
  151. this.setError('clear-cache')
  152. })
  153. }
  154. }