Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

RegistryContext.ts

83 lines2.8 KB

1/**
2 * Solid 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 Solid context lets components and
5 * computations in the same owner tree read and write the same atom state.
6 *
7 * @stability unstable
8 * @since 4.0.0
9 */
10import type * as Atom from "effect/reactivity/Atom"
11import * as AtomRegistry from "effect/reactivity/AtomRegistry"
12import type { JSX } from "solid-js"
13import { createComponent, createContext, onCleanup } from "solid-js"
14​
15/**
16 * Provides a Solid context that carries the `AtomRegistry` used by atom hooks in the
17 * current owner tree.
18 *
19 * **When to use**
20 *
21 * Use when you need to integrate lower-level Solid context APIs, such as custom
22 * providers or hooks, instead of using the default atom hook setup.
23 *
24 * **Details**
25 *
26 * When no provider is present, the context uses a standalone default registry.
27 *
28 * @see {@link RegistryProvider} for creating and providing a registry for a Solid subtree
29 *
30 * @stability unstable
31 * @category context
32 * @since 4.0.0
33 */
34export const RegistryContext = createContext<AtomRegistry.AtomRegistry>(AtomRegistry.make())
35​
36/**
37 * Creates an `AtomRegistry` for a Solid subtree, optionally seeding initial atom
38 * values and scheduler settings, and disposes the registry when the owner is
39 * cleaned up.
40 *
41 * **When to use**
42 *
43 * Use to scope atom state, scheduling, and cleanup to a Solid subtree.
44 *
45 * **Details**
46 *
47 * The provider creates an `AtomRegistry` with `AtomRegistry.make`, forwards
48 * `initialValues`, `scheduleTask`, `timeoutResolution`, and
49 * `defaultIdleTTL`, and supplies the registry through `RegistryContext`.
50 *
51 * **Gotchas**
52 *
53 * Provider options are consumed when the registry is created; they are not
54 * reactive updates. A custom `scheduleTask` should return a cancellation
55 * function that is safe to call during Solid cleanup.
56 *
57 * @see {@link RegistryContext} for the context supplied by this provider
58 *
59 * @stability unstable
60 * @category context
61 * @since 4.0.0
62 */
63export const RegistryProvider = (options: {
64 readonly children?: JSX.Element | undefined
65 readonly initialValues?: Iterable<readonly [Atom.Atom<any>, any, Atom.InitialValueOptions?]> | undefined
66 readonly scheduleTask?: ((f: () => void) => () => void) | undefined
67 readonly timeoutResolution?: number | undefined
68 readonly defaultIdleTTL?: number | undefined
69}) => {
70 const registry = AtomRegistry.make({
71 scheduleTask: options.scheduleTask,
72 initialValues: options.initialValues,
73 timeoutResolution: options.timeoutResolution,
74 defaultIdleTTL: options.defaultIdleTTL
75 })
76 onCleanup(() => registry.dispose())
77 return createComponent(RegistryContext.Provider, {
78 value: registry,
79 get children() {
80 return options.children
81 }
82 })