The best way to control Claude Code, Codex and more from any computer or phone

main
ts 112 lines 4.02 KB
Raw
1 /**
2 * React helpers for applying dehydrated Effect Atom state to a React subtree.
3 * The `HydrationBoundary` component reads the nearest `RegistryContext`,
4 * hydrates new Atom values before children render, and delays updates for
5 * existing Atom values until after commit so React transitions do not update
6 * the current UI too early.
7 *
8 * @stability unstable
9 * @since 4.0.0
10 */
11 "use client"
12 import * as Hydration from "effect/reactivity/Hydration"
13 import * as React from "react"
14 import { RegistryContext } from "./RegistryContext.ts"
15
16 /**
17 * Props for a boundary that applies dehydrated Atom values to the nearest
18 * {@link RegistryContext} while rendering its children.
19 *
20 * @stability unstable
21 * @category components
22 * @since 4.0.0
23 */
24 export interface HydrationBoundaryProps {
25 state?: Iterable<Hydration.DehydratedAtom>
26 children?: React.ReactNode
27 }
28
29 /**
30 * Provides a React hydration boundary that loads dehydrated Atom values into
31 * the current Atom registry.
32 *
33 * **When to use**
34 *
35 * Use to apply dehydrated Atom state to a React subtree that reads from the
36 * nearest `RegistryContext`.
37 *
38 * **Details**
39 *
40 * New Atom values are hydrated during render so descendants can read them
41 * immediately, while values for existing Atoms are deferred until after commit
42 * so transition data does not update the current UI before React accepts it.
43 *
44 * @see {@link Hydration.dehydrate} for producing dehydrated Atom state
45 * @see {@link Hydration.hydrate} for lower-level non-React hydration
46 *
47 * @stability unstable
48 * @category components
49 * @since 4.0.0
50 */
51 export const HydrationBoundary: React.FC<HydrationBoundaryProps> = ({
52 children,
53 state
54 }) => {
55 const registry = React.useContext(RegistryContext)
56
57 // This useMemo is for performance reasons only, everything inside it must
58 // be safe to run in every render and code here should be read as "in render".
59 //
60 // This code needs to happen during the render phase, because after initial
61 // SSR, hydration needs to happen _before_ children render. Also, if hydrating
62 // during a transition, we want to hydrate as much as is safe in render so
63 // we can prerender as much as possible.
64 //
65 // For any Atom values that already exist in the registry, we want to hold back on
66 // hydrating until _after_ the render phase. The reason for this is that during
67 // transitions, we don't want the existing Atom values and subscribers to update to
68 // the new data on the current page, only _after_ the transition is committed.
69 // If the transition is aborted, we will have hydrated any _new_ Atom values, but
70 // we throw away the fresh data for any existing ones to avoid unexpectedly
71 // updating the UI.
72 const hydrationQueue: Array<Hydration.DehydratedAtomValue> | undefined = React.useMemo(() => {
73 if (state) {
74 const dehydratedAtoms = Array.from(state) as Array<Hydration.DehydratedAtomValue>
75 const nodes = registry.getNodes()
76
77 const newDehydratedAtoms: Array<Hydration.DehydratedAtomValue> = []
78 const existingDehydratedAtoms: Array<Hydration.DehydratedAtomValue> = []
79
80 for (const dehydratedAtom of dehydratedAtoms) {
81 const existingNode = nodes.get(dehydratedAtom.key)
82
83 if (!existingNode) {
84 // This is a new Atom value, safe to hydrate immediately
85 newDehydratedAtoms.push(dehydratedAtom)
86 } else {
87 // This Atom value already exists, queue it for later hydration
88 existingDehydratedAtoms.push(dehydratedAtom)
89 }
90 }
91
92 if (newDehydratedAtoms.length > 0) {
93 // It's actually fine to call this with state that already exists
94 // in the registry, or is older. hydrate() is idempotent.
95 Hydration.hydrate(registry, newDehydratedAtoms)
96 }
97
98 if (existingDehydratedAtoms.length > 0) {
99 return existingDehydratedAtoms
100 }
101 }
102 return undefined
103 }, [registry, state])
104
105 React.useEffect(() => {
106 if (hydrationQueue) {
107 Hydration.hydrate(registry, hydrationQueue)
108 }
109 }, [registry, hydrationQueue])
110
111 return React.createElement(React.Fragment, {}, children)
112 }