detach-compile-context.js 11 KB

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