detach-compile-context.js 8.2 KB

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