detach-compile-context.js 8.9 KB

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