detach-compile-context.js 9.7 KB

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