@effect/atom-solid 4.0.0-beta.7 → 4.0.0-beta.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Hooks.d.ts CHANGED
@@ -1,73 +1,117 @@
1
1
  import * as Exit from "effect/Exit";
2
- import type * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
2
+ import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
3
3
  import * as Atom from "effect/unstable/reactivity/Atom";
4
4
  import type * as AtomRef from "effect/unstable/reactivity/AtomRef";
5
- import type { Accessor } from "solid-js";
5
+ import type { Accessor, ResourceOptions, ResourceReturn } from "solid-js";
6
6
  /**
7
- * @since 1.0.0
7
+ * Seeds initial atom values in the current Solid atom registry.
8
+ *
9
+ * **Details**
10
+ *
11
+ * Each atom is initialized at most once for a given registry, so subsequent
12
+ * computations do not overwrite values that have already been established.
13
+ *
8
14
  * @category hooks
15
+ * @since 4.0.0
9
16
  */
10
17
  export declare const useAtomInitialValues: (initialValues: Iterable<readonly [Atom.Atom<any>, any]>) => void;
11
18
  /**
12
- * @since 1.0.0
19
+ * Subscribes to an atom in the current Solid registry and returns its value as
20
+ * a Solid accessor.
21
+ *
13
22
  * @category hooks
23
+ * @since 4.0.0
14
24
  */
15
25
  export declare const useAtomValue: {
16
26
  /**
17
- * @since 1.0.0
27
+ * Subscribes to an atom in the current Solid registry and returns its value as
28
+ * a Solid accessor.
29
+ *
18
30
  * @category hooks
31
+ * @since 4.0.0
19
32
  */
20
- <A>(atom: Atom.Atom<A>): Accessor<A>;
33
+ <A>(atom: () => Atom.Atom<A>): Accessor<A>;
21
34
  /**
22
- * @since 1.0.0
35
+ * Subscribes to an atom in the current Solid registry and returns its value as
36
+ * a Solid accessor.
37
+ *
23
38
  * @category hooks
39
+ * @since 4.0.0
24
40
  */
25
- <A, B>(atom: Atom.Atom<A>, f: (_: A) => B): Accessor<B>;
41
+ <A, B>(atom: () => Atom.Atom<A>, f: (_: A) => B): Accessor<B>;
26
42
  };
27
43
  /**
28
- * @since 1.0.0
44
+ * Mounts an atom in the current Solid registry for the lifetime of the current
45
+ * Solid computation.
46
+ *
29
47
  * @category hooks
48
+ * @since 4.0.0
30
49
  */
31
- export declare const useAtomMount: <A>(atom: Atom.Atom<A>) => void;
50
+ export declare const useAtomMount: <A>(atom: () => Atom.Atom<A>) => void;
32
51
  /**
33
- * @since 1.0.0
52
+ * Returns a setter for a writable atom without subscribing to its value.
53
+ *
34
54
  * @category hooks
55
+ * @since 4.0.0
35
56
  */
36
- export declare const useAtomSet: <R, W, Mode extends "value" | "promise" | "promiseExit" = never>(atom: Atom.Writable<R, W>, options?: {
57
+ export declare const useAtomSet: <R, W, Mode extends "value" | "promise" | "promiseExit" = never>(atom: () => Atom.Writable<R, W>, options?: {
37
58
  readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined;
38
59
  }) => "promise" extends Mode ? ((value: W) => Promise<AsyncResult.AsyncResult.Success<R>>) : "promiseExit" extends Mode ? ((value: W) => Promise<Exit.Exit<AsyncResult.AsyncResult.Success<R>, AsyncResult.AsyncResult.Failure<R>>>) : ((value: W | ((value: R) => W)) => void);
39
60
  /**
40
- * @since 1.0.0
61
+ * Mounts an atom and returns a callback that refreshes the current atom.
62
+ *
41
63
  * @category hooks
64
+ * @since 4.0.0
42
65
  */
43
- export declare const useAtomRefresh: <A>(atom: Atom.Atom<A>) => () => void;
66
+ export declare const useAtomRefresh: <A>(atom: () => Atom.Atom<A>) => () => void;
44
67
  /**
45
- * @since 1.0.0
68
+ * Returns a Solid accessor for a writable atom together with a setter for
69
+ * updating it.
70
+ *
46
71
  * @category hooks
72
+ * @since 4.0.0
47
73
  */
48
- export declare const useAtom: <R, W, const Mode extends "value" | "promise" | "promiseExit" = never>(atom: Atom.Writable<R, W>, options?: {
74
+ export declare const useAtom: <R, W, const Mode extends "value" | "promise" | "promiseExit" = never>(atom: () => Atom.Writable<R, W>, options?: {
49
75
  readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined;
50
76
  }) => readonly [value: Accessor<R>, write: "promise" extends Mode ? ((value: W) => Promise<AsyncResult.AsyncResult.Success<R>>) : "promiseExit" extends Mode ? ((value: W) => Promise<Exit.Exit<AsyncResult.AsyncResult.Success<R>, AsyncResult.AsyncResult.Failure<R>>>) : ((value: W | ((value: R) => W)) => void)];
51
77
  /**
52
- * @since 1.0.0
78
+ * Subscribes a callback to an atom in the current Solid registry.
79
+ *
53
80
  * @category hooks
81
+ * @since 4.0.0
54
82
  */
55
- export declare const useAtomSubscribe: <A>(atom: Atom.Atom<A>, f: (_: A) => void, options?: {
83
+ export declare const useAtomSubscribe: <A>(atom: () => Atom.Atom<A>, f: (_: A) => void, options?: {
56
84
  readonly immediate?: boolean;
57
85
  }) => void;
58
86
  /**
59
- * @since 1.0.0
87
+ * Converts an `AsyncResult` atom into a Solid resource.
88
+ *
60
89
  * @category hooks
90
+ * @since 4.0.0
61
91
  */
62
- export declare const useAtomRef: <A>(ref: AtomRef.ReadonlyRef<A>) => Accessor<A>;
92
+ export declare const useAtomResource: <A, E>(atom: () => Atom.Atom<AsyncResult.AsyncResult<A, E>>, options?: ResourceOptions<A> & {
93
+ readonly suspendOnWaiting?: boolean | undefined;
94
+ }) => ResourceReturn<A, void>;
63
95
  /**
64
- * @since 1.0.0
96
+ * Subscribes to an atom ref and returns its value as a Solid accessor.
97
+ *
65
98
  * @category hooks
99
+ * @since 4.0.0
66
100
  */
67
- export declare const useAtomRefProp: <A, K extends keyof A>(ref: AtomRef.AtomRef<A>, prop: K) => AtomRef.AtomRef<A[K]>;
101
+ export declare const useAtomRef: <A>(ref: () => AtomRef.ReadonlyRef<A>) => Accessor<A>;
68
102
  /**
69
- * @since 1.0.0
103
+ * Returns a Solid accessor for a property ref derived from an atom ref.
104
+ *
70
105
  * @category hooks
106
+ * @since 4.0.0
71
107
  */
72
- export declare const useAtomRefPropValue: <A, K extends keyof A>(ref: AtomRef.AtomRef<A>, prop: K) => Accessor<A[K]>;
108
+ export declare const useAtomRefProp: <A, K extends keyof A>(ref: () => AtomRef.AtomRef<A>, prop: K) => Accessor<AtomRef.AtomRef<A[K]>>;
109
+ /**
110
+ * Returns a Solid accessor for the value of a property ref derived from an atom
111
+ * ref.
112
+ *
113
+ * @category hooks
114
+ * @since 4.0.0
115
+ */
116
+ export declare const useAtomRefPropValue: <A, K extends keyof A>(ref: () => AtomRef.AtomRef<A>, prop: K) => Accessor<A[K]>;
73
117
  //# sourceMappingURL=Hooks.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Hooks.d.ts","sourceRoot":"","sources":["../src/Hooks.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,IAAI,MAAM,aAAa,CAAA;AACnC,OAAO,KAAK,KAAK,WAAW,MAAM,wCAAwC,CAAA;AAC1E,OAAO,KAAK,IAAI,MAAM,iCAAiC,CAAA;AACvD,OAAO,KAAK,KAAK,OAAO,MAAM,oCAAoC,CAAA;AAElE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AAMxC;;;GAGG;AACH,eAAO,MAAM,oBAAoB,GAAI,eAAe,QAAQ,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC,KAAG,IAa9F,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,YAAY,EAAE;IACzB;;;OAGG;IACH,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAA;IACpC;;;OAGG;IACH,CAAC,CAAC,EAAE,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAA;CAIxD,CAAA;AA+CD;;;GAGG;AACH,eA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,CAAC,CAAC,GAAG;IAC7B,QAAQ,CAAC,gBAAgB,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;CAChD,KACA,cAAc,CAAC,CAAC,EAAE,IAAI,CAUxB,CAAA;AAID;;;;;GAKG;AACH,eAAO,MAAM,UAAU,GAAI,CAAC,EAAE,KAAK,MAAM,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,KAAG,QAAQ,CAAC,CAAC,CAQ3E,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,cAAc,GAAI,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,EACjD,KAAK,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,EAC7B,MAAM,CAAC,KACN,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAuC,CAAA;AAExE;;;;;;GAMG;AACH,eAAO,MAAM,mBAAmB,GAAI,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,EAAE,KAAK,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,KAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CACzE,CAAA"}
package/dist/Hooks.js CHANGED
@@ -1,17 +1,46 @@
1
1
  /**
2
- * @since 1.0.0
2
+ * The `Hooks` module provides SolidJS hooks for reading, writing, mounting, and
3
+ * subscribing to Effect atoms through the current Solid atom registry.
4
+ *
5
+ * **Common tasks**
6
+ *
7
+ * - Read an atom as a Solid accessor with {@link useAtomValue}
8
+ * - Read and write a writable atom with {@link useAtom}
9
+ * - Write without subscribing to the value with {@link useAtomSet}
10
+ * - Refresh or mount atoms from components with {@link useAtomRefresh} and {@link useAtomMount}
11
+ * - Convert `AsyncResult` atoms into Solid resources with {@link useAtomResource}
12
+ * - Work with atom refs and nested ref properties with {@link useAtomRef}, {@link useAtomRefProp},
13
+ * and {@link useAtomRefPropValue}
14
+ *
15
+ * **Solid integration notes**
16
+ *
17
+ * Hooks in this module read the registry from {@link RegistryContext}, so they
18
+ * should be used under the matching provider for the atom graph you want to
19
+ * observe. Atom arguments are thunks so Solid can track dynamic atom selection;
20
+ * subscriptions are registered in Solid computations and disposed with
21
+ * `onCleanup` when the computation changes or the component unmounts.
22
+ *
23
+ * @since 4.0.0
3
24
  */
4
25
  import * as Cause from "effect/Cause";
5
26
  import * as Effect from "effect/Effect";
6
27
  import * as Exit from "effect/Exit";
28
+ import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
7
29
  import * as Atom from "effect/unstable/reactivity/Atom";
8
30
  import * as AtomRegistry from "effect/unstable/reactivity/AtomRegistry";
9
- import { createSignal, onCleanup, useContext } from "solid-js";
31
+ import { createComputed, createEffect, createMemo, createResource, createSignal, onCleanup, useContext } from "solid-js";
10
32
  import { RegistryContext } from "./RegistryContext.js";
11
33
  const initialValuesSet = /*#__PURE__*/new WeakMap();
12
34
  /**
13
- * @since 1.0.0
35
+ * Seeds initial atom values in the current Solid atom registry.
36
+ *
37
+ * **Details**
38
+ *
39
+ * Each atom is initialized at most once for a given registry, so subsequent
40
+ * computations do not overwrite values that have already been established.
41
+ *
14
42
  * @category hooks
43
+ * @since 4.0.0
15
44
  */
16
45
  export const useAtomInitialValues = initialValues => {
17
46
  const registry = useContext(RegistryContext);
@@ -28,33 +57,44 @@ export const useAtomInitialValues = initialValues => {
28
57
  }
29
58
  };
30
59
  /**
31
- * @since 1.0.0
60
+ * Subscribes to an atom in the current Solid registry and returns its value as
61
+ * a Solid accessor.
62
+ *
32
63
  * @category hooks
64
+ * @since 4.0.0
33
65
  */
34
66
  export const useAtomValue = (atom, f) => {
35
67
  const registry = useContext(RegistryContext);
36
- return createAtomAccessor(registry, f ? Atom.map(atom, f) : atom);
68
+ return createAtomAccessor(registry, f ? () => Atom.map(atom(), f) : atom);
37
69
  };
38
70
  function createAtomAccessor(registry, atom) {
39
- const [value, setValue] = createSignal(registry.get(atom));
40
- onCleanup(registry.subscribe(atom, setValue));
71
+ const [value, setValue] = createSignal(null);
72
+ createComputed(() => {
73
+ onCleanup(registry.subscribe(atom(), setValue, constImmediate));
74
+ });
41
75
  return value;
42
76
  }
77
+ const constImmediate = {
78
+ immediate: true
79
+ };
43
80
  function mountAtom(registry, atom) {
44
- onCleanup(registry.mount(atom));
81
+ createComputed(() => {
82
+ onCleanup(registry.mount(atom()));
83
+ });
45
84
  }
46
85
  function setAtom(registry, atom, options) {
86
+ const memo = createMemo(atom);
47
87
  if (options?.mode === "promise" || options?.mode === "promiseExit") {
48
88
  return value => {
49
- registry.set(atom, value);
50
- const promise = Effect.runPromiseExit(AtomRegistry.getResult(registry, atom, {
89
+ registry.set(memo(), value);
90
+ const promise = Effect.runPromiseExit(AtomRegistry.getResult(registry, memo(), {
51
91
  suspendOnWaiting: true
52
92
  }));
53
93
  return options.mode === "promise" ? promise.then(flattenExit) : promise;
54
94
  };
55
95
  }
56
96
  return value => {
57
- registry.set(atom, typeof value === "function" ? value(registry.get(atom)) : value);
97
+ registry.set(memo(), typeof value === "function" ? value(registry.get(memo())) : value);
58
98
  };
59
99
  }
60
100
  const flattenExit = exit => {
@@ -62,16 +102,21 @@ const flattenExit = exit => {
62
102
  throw Cause.squash(exit.cause);
63
103
  };
64
104
  /**
65
- * @since 1.0.0
105
+ * Mounts an atom in the current Solid registry for the lifetime of the current
106
+ * Solid computation.
107
+ *
66
108
  * @category hooks
109
+ * @since 4.0.0
67
110
  */
68
111
  export const useAtomMount = atom => {
69
112
  const registry = useContext(RegistryContext);
70
113
  mountAtom(registry, atom);
71
114
  };
72
115
  /**
73
- * @since 1.0.0
116
+ * Returns a setter for a writable atom without subscribing to its value.
117
+ *
74
118
  * @category hooks
119
+ * @since 4.0.0
75
120
  */
76
121
  export const useAtomSet = (atom, options) => {
77
122
  const registry = useContext(RegistryContext);
@@ -79,47 +124,86 @@ export const useAtomSet = (atom, options) => {
79
124
  return setAtom(registry, atom, options);
80
125
  };
81
126
  /**
82
- * @since 1.0.0
127
+ * Mounts an atom and returns a callback that refreshes the current atom.
128
+ *
83
129
  * @category hooks
130
+ * @since 4.0.0
84
131
  */
85
132
  export const useAtomRefresh = atom => {
86
133
  const registry = useContext(RegistryContext);
87
134
  mountAtom(registry, atom);
88
- return () => registry.refresh(atom);
135
+ const memo = createMemo(atom);
136
+ return () => registry.refresh(memo());
89
137
  };
90
138
  /**
91
- * @since 1.0.0
139
+ * Returns a Solid accessor for a writable atom together with a setter for
140
+ * updating it.
141
+ *
92
142
  * @category hooks
143
+ * @since 4.0.0
93
144
  */
94
145
  export const useAtom = (atom, options) => {
95
146
  const registry = useContext(RegistryContext);
96
147
  return [createAtomAccessor(registry, atom), setAtom(registry, atom, options)];
97
148
  };
98
149
  /**
99
- * @since 1.0.0
150
+ * Subscribes a callback to an atom in the current Solid registry.
151
+ *
100
152
  * @category hooks
153
+ * @since 4.0.0
101
154
  */
102
155
  export const useAtomSubscribe = (atom, f, options) => {
103
156
  const registry = useContext(RegistryContext);
104
- onCleanup(registry.subscribe(atom, f, options));
157
+ createEffect(() => {
158
+ onCleanup(registry.subscribe(atom(), f, options));
159
+ });
160
+ };
161
+ /**
162
+ * Converts an `AsyncResult` atom into a Solid resource.
163
+ *
164
+ * @category hooks
165
+ * @since 4.0.0
166
+ */
167
+ export const useAtomResource = (atom, options) => {
168
+ const result = useAtomValue(atom);
169
+ return createResource(result, result => {
170
+ if (AsyncResult.isInitial(result) || options?.suspendOnWaiting && result.waiting) {
171
+ return constUnresolvedPromise;
172
+ } else if (AsyncResult.isSuccess(result)) {
173
+ return Promise.resolve(result.value);
174
+ }
175
+ return Promise.reject(Cause.squash(result.cause));
176
+ });
105
177
  };
178
+ const constUnresolvedPromise = /*#__PURE__*/new Promise(() => {});
106
179
  /**
107
- * @since 1.0.0
180
+ * Subscribes to an atom ref and returns its value as a Solid accessor.
181
+ *
108
182
  * @category hooks
183
+ * @since 4.0.0
109
184
  */
110
185
  export const useAtomRef = ref => {
111
- const [value, setValue] = createSignal(ref.value);
112
- onCleanup(ref.subscribe(setValue));
186
+ const [value, setValue] = createSignal(null);
187
+ createComputed(() => {
188
+ const r = ref();
189
+ setValue(r.value);
190
+ onCleanup(r.subscribe(setValue));
191
+ });
113
192
  return value;
114
193
  };
115
194
  /**
116
- * @since 1.0.0
195
+ * Returns a Solid accessor for a property ref derived from an atom ref.
196
+ *
117
197
  * @category hooks
198
+ * @since 4.0.0
118
199
  */
119
- export const useAtomRefProp = (ref, prop) => ref.prop(prop);
200
+ export const useAtomRefProp = (ref, prop) => createMemo(() => ref().prop(prop));
120
201
  /**
121
- * @since 1.0.0
202
+ * Returns a Solid accessor for the value of a property ref derived from an atom
203
+ * ref.
204
+ *
122
205
  * @category hooks
206
+ * @since 4.0.0
123
207
  */
124
208
  export const useAtomRefPropValue = (ref, prop) => useAtomRef(useAtomRefProp(ref, prop));
125
209
  //# sourceMappingURL=Hooks.js.map
package/dist/Hooks.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"Hooks.js","names":["Cause","Effect","Exit","Atom","AtomRegistry","createSignal","onCleanup","useContext","RegistryContext","initialValuesSet","WeakMap","useAtomInitialValues","initialValues","registry","set","get","undefined","WeakSet","atom","value","has","add","ensureNode","setValue","useAtomValue","f","createAtomAccessor","map","subscribe","mountAtom","mount","setAtom","options","mode","promise","runPromiseExit","getResult","suspendOnWaiting","then","flattenExit","exit","isSuccess","squash","cause","useAtomMount","useAtomSet","useAtomRefresh","refresh","useAtom","useAtomSubscribe","useAtomRef","ref","useAtomRefProp","prop","useAtomRefPropValue"],"sources":["../src/Hooks.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAGA,OAAO,KAAKA,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,IAAI,MAAM,aAAa;AAEnC,OAAO,KAAKC,IAAI,MAAM,iCAAiC;AAEvD,OAAO,KAAKC,YAAY,MAAM,yCAAyC;AAEvE,SAASC,YAAY,EAAEC,SAAS,EAAEC,UAAU,QAAQ,U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AGA,CAAuBD,GAAuB,EAAEE,IAAO,KACnFF,GAAG,CAACE,IAAI,CAACA,IAAI,CAAC;AAEhB;;;;AAIA,OAAO,MAAMC,mBAAmB,GAAGA,CAAuBH,GAAuB,EAAEE,IAAO,KACxFH,UAAU,CAACE,cAAc,CAACD,GAAG,EAAEE,IAAI,CAAC,CAAC","ignoreList":[]}
1
+ {"version":3,"file":"Hooks.js","names":["Cause","Effect","Exit","AsyncResult","Atom","AtomRegistry","createComputed","createEffect","createMemo","createResource","createSignal","onCleanup","useContext","RegistryContext","initialValuesSet","WeakMap","useAtomInitialValues","initialValues","registry","set","get","undefined","WeakSet","atom","value","has","add","ensureNode","setValue","useAtomValue","f","createAtomAccessor","map","subscribe","constImmediate","immediate","mountAtom","mount","setAtom","options","memo","mode","promise","runPromiseExit","getResult","suspendOnWaiting","then","flattenExit","exit","isSuccess","squash","cause","useAtomMount","useAtomSet","useAtomRefresh","refresh","useAtom","useAtomSubscribe","useAtomResource","result","isInitial","waiting","constUnresolvedPromise","Promise","resolve","reject","useAtomRef","ref","r","useAtomRefProp","prop","useAtomRefPropValue"],"sources":["../src/Hooks.ts"],"sourcesContent":[null],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;AAwBA,OAAO,KAAKA,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,OAAO,KAAKC,IAAI,MAAM,aAAa;AACnC,OAAO,KAAKC,WAAW,MAAM,wCAAwC;AACrE,OAAO,KAAKC,IAAI,MAAM,iCAAiC;AAEvD,OAAO,KAAKC,YAAY,MAAM,yCAAyC;AAEvE,SAASC,cAAc,EAAEC,YAAY,EAAEC,UAAU,EAAEC,cAAc,EAAEC,YAAY,EAAEC,SAAS,EAAEC,UAAU,QAAQ,UAAU;AACxH,SAASC,eAAe,QAAQ,sBAAsB;AAEtD,MAAMC,gBAAgB,gBAAG,IAAIC,OAAO,EAAsD;AAE1F;;;;;;;;;;;AAWA,OAAO,MAAMC,o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,UAAU,CAACG,cAAc,CAACF,GAAG,EAAEG,IAAI,CAAC,CAAC","ignoreList":[]}
@@ -1,17 +1,46 @@
1
1
  /**
2
- * @since 1.0.0
2
+ * The `RegistryContext` module provides the Solid context used by Effect Atom
3
+ * hooks to share an `AtomRegistry` across an owner tree. The registry owns atom
4
+ * state, scheduling, and idle cleanup, so Solid components that read or write
5
+ * atoms coordinate through the same runtime instead of creating isolated
6
+ * registries.
7
+ *
8
+ * **Common tasks**
9
+ *
10
+ * - Use {@link RegistryProvider} to scope atom state to a Solid subtree
11
+ * - Seed atoms for tests, examples, or server-provided data with `initialValues`
12
+ * - Override scheduling or idle timing for custom rendering environments
13
+ * - Read {@link RegistryContext} when integrating lower-level atom APIs
14
+ *
15
+ * **Gotchas**
16
+ *
17
+ * - The provider creates a registry for the current Solid owner and disposes it
18
+ * with `onCleanup`
19
+ * - `initialValues` are applied when the provider creates the registry, not as
20
+ * reactive updates after creation
21
+ * - Overriding `scheduleTask` changes when atom work is flushed, so it should
22
+ * return a cancellation function that is safe to call during Solid cleanup
23
+ *
24
+ * @since 4.0.0
3
25
  */
4
26
  import type * as Atom from "effect/unstable/reactivity/Atom";
5
27
  import * as AtomRegistry from "effect/unstable/reactivity/AtomRegistry";
6
28
  import type { JSX } from "solid-js";
7
29
  /**
8
- * @since 1.0.0
30
+ * A Solid context that carries the `AtomRegistry` used by atom hooks in the
31
+ * current owner tree.
32
+ *
9
33
  * @category context
34
+ * @since 4.0.0
10
35
  */
11
36
  export declare const RegistryContext: import("solid-js").Context<AtomRegistry.AtomRegistry>;
12
37
  /**
13
- * @since 1.0.0
38
+ * Creates an `AtomRegistry` for a Solid subtree, optionally seeding initial atom
39
+ * values and scheduler settings, and disposes the registry when the owner is
40
+ * cleaned up.
41
+ *
14
42
  * @category context
43
+ * @since 4.0.0
15
44
  */
16
45
  export declare const RegistryProvider: (options: {
17
46
  readonly children?: JSX.Element | undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"RegistryContext.d.ts","sourceRoot":"","sources":["../src/RegistryContext.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,KAAK,IAAI,MAAM,iCAAiC,CAAA;AAC5D,OAAO,KAAK,YAAY,MAAM,yCAAyC,CAAA;AACvE,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,UAAU,CAAA;AAGnC;;;GAGG;AACH,eAAO,MAAM,eAAe,uDAAgE,CAAA;AAE5F;;;GAGG;AACH,eAAO,MAAM,gBAAgB,GAAI,SAAS;IACxC,QAAQ,CAAC,QAAQ,CAAC,EAAE,GAAG,CAAC,OAAO,GAAG,SAAS,CAAA;IAC3C,QAAQ,CAAC,aAAa,CAAC,EAAE,QAAQ,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC,GAAG,SAAS,CAAA;IAC7E,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,IAAI,KAAK,MAAM,IAAI,CAAC,GAAG,SAAS,CAAA;IACnE,QAAQ,CAAC,iBAAiB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC/C,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAC7C,gBAcA,CAAA"}
1
+ {"version":3,"file":"RegistryContext.d.ts","sourceRoot":"","sources":["../src/RegistryContext.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,OAAO,KAAK,KAAK,IAAI,MAAM,iCAAiC,CAAA;AAC5D,OAAO,KAAK,YAAY,MAAM,yCAAyC,CAAA;AACvE,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,UAAU,CAAA;AAGnC;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,uDAAgE,CAAA;AAE5F;;;;;;;GAOG;AACH,eAAO,MAAM,gBAAgB,GAAI,SAAS;IACxC,QAAQ,CAAC,QAAQ,CAAC,EAAE,GAAG,CAAC,OAAO,GAAG,SAAS,CAAA;IAC3C,QAAQ,CAAC,aAAa,CAAC,EAAE,QAAQ,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC,GAAG,SAAS,CAAA;IAC7E,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,IAAI,KAAK,MAAM,IAAI,CAAC,GAAG,SAAS,CAAA;IACnE,QAAQ,CAAC,iBAAiB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC/C,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAC7C,gBAcA,CAAA"}
@@ -1,20 +1,27 @@
1
1
  import * as AtomRegistry from "effect/unstable/reactivity/AtomRegistry";
2
2
  import { createComponent, createContext, onCleanup } from "solid-js";
3
3
  /**
4
- * @since 1.0.0
4
+ * A Solid context that carries the `AtomRegistry` used by atom hooks in the
5
+ * current owner tree.
6
+ *
5
7
  * @category context
8
+ * @since 4.0.0
6
9
  */
7
10
  export const RegistryContext = /*#__PURE__*/createContext(/*#__PURE__*/AtomRegistry.make());
8
11
  /**
9
- * @since 1.0.0
12
+ * Creates an `AtomRegistry` for a Solid subtree, optionally seeding initial atom
13
+ * values and scheduler settings, and disposes the registry when the owner is
14
+ * cleaned up.
15
+ *
10
16
  * @category context
17
+ * @since 4.0.0
11
18
  */
12
19
  export const RegistryProvider = options => {
13
20
  const registry = AtomRegistry.make({
14
21
  scheduleTask: options.scheduleTask,
15
22
  initialValues: options.initialValues,
16
23
  timeoutResolution: options.timeoutResolution,
17
- defaultIdleTTL: options.defaultIdleTTL
24
+ defaultIdleTTL: options.defaultIdleTTL ?? 400
18
25
  });
19
26
  onCleanup(() => registry.dispose());
20
27
  return createComponent(RegistryContext.Provider, {
@@ -1 +1 @@
1
- {"version":3,"file":"RegistryContext.js","names":["AtomRegistry","createComponent","createContext","onCleanup","RegistryContext","make","RegistryProvider","options","registry","scheduleTask","initialValues","timeoutResolution","defaultIdleTTL","dispose","Provider","value","children"],"sources":["../src/RegistryContext.ts"],"sourcesContent":[null],"mappings":"AAIA,OAAO,KAAKA,YAAY,MAAM,yCAAyC;AAEvE,SAASC,eAAe,EAAEC,aAAa,EAAEC,SAAS,QAAQ,UAAU;AAEpE;;;;AAIA,OAAO,MAAMC,eAAe,gBAAGF,aAAa,cAA4BF,YAAY,CAACK,IAAI,EAAE,CAAC;AAE5F;;;;AAIA,OAAO,MAAMC,gBAAgB,GAAIC,OAMhC,IAAI;EACH,MAAMC,QAAQ,GAAGR,YAAY,CAACK,IAAI,CAAC;IACjCI,YAAY,EAAEF,OAAO,CAACE,YAAY;IAClCC,aAAa,EAAEH,OAAO,CAACG,aAAa;IACpCC,iBAAiB,EAAEJ,OAAO,CAACI,iBAAiB;IAC5CC,cAAc,EAAEL,OAAO,CAACK;GACzB,CAAC;EACFT,SAAS,CAAC,MAAMK,QAAQ,CAACK,OAAO,EAAE,CAAC;EACnC,OAAOZ,eAAe,CAACG,eAAe,CAACU,QAAQ,EAAE;IAC/CC,KAAK,EAAEP,QAAQ;IACf,IAAIQ,QAAQA,CAAA;MACV,OAAOT,OAAO,CAACS,QAAQ;IACzB;GACD,CAAC;AACJ,CAAC","ignoreList":[]}
1
+ {"version":3,"file":"RegistryContext.js","names":["AtomRegistry","createComponent","createContext","onCleanup","RegistryContext","make","RegistryProvider","options","registry","scheduleTask","initialValues","timeoutResolution","defaultIdleTTL","dispose","Provider","value","children"],"sources":["../src/RegistryContext.ts"],"sourcesContent":[null],"mappings":"AA0BA,OAAO,KAAKA,YAAY,MAAM,yCAAyC;AAEvE,SAASC,eAAe,EAAEC,aAAa,EAAEC,SAAS,QAAQ,UAAU;AAEpE;;;;;;;AAOA,OAAO,MAAMC,eAAe,gBAAGF,aAAa,cAA4BF,YAAY,CAACK,IAAI,EAAE,CAAC;AAE5F;;;;;;;;AAQA,OAAO,MAAMC,gBAAgB,GAAIC,OAMhC,IAAI;EACH,MAAMC,QAAQ,GAAGR,YAAY,CAACK,IAAI,CAAC;IACjCI,YAAY,EAAEF,OAAO,CAACE,YAAY;IAClCC,aAAa,EAAEH,OAAO,CAACG,aAAa;IACpCC,iBAAiB,EAAEJ,OAAO,CAACI,iBAAiB;IAC5CC,cAAc,EAAEL,OAAO,CAACK,cAAc,IAAI;GAC3C,CAAC;EACFT,SAAS,CAAC,MAAMK,QAAQ,CAACK,OAAO,EAAE,CAAC;EACnC,OAAOZ,eAAe,CAACG,eAAe,CAACU,QAAQ,EAAE;IAC/CC,KAAK,EAAEP,QAAQ;IACf,IAAIQ,QAAQA,CAAA;MACV,OAAOT,OAAO,CAACS,QAAQ;IACzB;GACD,CAAC;AACJ,CAAC","ignoreList":[]}
package/dist/index.d.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  /**
2
- * @since 1.0.0
2
+ * @since 4.0.0
3
3
  */
4
4
  /**
5
- * @since 1.0.0
5
+ * @since 4.0.0
6
6
  */
7
7
  export * from "./Hooks.ts";
8
8
  /**
9
- * @since 1.0.0
9
+ * @since 4.0.0
10
10
  */
11
11
  export * from "./RegistryContext.ts";
12
12
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,12 +1,12 @@
1
1
  /**
2
- * @since 1.0.0
2
+ * @since 4.0.0
3
3
  */
4
4
  /**
5
- * @since 1.0.0
5
+ * @since 4.0.0
6
6
  */
7
7
  export * from "./Hooks.js";
8
8
  /**
9
- * @since 1.0.0
9
+ * @since 4.0.0
10
10
  */
11
11
  export * from "./RegistryContext.js";
12
12
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@effect/atom-solid",
3
- "version": "4.0.0-beta.7",
3
+ "version": "4.0.0-beta.70",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "SolidJS bindings for the Effect Atom modules",
@@ -44,15 +44,15 @@
44
44
  },
45
45
  "peerDependencies": {
46
46
  "solid-js": ">=1 <2",
47
- "effect": "^4.0.0-beta.7"
47
+ "effect": "^4.0.0-beta.70"
48
48
  },
49
49
  "devDependencies": {
50
50
  "@solidjs/testing-library": "^0.8.0",
51
51
  "@testing-library/dom": "^10.4.1",
52
52
  "@testing-library/jest-dom": "^6.9.1",
53
- "jsdom": "^27.1.0",
54
- "solid-js": "^1.9.0",
55
- "effect": "^4.0.0-beta.7"
53
+ "jsdom": "^29.1.1",
54
+ "solid-js": "^1.9.12",
55
+ "effect": "^4.0.0-beta.70"
56
56
  },
57
57
  "scripts": {
58
58
  "build": "tsc -b tsconfig.json && pnpm babel",
package/src/Hooks.ts CHANGED
@@ -1,22 +1,50 @@
1
1
  /**
2
- * @since 1.0.0
2
+ * The `Hooks` module provides SolidJS hooks for reading, writing, mounting, and
3
+ * subscribing to Effect atoms through the current Solid atom registry.
4
+ *
5
+ * **Common tasks**
6
+ *
7
+ * - Read an atom as a Solid accessor with {@link useAtomValue}
8
+ * - Read and write a writable atom with {@link useAtom}
9
+ * - Write without subscribing to the value with {@link useAtomSet}
10
+ * - Refresh or mount atoms from components with {@link useAtomRefresh} and {@link useAtomMount}
11
+ * - Convert `AsyncResult` atoms into Solid resources with {@link useAtomResource}
12
+ * - Work with atom refs and nested ref properties with {@link useAtomRef}, {@link useAtomRefProp},
13
+ * and {@link useAtomRefPropValue}
14
+ *
15
+ * **Solid integration notes**
16
+ *
17
+ * Hooks in this module read the registry from {@link RegistryContext}, so they
18
+ * should be used under the matching provider for the atom graph you want to
19
+ * observe. Atom arguments are thunks so Solid can track dynamic atom selection;
20
+ * subscriptions are registered in Solid computations and disposed with
21
+ * `onCleanup` when the computation changes or the component unmounts.
22
+ *
23
+ * @since 4.0.0
3
24
  */
4
25
  import * as Cause from "effect/Cause"
5
26
  import * as Effect from "effect/Effect"
6
27
  import * as Exit from "effect/Exit"
7
- import type * as AsyncResult from "effect/unstable/reactivity/AsyncResult"
28
+ import * as AsyncResult from "effect/unstable/reactivity/AsyncResult"
8
29
  import * as Atom from "effect/unstable/reactivity/Atom"
9
30
  import type * as AtomRef from "effect/unstable/reactivity/AtomRef"
10
31
  import * as AtomRegistry from "effect/unstable/reactivity/AtomRegistry"
11
- import type { Accessor } from "solid-js"
12
- import { createSignal, onCleanup, useContext } from "solid-js"
32
+ import type { Accessor, ResourceOptions, ResourceReturn } from "solid-js"
33
+ import { createComputed, createEffect, createMemo, createResource, createSignal, onCleanup, useContext } from "solid-js"
13
34
  import { RegistryContext } from "./RegistryContext.ts"
14
35
 
15
36
  const initialValuesSet = new WeakMap<AtomRegistry.AtomRegistry, WeakSet<Atom.Atom<any>>>()
16
37
 
17
38
  /**
18
- * @since 1.0.0
39
+ * Seeds initial atom values in the current Solid atom registry.
40
+ *
41
+ * **Details**
42
+ *
43
+ * Each atom is initialized at most once for a given registry, so subsequent
44
+ * computations do not overwrite values that have already been established.
45
+ *
19
46
  * @category hooks
47
+ * @since 4.0.0
20
48
  */
21
49
  export const useAtomInitialValues = (initialValues: Iterable<readonly [Atom.Atom<any>, any]>): void => {
22
50
  const registry = useContext(RegistryContext)
@@ -34,38 +62,53 @@ export const useAtomInitialValues = (initialValues: Iterable<readonly [Atom.Atom
34
62
  }
35
63
 
36
64
  /**
37
- * @since 1.0.0
65
+ * Subscribes to an atom in the current Solid registry and returns its value as
66
+ * a Solid accessor.
67
+ *
38
68
  * @category hooks
69
+ * @since 4.0.0
39
70
  */
40
71
  export const useAtomValue: {
41
72
  /**
42
- * @since 1.0.0
73
+ * Subscribes to an atom in the current Solid registry and returns its value as
74
+ * a Solid accessor.
75
+ *
43
76
  * @category hooks
77
+ * @since 4.0.0
44
78
  */
45
- <A>(atom: Atom.Atom<A>): Accessor<A>
79
+ <A>(atom: () => Atom.Atom<A>): Accessor<A>
46
80
  /**
47
- * @since 1.0.0
81
+ * Subscribes to an atom in the current Solid registry and returns its value as
82
+ * a Solid accessor.
83
+ *
48
84
  * @category hooks
85
+ * @since 4.0.0
49
86
  */
50
- <A, B>(atom: Atom.Atom<A>, f: (_: A) => B): Accessor<B>
51
- } = <A>(atom: Atom.Atom<A>, f?: (_: A) => A): Accessor<A> => {
87
+ <A, B>(atom: () => Atom.Atom<A>, f: (_: A) => B): Accessor<B>
88
+ } = <A>(atom: () => Atom.Atom<A>, f?: (_: A) => A): Accessor<A> => {
52
89
  const registry = useContext(RegistryContext)
53
- return createAtomAccessor(registry, f ? Atom.map(atom, f) : atom)
90
+ return createAtomAccessor(registry, f ? () => Atom.map(atom(), f) : atom)
54
91
  }
55
92
 
56
- function createAtomAccessor<A>(registry: AtomRegistry.AtomRegistry, atom: Atom.Atom<A>): Accessor<A> {
57
- const [value, setValue] = createSignal<A>(registry.get(atom))
58
- onCleanup(registry.subscribe(atom, setValue as any))
93
+ function createAtomAccessor<A>(registry: AtomRegistry.AtomRegistry, atom: () => Atom.Atom<A>): Accessor<A> {
94
+ const [value, setValue] = createSignal<A>(null as any)
95
+ createComputed(() => {
96
+ onCleanup(registry.subscribe(atom(), setValue as any, constImmediate))
97
+ })
59
98
  return value
60
99
  }
61
100
 
62
- function mountAtom<A>(registry: AtomRegistry.AtomRegistry, atom: Atom.Atom<A>): void {
63
- onCleanup(registry.mount(atom))
101
+ const constImmediate = { immediate: true }
102
+
103
+ function mountAtom<A>(registry: AtomRegistry.AtomRegistry, atom: () => Atom.Atom<A>): void {
104
+ createComputed(() => {
105
+ onCleanup(registry.mount(atom()))
106
+ })
64
107
  }
65
108
 
66
109
  function setAtom<R, W, Mode extends "value" | "promise" | "promiseExit" = never>(
67
110
  registry: AtomRegistry.AtomRegistry,
68
- atom: Atom.Writable<R, W>,
111
+ atom: () => Atom.Writable<R, W>,
69
112
  options?: {
70
113
  readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined
71
114
  }
@@ -77,11 +120,12 @@ function setAtom<R, W, Mode extends "value" | "promise" | "promiseExit" = never>
77
120
  ) :
78
121
  ((value: W | ((value: R) => W)) => void)
79
122
  {
123
+ const memo = createMemo(atom)
80
124
  if (options?.mode === "promise" || options?.mode === "promiseExit") {
81
125
  return ((value: W) => {
82
- registry.set(atom, value)
126
+ registry.set(memo(), value)
83
127
  const promise = Effect.runPromiseExit(
84
- AtomRegistry.getResult(registry, atom as Atom.Atom<AsyncResult.AsyncResult<any, any>>, {
128
+ AtomRegistry.getResult(registry, memo() as Atom.Atom<AsyncResult.AsyncResult<any, any>>, {
85
129
  suspendOnWaiting: true
86
130
  })
87
131
  )
@@ -89,7 +133,7 @@ function setAtom<R, W, Mode extends "value" | "promise" | "promiseExit" = never>
89
133
  }) as any
90
134
  }
91
135
  return ((value: W | ((value: R) => W)) => {
92
- registry.set(atom, typeof value === "function" ? (value as any)(registry.get(atom)) : value)
136
+ registry.set(memo(), typeof value === "function" ? (value as any)(registry.get(memo())) : value)
93
137
  }) as any
94
138
  }
95
139
 
@@ -99,24 +143,29 @@ const flattenExit = <A, E>(exit: Exit.Exit<A, E>): A => {
99
143
  }
100
144
 
101
145
  /**
102
- * @since 1.0.0
146
+ * Mounts an atom in the current Solid registry for the lifetime of the current
147
+ * Solid computation.
148
+ *
103
149
  * @category hooks
150
+ * @since 4.0.0
104
151
  */
105
- export const useAtomMount = <A>(atom: Atom.Atom<A>): void => {
152
+ export const useAtomMount = <A>(atom: () => Atom.Atom<A>): void => {
106
153
  const registry = useContext(RegistryContext)
107
154
  mountAtom(registry, atom)
108
155
  }
109
156
 
110
157
  /**
111
- * @since 1.0.0
158
+ * Returns a setter for a writable atom without subscribing to its value.
159
+ *
112
160
  * @category hooks
161
+ * @since 4.0.0
113
162
  */
114
163
  export const useAtomSet = <
115
164
  R,
116
165
  W,
117
166
  Mode extends "value" | "promise" | "promiseExit" = never
118
167
  >(
119
- atom: Atom.Writable<R, W>,
168
+ atom: () => Atom.Writable<R, W>,
120
169
  options?: {
121
170
  readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined
122
171
  }
@@ -134,21 +183,27 @@ export const useAtomSet = <
134
183
  }
135
184
 
136
185
  /**
137
- * @since 1.0.0
186
+ * Mounts an atom and returns a callback that refreshes the current atom.
187
+ *
138
188
  * @category hooks
189
+ * @since 4.0.0
139
190
  */
140
- export const useAtomRefresh = <A>(atom: Atom.Atom<A>): () => void => {
191
+ export const useAtomRefresh = <A>(atom: () => Atom.Atom<A>): () => void => {
141
192
  const registry = useContext(RegistryContext)
142
193
  mountAtom(registry, atom)
143
- return () => registry.refresh(atom)
194
+ const memo = createMemo(atom)
195
+ return () => registry.refresh(memo())
144
196
  }
145
197
 
146
198
  /**
147
- * @since 1.0.0
199
+ * Returns a Solid accessor for a writable atom together with a setter for
200
+ * updating it.
201
+ *
148
202
  * @category hooks
203
+ * @since 4.0.0
149
204
  */
150
205
  export const useAtom = <R, W, const Mode extends "value" | "promise" | "promiseExit" = never>(
151
- atom: Atom.Writable<R, W>,
206
+ atom: () => Atom.Writable<R, W>,
152
207
  options?: {
153
208
  readonly mode?: ([R] extends [AsyncResult.AsyncResult<any, any>] ? Mode : "value") | undefined
154
209
  }
@@ -170,38 +225,80 @@ export const useAtom = <R, W, const Mode extends "value" | "promise" | "promiseE
170
225
  }
171
226
 
172
227
  /**
173
- * @since 1.0.0
228
+ * Subscribes a callback to an atom in the current Solid registry.
229
+ *
174
230
  * @category hooks
231
+ * @since 4.0.0
175
232
  */
176
233
  export const useAtomSubscribe = <A>(
177
- atom: Atom.Atom<A>,
234
+ atom: () => Atom.Atom<A>,
178
235
  f: (_: A) => void,
179
236
  options?: { readonly immediate?: boolean }
180
237
  ): void => {
181
238
  const registry = useContext(RegistryContext)
182
- onCleanup(registry.subscribe(atom, f, options))
239
+ createEffect(() => {
240
+ onCleanup(registry.subscribe(atom(), f, options))
241
+ })
183
242
  }
184
243
 
185
244
  /**
186
- * @since 1.0.0
245
+ * Converts an `AsyncResult` atom into a Solid resource.
246
+ *
247
+ * @category hooks
248
+ * @since 4.0.0
249
+ */
250
+ export const useAtomResource = <A, E>(
251
+ atom: () => Atom.Atom<AsyncResult.AsyncResult<A, E>>,
252
+ options?: ResourceOptions<A> & {
253
+ readonly suspendOnWaiting?: boolean | undefined
254
+ }
255
+ ): ResourceReturn<A, void> => {
256
+ const result = useAtomValue(atom)
257
+ return createResource(result, (result) => {
258
+ if (AsyncResult.isInitial(result) || (options?.suspendOnWaiting && result.waiting)) {
259
+ return constUnresolvedPromise
260
+ } else if (AsyncResult.isSuccess(result)) {
261
+ return Promise.resolve(result.value)
262
+ }
263
+ return Promise.reject(Cause.squash(result.cause))
264
+ })
265
+ }
266
+
267
+ const constUnresolvedPromise = new Promise<never>(() => {})
268
+
269
+ /**
270
+ * Subscribes to an atom ref and returns its value as a Solid accessor.
271
+ *
187
272
  * @category hooks
273
+ * @since 4.0.0
188
274
  */
189
- export const useAtomRef = <A>(ref: AtomRef.ReadonlyRef<A>): Accessor<A> => {
190
- const [value, setValue] = createSignal(ref.value)
191
- onCleanup(ref.subscribe(setValue))
275
+ export const useAtomRef = <A>(ref: () => AtomRef.ReadonlyRef<A>): Accessor<A> => {
276
+ const [value, setValue] = createSignal(null as A)
277
+ createComputed(() => {
278
+ const r = ref()
279
+ setValue(r.value as any)
280
+ onCleanup(r.subscribe(setValue))
281
+ })
192
282
  return value
193
283
  }
194
284
 
195
285
  /**
196
- * @since 1.0.0
286
+ * Returns a Solid accessor for a property ref derived from an atom ref.
287
+ *
197
288
  * @category hooks
289
+ * @since 4.0.0
198
290
  */
199
- export const useAtomRefProp = <A, K extends keyof A>(ref: AtomRef.AtomRef<A>, prop: K): AtomRef.AtomRef<A[K]> =>
200
- ref.prop(prop)
291
+ export const useAtomRefProp = <A, K extends keyof A>(
292
+ ref: () => AtomRef.AtomRef<A>,
293
+ prop: K
294
+ ): Accessor<AtomRef.AtomRef<A[K]>> => createMemo(() => ref().prop(prop))
201
295
 
202
296
  /**
203
- * @since 1.0.0
297
+ * Returns a Solid accessor for the value of a property ref derived from an atom
298
+ * ref.
299
+ *
204
300
  * @category hooks
301
+ * @since 4.0.0
205
302
  */
206
- export const useAtomRefPropValue = <A, K extends keyof A>(ref: AtomRef.AtomRef<A>, prop: K): Accessor<A[K]> =>
303
+ export const useAtomRefPropValue = <A, K extends keyof A>(ref: () => AtomRef.AtomRef<A>, prop: K): Accessor<A[K]> =>
207
304
  useAtomRef(useAtomRefProp(ref, prop))
@@ -1,5 +1,27 @@
1
1
  /**
2
- * @since 1.0.0
2
+ * The `RegistryContext` module provides the Solid context used by Effect Atom
3
+ * hooks to share an `AtomRegistry` across an owner tree. The registry owns atom
4
+ * state, scheduling, and idle cleanup, so Solid components that read or write
5
+ * atoms coordinate through the same runtime instead of creating isolated
6
+ * registries.
7
+ *
8
+ * **Common tasks**
9
+ *
10
+ * - Use {@link RegistryProvider} to scope atom state to a Solid subtree
11
+ * - Seed atoms for tests, examples, or server-provided data with `initialValues`
12
+ * - Override scheduling or idle timing for custom rendering environments
13
+ * - Read {@link RegistryContext} when integrating lower-level atom APIs
14
+ *
15
+ * **Gotchas**
16
+ *
17
+ * - The provider creates a registry for the current Solid owner and disposes it
18
+ * with `onCleanup`
19
+ * - `initialValues` are applied when the provider creates the registry, not as
20
+ * reactive updates after creation
21
+ * - Overriding `scheduleTask` changes when atom work is flushed, so it should
22
+ * return a cancellation function that is safe to call during Solid cleanup
23
+ *
24
+ * @since 4.0.0
3
25
  */
4
26
  import type * as Atom from "effect/unstable/reactivity/Atom"
5
27
  import * as AtomRegistry from "effect/unstable/reactivity/AtomRegistry"
@@ -7,14 +29,21 @@ import type { JSX } from "solid-js"
7
29
  import { createComponent, createContext, onCleanup } from "solid-js"
8
30
 
9
31
  /**
10
- * @since 1.0.0
32
+ * A Solid context that carries the `AtomRegistry` used by atom hooks in the
33
+ * current owner tree.
34
+ *
11
35
  * @category context
36
+ * @since 4.0.0
12
37
  */
13
38
  export const RegistryContext = createContext<AtomRegistry.AtomRegistry>(AtomRegistry.make())
14
39
 
15
40
  /**
16
- * @since 1.0.0
41
+ * Creates an `AtomRegistry` for a Solid subtree, optionally seeding initial atom
42
+ * values and scheduler settings, and disposes the registry when the owner is
43
+ * cleaned up.
44
+ *
17
45
  * @category context
46
+ * @since 4.0.0
18
47
  */
19
48
  export const RegistryProvider = (options: {
20
49
  readonly children?: JSX.Element | undefined
@@ -27,7 +56,7 @@ export const RegistryProvider = (options: {
27
56
  scheduleTask: options.scheduleTask,
28
57
  initialValues: options.initialValues,
29
58
  timeoutResolution: options.timeoutResolution,
30
- defaultIdleTTL: options.defaultIdleTTL
59
+ defaultIdleTTL: options.defaultIdleTTL ?? 400
31
60
  })
32
61
  onCleanup(() => registry.dispose())
33
62
  return createComponent(RegistryContext.Provider, {
package/src/index.ts CHANGED
@@ -1,13 +1,13 @@
1
1
  /**
2
- * @since 1.0.0
2
+ * @since 4.0.0
3
3
  */
4
4
 
5
5
  /**
6
- * @since 1.0.0
6
+ * @since 4.0.0
7
7
  */
8
8
  export * from "./Hooks.ts"
9
9
 
10
10
  /**
11
- * @since 1.0.0
11
+ * @since 4.0.0
12
12
  */
13
13
  export * from "./RegistryContext.ts"