detach-compile-context.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. import { createContext, useContext, useMemo } from 'react'
  2. import PropTypes from 'prop-types'
  3. import {
  4. useLocalCompileContext,
  5. CompileContextPropTypes,
  6. } from './local-compile-context'
  7. import useDetachStateWatcher from '../hooks/use-detach-state-watcher'
  8. import useDetachAction from '../hooks/use-detach-action'
  9. export const DetachCompileContext = createContext()
  10. DetachCompileContext.Provider.propTypes = CompileContextPropTypes
  11. export function DetachCompileProvider({ children }) {
  12. const localCompileContext = useLocalCompileContext()
  13. if (!localCompileContext) {
  14. throw new Error(
  15. 'DetachCompileProvider is only available inside LocalCompileProvider'
  16. )
  17. }
  18. const {
  19. autoCompile: _autoCompile,
  20. clearingCache: _clearingCache,
  21. clsiServerId: _clsiServerId,
  22. codeCheckFailed: _codeCheckFailed,
  23. compiling: _compiling,
  24. draft: _draft,
  25. error: _error,
  26. fileList: _fileList,
  27. hasChanges: _hasChanges,
  28. highlights: _highlights,
  29. logEntries: _logEntries,
  30. logEntryAnnotations: _logEntryAnnotations,
  31. pdfDownloadUrl: _pdfDownloadUrl,
  32. pdfUrl: _pdfUrl,
  33. pdfViewer: _pdfViewer,
  34. position: _position,
  35. rawLog: _rawLog,
  36. setAutoCompile: _setAutoCompile,
  37. setDraft: _setDraft,
  38. setError: _setError,
  39. setHasLintingError: _setHasLintingError,
  40. setHighlights: _setHighlights,
  41. setPosition: _setPosition,
  42. setShowLogs: _setShowLogs,
  43. toggleLogs: _toggleLogs,
  44. setStopOnFirstError: _setStopOnFirstError,
  45. setStopOnValidationError: _setStopOnValidationError,
  46. showLogs: _showLogs,
  47. stopOnFirstError: _stopOnFirstError,
  48. stopOnValidationError: _stopOnValidationError,
  49. uncompiled: _uncompiled,
  50. validationIssues: _validationIssues,
  51. firstRenderDone: _firstRenderDone,
  52. cleanupCompileResult: _cleanupCompileResult,
  53. recompileFromScratch: _recompileFromScratch,
  54. setCompiling: _setCompiling,
  55. startCompile: _startCompile,
  56. stopCompile: _stopCompile,
  57. setChangedAt: _setChangedAt,
  58. clearCache: _clearCache,
  59. } = localCompileContext
  60. const [autoCompile] = useDetachStateWatcher(
  61. 'autoCompile',
  62. _autoCompile,
  63. 'detacher',
  64. 'detached'
  65. )
  66. const [clearingCache] = useDetachStateWatcher(
  67. 'clearingCache',
  68. _clearingCache,
  69. 'detacher',
  70. 'detached'
  71. )
  72. const [clsiServerId] = useDetachStateWatcher(
  73. 'clsiServerId',
  74. _clsiServerId,
  75. 'detacher',
  76. 'detached'
  77. )
  78. const [codeCheckFailed] = useDetachStateWatcher(
  79. 'codeCheckFailed',
  80. _codeCheckFailed,
  81. 'detacher',
  82. 'detached'
  83. )
  84. const [compiling] = useDetachStateWatcher(
  85. 'compiling',
  86. _compiling,
  87. 'detacher',
  88. 'detached'
  89. )
  90. const [draft] = useDetachStateWatcher('draft', _draft, 'detacher', 'detached')
  91. const [error] = useDetachStateWatcher('error', _error, 'detacher', 'detached')
  92. const [fileList] = useDetachStateWatcher(
  93. 'fileList',
  94. _fileList,
  95. 'detacher',
  96. 'detached'
  97. )
  98. const [hasChanges] = useDetachStateWatcher(
  99. 'hasChanges',
  100. _hasChanges,
  101. 'detacher',
  102. 'detached'
  103. )
  104. const [highlights] = useDetachStateWatcher(
  105. 'highlights',
  106. _highlights,
  107. 'detacher',
  108. 'detached'
  109. )
  110. const [logEntries] = useDetachStateWatcher(
  111. 'logEntries',
  112. _logEntries,
  113. 'detacher',
  114. 'detached'
  115. )
  116. const [logEntryAnnotations] = useDetachStateWatcher(
  117. 'logEntryAnnotations',
  118. _logEntryAnnotations,
  119. 'detacher',
  120. 'detached'
  121. )
  122. const [pdfDownloadUrl] = useDetachStateWatcher(
  123. 'pdfDownloadUrl',
  124. _pdfDownloadUrl,
  125. 'detacher',
  126. 'detached'
  127. )
  128. const [pdfUrl] = useDetachStateWatcher(
  129. 'pdfUrl',
  130. _pdfUrl,
  131. 'detacher',
  132. 'detached'
  133. )
  134. const [pdfViewer] = useDetachStateWatcher(
  135. 'pdfViewer',
  136. _pdfViewer,
  137. 'detacher',
  138. 'detached'
  139. )
  140. const [position] = useDetachStateWatcher(
  141. 'position',
  142. _position,
  143. 'detacher',
  144. 'detached'
  145. )
  146. const [rawLog] = useDetachStateWatcher(
  147. 'rawLog',
  148. _rawLog,
  149. 'detacher',
  150. 'detached'
  151. )
  152. const [showLogs] = useDetachStateWatcher(
  153. 'showLogs',
  154. _showLogs,
  155. 'detacher',
  156. 'detached'
  157. )
  158. const [stopOnFirstError] = useDetachStateWatcher(
  159. 'stopOnFirstError',
  160. _stopOnFirstError,
  161. 'detacher',
  162. 'detached'
  163. )
  164. const [stopOnValidationError] = useDetachStateWatcher(
  165. 'stopOnValidationError',
  166. _stopOnValidationError,
  167. 'detacher',
  168. 'detached'
  169. )
  170. const [uncompiled] = useDetachStateWatcher(
  171. 'uncompiled',
  172. _uncompiled,
  173. 'detacher',
  174. 'detached'
  175. )
  176. const [validationIssues] = useDetachStateWatcher(
  177. 'validationIssues',
  178. _validationIssues,
  179. 'detacher',
  180. 'detached'
  181. )
  182. const setAutoCompile = useDetachAction(
  183. 'setAutoCompile',
  184. _setAutoCompile,
  185. 'detached',
  186. 'detacher'
  187. )
  188. const setDraft = useDetachAction(
  189. 'setDraft',
  190. _setDraft,
  191. 'detached',
  192. 'detacher'
  193. )
  194. const setError = useDetachAction(
  195. 'setError',
  196. _setError,
  197. 'detacher',
  198. 'detached'
  199. )
  200. const setPosition = useDetachAction(
  201. 'setPosition',
  202. _setPosition,
  203. 'detached',
  204. 'detacher'
  205. )
  206. const firstRenderDone = useDetachAction(
  207. 'firstRenderDone',
  208. _firstRenderDone,
  209. 'detacher',
  210. 'detached'
  211. )
  212. const setHasLintingError = useDetachAction(
  213. 'setHasLintingError',
  214. _setHasLintingError,
  215. 'detacher',
  216. 'detached'
  217. )
  218. const setHighlights = useDetachAction(
  219. 'setHighlights',
  220. _setHighlights,
  221. 'detacher',
  222. 'detached'
  223. )
  224. const setShowLogs = useDetachAction(
  225. 'setShowLogs',
  226. _setShowLogs,
  227. 'detached',
  228. 'detacher'
  229. )
  230. const toggleLogs = useDetachAction(
  231. 'toggleLogs',
  232. _toggleLogs,
  233. 'detached',
  234. 'detacher'
  235. )
  236. const setStopOnFirstError = useDetachAction(
  237. 'setStopOnFirstError',
  238. _setStopOnFirstError,
  239. 'detached',
  240. 'detacher'
  241. )
  242. const setStopOnValidationError = useDetachAction(
  243. 'setStopOnValidationError',
  244. _setStopOnValidationError,
  245. 'detached',
  246. 'detacher'
  247. )
  248. const cleanupCompileResult = useDetachAction(
  249. 'cleanupCompileResult',
  250. _cleanupCompileResult,
  251. 'detached',
  252. 'detacher'
  253. )
  254. const recompileFromScratch = useDetachAction(
  255. 'recompileFromScratch',
  256. _recompileFromScratch,
  257. 'detached',
  258. 'detacher'
  259. )
  260. const setCompiling = useDetachAction(
  261. 'setCompiling',
  262. _setCompiling,
  263. 'detacher',
  264. 'detached'
  265. )
  266. const startCompile = useDetachAction(
  267. 'startCompile',
  268. _startCompile,
  269. 'detached',
  270. 'detacher'
  271. )
  272. const stopCompile = useDetachAction(
  273. 'stopCompile',
  274. _stopCompile,
  275. 'detached',
  276. 'detacher'
  277. )
  278. const setChangedAt = useDetachAction(
  279. 'setChangedAt',
  280. _setChangedAt,
  281. 'detached',
  282. 'detacher'
  283. )
  284. const clearCache = useDetachAction(
  285. 'clearCache',
  286. _clearCache,
  287. 'detached',
  288. 'detacher'
  289. )
  290. const value = useMemo(
  291. () => ({
  292. autoCompile,
  293. clearCache,
  294. clearingCache,
  295. clsiServerId,
  296. codeCheckFailed,
  297. compiling,
  298. draft,
  299. error,
  300. fileList,
  301. hasChanges,
  302. highlights,
  303. logEntryAnnotations,
  304. logEntries,
  305. pdfDownloadUrl,
  306. pdfUrl,
  307. pdfViewer,
  308. position,
  309. rawLog,
  310. recompileFromScratch,
  311. setAutoCompile,
  312. setCompiling,
  313. setDraft,
  314. setError,
  315. setHasLintingError,
  316. setHighlights,
  317. setPosition,
  318. setShowLogs,
  319. toggleLogs,
  320. setStopOnFirstError,
  321. setStopOnValidationError,
  322. showLogs,
  323. startCompile,
  324. stopCompile,
  325. stopOnFirstError,
  326. stopOnValidationError,
  327. uncompiled,
  328. validationIssues,
  329. firstRenderDone,
  330. setChangedAt,
  331. cleanupCompileResult,
  332. }),
  333. [
  334. autoCompile,
  335. clearCache,
  336. clearingCache,
  337. clsiServerId,
  338. codeCheckFailed,
  339. compiling,
  340. draft,
  341. error,
  342. fileList,
  343. hasChanges,
  344. highlights,
  345. logEntryAnnotations,
  346. logEntries,
  347. pdfDownloadUrl,
  348. pdfUrl,
  349. pdfViewer,
  350. position,
  351. rawLog,
  352. recompileFromScratch,
  353. setAutoCompile,
  354. setCompiling,
  355. setDraft,
  356. setError,
  357. setHasLintingError,
  358. setHighlights,
  359. setPosition,
  360. setShowLogs,
  361. toggleLogs,
  362. setStopOnFirstError,
  363. setStopOnValidationError,
  364. showLogs,
  365. startCompile,
  366. stopCompile,
  367. stopOnFirstError,
  368. stopOnValidationError,
  369. uncompiled,
  370. validationIssues,
  371. firstRenderDone,
  372. setChangedAt,
  373. cleanupCompileResult,
  374. ]
  375. )
  376. return (
  377. <DetachCompileContext.Provider value={value}>
  378. {children}
  379. </DetachCompileContext.Provider>
  380. )
  381. }
  382. DetachCompileProvider.propTypes = {
  383. children: PropTypes.any,
  384. }
  385. export function useDetachCompileContext(propTypes) {
  386. const data = useContext(DetachCompileContext)
  387. PropTypes.checkPropTypes(
  388. propTypes,
  389. data,
  390. 'data',
  391. 'DetachCompileContext.Provider'
  392. )
  393. return data
  394. }