detach-compile-context.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. pdfDownloadUrl: _pdfDownloadUrl,
  35. pdfSize: _pdfSize,
  36. pdfUrl: _pdfUrl,
  37. pdfViewer: _pdfViewer,
  38. position: _position,
  39. rawLog: _rawLog,
  40. setAnimateCompileDropdownArrow: _setAnimateCompileDropdownArrow,
  41. setAutoCompile: _setAutoCompile,
  42. setDraft: _setDraft,
  43. setError: _setError,
  44. setHasLintingError: _setHasLintingError,
  45. setHighlights: _setHighlights,
  46. setPosition: _setPosition,
  47. setShowLogs: _setShowLogs,
  48. toggleLogs: _toggleLogs,
  49. setStopOnFirstError: _setStopOnFirstError,
  50. setStopOnValidationError: _setStopOnValidationError,
  51. showLogs: _showLogs,
  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 [pdfDownloadUrl] = useDetachStateWatcher(
  148. 'pdfDownloadUrl',
  149. _pdfDownloadUrl,
  150. 'detacher',
  151. 'detached'
  152. )
  153. const [pdfSize] = useDetachStateWatcher(
  154. 'pdfSize',
  155. _pdfSize,
  156. 'detacher',
  157. 'detached'
  158. )
  159. const [pdfUrl] = useDetachStateWatcher(
  160. 'pdfUrl',
  161. _pdfUrl,
  162. 'detacher',
  163. 'detached'
  164. )
  165. const [pdfViewer] = useDetachStateWatcher(
  166. 'pdfViewer',
  167. _pdfViewer,
  168. 'detacher',
  169. 'detached'
  170. )
  171. const [position] = useDetachStateWatcher(
  172. 'position',
  173. _position,
  174. 'detacher',
  175. 'detached'
  176. )
  177. const [rawLog] = useDetachStateWatcher(
  178. 'rawLog',
  179. _rawLog,
  180. 'detacher',
  181. 'detached'
  182. )
  183. const [showLogs] = useDetachStateWatcher(
  184. 'showLogs',
  185. _showLogs,
  186. 'detacher',
  187. 'detached'
  188. )
  189. const [showFasterCompilesFeedbackUI] = useDetachStateWatcher(
  190. 'showFasterCompilesFeedbackUI',
  191. _showFasterCompilesFeedbackUI,
  192. 'detacher',
  193. 'detached'
  194. )
  195. const [stopOnFirstError] = useDetachStateWatcher(
  196. 'stopOnFirstError',
  197. _stopOnFirstError,
  198. 'detacher',
  199. 'detached'
  200. )
  201. const [stopOnValidationError] = useDetachStateWatcher(
  202. 'stopOnValidationError',
  203. _stopOnValidationError,
  204. 'detacher',
  205. 'detached'
  206. )
  207. const [stoppedOnFirstError] = useDetachStateWatcher(
  208. 'stoppedOnFirstError',
  209. _stoppedOnFirstError,
  210. 'detacher',
  211. 'detached'
  212. )
  213. const [uncompiled] = useDetachStateWatcher(
  214. 'uncompiled',
  215. _uncompiled,
  216. 'detacher',
  217. 'detached'
  218. )
  219. const [validationIssues] = useDetachStateWatcher(
  220. 'validationIssues',
  221. _validationIssues,
  222. 'detacher',
  223. 'detached'
  224. )
  225. const setAnimateCompileDropdownArrow = useDetachAction(
  226. 'setAnimateCompileDropdownArrow',
  227. _setAnimateCompileDropdownArrow,
  228. 'detached',
  229. 'detacher'
  230. )
  231. const setAutoCompile = useDetachAction(
  232. 'setAutoCompile',
  233. _setAutoCompile,
  234. 'detached',
  235. 'detacher'
  236. )
  237. const setDraft = useDetachAction(
  238. 'setDraft',
  239. _setDraft,
  240. 'detached',
  241. 'detacher'
  242. )
  243. const setError = useDetachAction(
  244. 'setError',
  245. _setError,
  246. 'detacher',
  247. 'detached'
  248. )
  249. const setPosition = useDetachAction(
  250. 'setPosition',
  251. _setPosition,
  252. 'detached',
  253. 'detacher'
  254. )
  255. const firstRenderDone = useDetachAction(
  256. 'firstRenderDone',
  257. _firstRenderDone,
  258. 'detacher',
  259. 'detached'
  260. )
  261. const setHasLintingError = useDetachAction(
  262. 'setHasLintingError',
  263. _setHasLintingError,
  264. 'detacher',
  265. 'detached'
  266. )
  267. const setHighlights = useDetachAction(
  268. 'setHighlights',
  269. _setHighlights,
  270. 'detacher',
  271. 'detached'
  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,
  358. pdfSize,
  359. 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. setShowLogs,
  373. toggleLogs,
  374. setStopOnFirstError,
  375. setStopOnValidationError,
  376. showLogs,
  377. showFasterCompilesFeedbackUI,
  378. startCompile,
  379. stopCompile,
  380. stopOnFirstError,
  381. stopOnValidationError,
  382. stoppedOnFirstError,
  383. uncompiled,
  384. validationIssues,
  385. firstRenderDone,
  386. setChangedAt,
  387. cleanupCompileResult,
  388. }),
  389. [
  390. animateCompileDropdownArrow,
  391. autoCompile,
  392. clearCache,
  393. clearingCache,
  394. clsiServerId,
  395. codeCheckFailed,
  396. compiling,
  397. deliveryLatencies,
  398. draft,
  399. error,
  400. fileList,
  401. hasChanges,
  402. highlights,
  403. lastCompileOptions,
  404. logEntryAnnotations,
  405. logEntries,
  406. pdfDownloadUrl,
  407. pdfSize,
  408. pdfUrl,
  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. setShowLogs,
  422. toggleLogs,
  423. setStopOnFirstError,
  424. setStopOnValidationError,
  425. showLogs,
  426. showFasterCompilesFeedbackUI,
  427. startCompile,
  428. stopCompile,
  429. stopOnFirstError,
  430. stopOnValidationError,
  431. stoppedOnFirstError,
  432. uncompiled,
  433. validationIssues,
  434. firstRenderDone,
  435. setChangedAt,
  436. cleanupCompileResult,
  437. ]
  438. )
  439. return (
  440. <DetachCompileContext.Provider value={value}>
  441. {children}
  442. </DetachCompileContext.Provider>
  443. )
  444. }
  445. DetachCompileProvider.propTypes = {
  446. children: PropTypes.any,
  447. }
  448. export function useDetachCompileContext(propTypes) {
  449. const data = useContext(DetachCompileContext)
  450. PropTypes.checkPropTypes(
  451. propTypes,
  452. data,
  453. 'data',
  454. 'DetachCompileContext.Provider'
  455. )
  456. return data
  457. }