compiler.js 5.1 KB

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