@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 112 lines 3.65 KB
Raw
1 /**
2 * React context and provider for the Atom registry used by Effect Atom hooks.
3 * The registry stores atom values, schedules update work, and cleans up unused
4 * atoms. Sharing one registry through React context lets components in the same
5 * subtree read and write the same atom state.
6 *
7 * @stability unstable
8 * @since 4.0.0
9 */
10 "use client"
11
12 import type * as Atom from "effect/reactivity/Atom"
13 import * as AtomRegistry from "effect/reactivity/AtomRegistry"
14 import * as React from "react"
15 import * as Scheduler from "scheduler"
16
17 /**
18 * Schedules Atom registry work with React's scheduler at low priority and
19 * returns a cancellation function for the scheduled task.
20 *
21 * @stability unstable
22 * @category context
23 * @since 4.0.0
24 */
25 export function scheduleTask(f: () => void): () => void {
26 const node = Scheduler.unstable_scheduleCallback(Scheduler.unstable_LowPriority, f)
27 return () => Scheduler.unstable_cancelCallback(node)
28 }
29
30 /**
31 * Provides a React context that supplies the `AtomRegistry` used by Atom hooks and
32 * hydration helpers, defaulting to a standalone registry when no provider is
33 * present.
34 *
35 * **When to use**
36 *
37 * Use to supply an existing `AtomRegistry` through React context when hooks or
38 * hydration helpers need to share registry state that is managed outside
39 * `RegistryProvider`.
40 *
41 * @see {@link RegistryProvider} for creating and providing a registry for a React subtree
42 *
43 * @stability unstable
44 * @category context
45 * @since 4.0.0
46 */
47 export const RegistryContext = React.createContext<AtomRegistry.AtomRegistry>(AtomRegistry.make({
48 scheduleTask,
49 defaultIdleTTL: 400
50 }))
51
52 /**
53 * Provides a stable `AtomRegistry` to a React subtree, optionally seeding
54 * initial atom values and overriding registry scheduling or idle settings.
55 *
56 * **When to use**
57 *
58 * Use to scope atom state, scheduling, and idle cleanup to a React subtree.
59 *
60 * **Details**
61 *
62 * The provider creates one `AtomRegistry` with `AtomRegistry.make`, passes it
63 * through `RegistryContext.Provider`, and forwards `initialValues`,
64 * `scheduleTask`, `timeoutResolution`, and `defaultIdleTTL` only when that
65 * registry is created.
66 *
67 * **Gotchas**
68 *
69 * Option changes after the first render do not rebuild the registry. When the
70 * provider unmounts, registry disposal is delayed briefly and canceled if the
71 * provider remounts before the timeout fires.
72 *
73 * @see {@link RegistryContext} for the React context supplied by this provider
74 *
75 * @stability unstable
76 * @category context
77 * @since 4.0.0
78 */
79 export const RegistryProvider = (options: {
80 readonly children?: React.ReactNode | undefined
81 readonly initialValues?: Iterable<readonly [Atom.Atom<any>, any, Atom.InitialValueOptions?]> | undefined
82 readonly scheduleTask?: ((f: () => void) => () => void) | undefined
83 readonly timeoutResolution?: number | undefined
84 readonly defaultIdleTTL?: number | undefined
85 }) => {
86 const ref = React.useRef<{
87 readonly registry: AtomRegistry.AtomRegistry
88 timeout?: number | undefined
89 }>(null)
90 if (ref.current === null) {
91 ref.current = {
92 registry: AtomRegistry.make({
93 scheduleTask: options.scheduleTask ?? scheduleTask,
94 initialValues: options.initialValues,
95 timeoutResolution: options.timeoutResolution,
96 defaultIdleTTL: options.defaultIdleTTL
97 })
98 }
99 }
100 React.useEffect(() => {
101 if (ref.current?.timeout !== undefined) {
102 clearTimeout(ref.current.timeout)
103 }
104 return () => {
105 ref.current!.timeout = setTimeout(() => {
106 ref.current?.registry.dispose()
107 ref.current = null
108 }, 500) as any
109 }
110 }, [ref])
111 return React.createElement(RegistryContext.Provider, { value: ref.current.registry }, options?.children)
112 }