boot.tsx 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * Web shell boot — the library face consumed by the apps/web entry (api
  3. * contracts v3 §0.3/§9.3): root ctx → hold the loader machinery (statically
  4. * imported; the loader cannot load itself) → seed the module table → render
  5. * the AppRoot loading page → loader.start() → await settled() → flip the
  6. * settled signal so AppRoot switches to the real UI in one pass. Load
  7. * failures reject settled(); AppRoot stays on the loading page listing them
  8. * (fail loud).
  9. */
  10. import { Context } from 'cordis'
  11. import { createRoot } from 'react-dom/client'
  12. import type { ReactNode } from 'react'
  13. import type { ObservableSnapshot } from '@deepseek-ai/dsh-client-runtime/client'
  14. import { createSlotRenderer } from '@deepseek-ai/dsh-client-web-react'
  15. import { createClientLoader, type ClientLoaderOptions } from '@deepseek-ai/dsh-client-runtime/loader'
  16. import { AppRoot } from './AppRoot.tsx'
  17. import { buildRenderApp } from './app.tsx'
  18. import { seedModules } from './seed.ts'
  19. import './base.css'
  20. /** Manually flipped settled signal (AppRoot's gate; see AppRootProps.settled). */
  21. function settledSignal(): ObservableSnapshot<boolean> & { flip: () => void } {
  22. let value = false
  23. const listeners = new Set<() => void>()
  24. return {
  25. getSnapshot: () => value,
  26. subscribe: (fn) => { listeners.add(fn); return () => { listeners.delete(fn) } },
  27. flip: () => {
  28. value = true
  29. for (const fn of [...listeners]) fn()
  30. },
  31. }
  32. }
  33. /** Loader transport seams the shell passes through (jsdom tests replace the <script> path). */
  34. export type BootSeams = Pick<ClientLoaderOptions, 'fetchBundle' | 'executeBundle'>
  35. /**
  36. * Mount the web shell into a DOM element and start the plugin load chain.
  37. * @param el - mount point (the app's #root).
  38. * @param seams - optional loader transport overrides (test environments).
  39. * @returns unmount disposer.
  40. */
  41. export function bootWebShell(el: HTMLElement, seams?: BootSeams): () => void {
  42. const ctx = new Context()
  43. const loader = createClientLoader({ ctx, modules: seedModules(), ...seams })
  44. ctx.reflect.provide('loader', loader)
  45. const settled = settledSignal()
  46. // Assemble once on first post-settled render: SessionProvider and the slot
  47. // closures must be identity-stable across re-renders.
  48. let renderApp: (() => ReactNode) | undefined
  49. const renderAppOnce = (): ReactNode => {
  50. renderApp ??= buildRenderApp({ ctx, requireModule: (spec) => loader.requireModule(spec) })
  51. return renderApp()
  52. }
  53. const root = createRoot(el)
  54. root.render(<AppRoot settled={settled} status={loader.status} renderApp={renderAppOnce} />)
  55. loader.start()
  56. loader.settled().then(
  57. () => {
  58. // The renderer install is a shell-boot act, but ctx.slots exists only
  59. // once the runtime plugin loaded — so it lands here, after settled and
  60. // before the flip that lets renderApp call renderSlot('root').
  61. ctx.slots.install(createSlotRenderer())
  62. settled.flip()
  63. },
  64. () => { /* stay on the loading page; failures render from loader.status */ },
  65. )
  66. return () => { root.unmount() }
  67. }