detach-compile-context.js 9.1 KB

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