atomirx 0.0.2 → 0.0.5

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.
Files changed (65) hide show
  1. package/README.md +868 -161
  2. package/coverage/src/core/onCreateHook.ts.html +72 -70
  3. package/dist/core/atom.d.ts +83 -6
  4. package/dist/core/batch.d.ts +3 -3
  5. package/dist/core/derived.d.ts +69 -22
  6. package/dist/core/effect.d.ts +52 -52
  7. package/dist/core/getAtomState.d.ts +29 -0
  8. package/dist/core/hook.d.ts +1 -1
  9. package/dist/core/onCreateHook.d.ts +37 -23
  10. package/dist/core/onErrorHook.d.ts +49 -0
  11. package/dist/core/promiseCache.d.ts +23 -32
  12. package/dist/core/select.d.ts +208 -29
  13. package/dist/core/types.d.ts +107 -22
  14. package/dist/core/withReady.d.ts +115 -0
  15. package/dist/core/withReady.test.d.ts +1 -0
  16. package/dist/index-CBVj1kSj.js +1350 -0
  17. package/dist/index-Cxk9v0um.cjs +1 -0
  18. package/dist/index.cjs +1 -1
  19. package/dist/index.d.ts +12 -8
  20. package/dist/index.js +18 -15
  21. package/dist/react/index.cjs +10 -10
  22. package/dist/react/index.d.ts +2 -1
  23. package/dist/react/index.js +422 -377
  24. package/dist/react/rx.d.ts +114 -25
  25. package/dist/react/useAction.d.ts +5 -4
  26. package/dist/react/{useValue.d.ts → useSelector.d.ts} +56 -25
  27. package/dist/react/useSelector.test.d.ts +1 -0
  28. package/package.json +1 -1
  29. package/src/core/atom.test.ts +307 -43
  30. package/src/core/atom.ts +144 -22
  31. package/src/core/batch.test.ts +10 -10
  32. package/src/core/batch.ts +3 -3
  33. package/src/core/define.test.ts +12 -11
  34. package/src/core/define.ts +1 -1
  35. package/src/core/derived.test.ts +906 -72
  36. package/src/core/derived.ts +192 -81
  37. package/src/core/effect.test.ts +651 -45
  38. package/src/core/effect.ts +102 -98
  39. package/src/core/getAtomState.ts +69 -0
  40. package/src/core/hook.test.ts +5 -5
  41. package/src/core/hook.ts +1 -1
  42. package/src/core/onCreateHook.ts +38 -23
  43. package/src/core/onErrorHook.test.ts +350 -0
  44. package/src/core/onErrorHook.ts +52 -0
  45. package/src/core/promiseCache.test.ts +5 -3
  46. package/src/core/promiseCache.ts +76 -71
  47. package/src/core/select.ts +405 -130
  48. package/src/core/selector.test.ts +574 -32
  49. package/src/core/types.ts +107 -29
  50. package/src/core/withReady.test.ts +534 -0
  51. package/src/core/withReady.ts +191 -0
  52. package/src/core/withUse.ts +1 -1
  53. package/src/index.test.ts +4 -4
  54. package/src/index.ts +21 -7
  55. package/src/react/index.ts +2 -1
  56. package/src/react/rx.test.tsx +173 -18
  57. package/src/react/rx.tsx +274 -43
  58. package/src/react/useAction.test.ts +12 -14
  59. package/src/react/useAction.ts +11 -9
  60. package/src/react/{useValue.test.ts → useSelector.test.ts} +16 -16
  61. package/src/react/{useValue.ts → useSelector.ts} +64 -33
  62. package/v2.md +44 -44
  63. package/dist/index-2ok7ilik.js +0 -1217
  64. package/dist/index-B_5SFzfl.cjs +0 -1
  65. /package/dist/{react/useValue.test.d.ts → core/onErrorHook.test.d.ts} +0 -0
@@ -0,0 +1,350 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2
+ import { atom } from "./atom";
3
+ import { derived } from "./derived";
4
+ import { effect } from "./effect";
5
+ import { onErrorHook, ErrorInfo } from "./onErrorHook";
6
+
7
+ describe("onErrorHook", () => {
8
+ beforeEach(() => {
9
+ onErrorHook.reset();
10
+ });
11
+
12
+ afterEach(() => {
13
+ onErrorHook.reset();
14
+ });
15
+
16
+ describe("with derived", () => {
17
+ it("should call onErrorHook when derived throws synchronously", async () => {
18
+ const hookFn = vi.fn();
19
+ onErrorHook.override(() => hookFn);
20
+
21
+ const source$ = atom(0);
22
+ const derived$ = derived(
23
+ ({ read }) => {
24
+ const val = read(source$);
25
+ if (val > 0) {
26
+ throw new Error("Derived error");
27
+ }
28
+ return val;
29
+ },
30
+ { meta: { key: "testDerived" } }
31
+ );
32
+
33
+ await derived$.get();
34
+ expect(hookFn).not.toHaveBeenCalled();
35
+
36
+ // Trigger error
37
+ source$.set(5);
38
+ derived$.get().catch(() => {});
39
+ await new Promise((r) => setTimeout(r, 0));
40
+
41
+ expect(hookFn).toHaveBeenCalledTimes(1);
42
+ const info: ErrorInfo = hookFn.mock.calls[0][0];
43
+ expect(info.source.type).toBe("derived");
44
+ expect(info.source.key).toBe("testDerived");
45
+ expect((info.error as Error).message).toBe("Derived error");
46
+ });
47
+
48
+ it("should call onErrorHook when async dependency rejects", async () => {
49
+ const hookFn = vi.fn();
50
+ onErrorHook.override(() => hookFn);
51
+
52
+ const asyncSource$ = atom(Promise.reject(new Error("Async error")));
53
+
54
+ const derived$ = derived(({ read }) => read(asyncSource$), {
55
+ meta: { key: "asyncDerived" },
56
+ });
57
+
58
+ derived$.get().catch(() => {});
59
+ await new Promise((r) => setTimeout(r, 20));
60
+
61
+ expect(hookFn).toHaveBeenCalledTimes(1);
62
+ const info: ErrorInfo = hookFn.mock.calls[0][0];
63
+ expect(info.source.type).toBe("derived");
64
+ expect(info.source.key).toBe("asyncDerived");
65
+ expect((info.error as Error).message).toBe("Async error");
66
+ });
67
+
68
+ it("should include derived atom in source", async () => {
69
+ const hookFn = vi.fn();
70
+ onErrorHook.override(() => hookFn);
71
+
72
+ const source$ = atom(1);
73
+ const derived$ = derived(({ read }) => {
74
+ throw new Error("Test");
75
+ return read(source$);
76
+ });
77
+
78
+ derived$.get().catch(() => {});
79
+ await new Promise((r) => setTimeout(r, 0));
80
+
81
+ const info: ErrorInfo = hookFn.mock.calls[0][0];
82
+ expect(info.source.type).toBe("derived");
83
+ if (info.source.type === "derived") {
84
+ expect(info.source.instance).toBe(derived$);
85
+ }
86
+ });
87
+
88
+ it("should call both onError option and onErrorHook", async () => {
89
+ const hookFn = vi.fn();
90
+ const onErrorFn = vi.fn();
91
+ onErrorHook.override(() => hookFn);
92
+
93
+ const source$ = atom(0);
94
+ const derived$ = derived(
95
+ ({ read }) => {
96
+ const val = read(source$);
97
+ if (val > 0) throw new Error("Error");
98
+ return val;
99
+ },
100
+ { onError: onErrorFn }
101
+ );
102
+
103
+ await derived$.get();
104
+ source$.set(1);
105
+ derived$.get().catch(() => {});
106
+ await new Promise((r) => setTimeout(r, 0));
107
+
108
+ expect(onErrorFn).toHaveBeenCalledTimes(1);
109
+ expect(hookFn).toHaveBeenCalledTimes(1);
110
+ });
111
+ });
112
+
113
+ describe("with effect", () => {
114
+ it("should call onErrorHook with effect source when effect throws", async () => {
115
+ const hookFn = vi.fn();
116
+ onErrorHook.override(() => hookFn);
117
+
118
+ const source$ = atom(0);
119
+
120
+ effect(
121
+ ({ read }) => {
122
+ const val = read(source$);
123
+ if (val > 0) {
124
+ throw new Error("Effect error");
125
+ }
126
+ },
127
+ { meta: { key: "testEffect" } }
128
+ );
129
+
130
+ await new Promise((r) => setTimeout(r, 0));
131
+ expect(hookFn).not.toHaveBeenCalled();
132
+
133
+ // Trigger error
134
+ source$.set(5);
135
+ await new Promise((r) => setTimeout(r, 10));
136
+
137
+ expect(hookFn).toHaveBeenCalledTimes(1);
138
+ const info: ErrorInfo = hookFn.mock.calls[0][0];
139
+ expect(info.source.type).toBe("effect"); // Should be effect, not derived!
140
+ expect(info.source.key).toBe("testEffect");
141
+ expect((info.error as Error).message).toBe("Effect error");
142
+ });
143
+
144
+ it("should include effect instance in source", async () => {
145
+ const hookFn = vi.fn();
146
+ onErrorHook.override(() => hookFn);
147
+
148
+ const source$ = atom(1);
149
+ const e = effect(
150
+ ({ read }) => {
151
+ read(source$);
152
+ throw new Error("Test");
153
+ },
154
+ { meta: { key: "myEffect" } }
155
+ );
156
+
157
+ await new Promise((r) => setTimeout(r, 10));
158
+
159
+ const info: ErrorInfo = hookFn.mock.calls[0][0];
160
+ expect(info.source.type).toBe("effect");
161
+ if (info.source.type === "effect") {
162
+ expect(info.source.instance).toBe(e);
163
+ }
164
+ });
165
+
166
+ it("should call both onError option and onErrorHook for effect", async () => {
167
+ const hookFn = vi.fn();
168
+ const onErrorFn = vi.fn();
169
+ onErrorHook.override(() => hookFn);
170
+
171
+ const source$ = atom(0);
172
+
173
+ effect(
174
+ ({ read }) => {
175
+ const val = read(source$);
176
+ if (val > 0) throw new Error("Error");
177
+ },
178
+ { onError: onErrorFn }
179
+ );
180
+
181
+ await new Promise((r) => setTimeout(r, 0));
182
+ source$.set(1);
183
+ await new Promise((r) => setTimeout(r, 10));
184
+
185
+ expect(onErrorFn).toHaveBeenCalledTimes(1);
186
+ expect(hookFn).toHaveBeenCalledTimes(1);
187
+ });
188
+ });
189
+
190
+ describe("hook behavior", () => {
191
+ it("should not throw when onErrorHook is not set", async () => {
192
+ // Hook is reset in beforeEach, so no handler is set
193
+
194
+ const source$ = atom(1);
195
+ const derived$ = derived(({ read }) => {
196
+ throw new Error("Test");
197
+ return read(source$);
198
+ });
199
+
200
+ // Should not throw
201
+ derived$.get().catch(() => {});
202
+ await new Promise((r) => setTimeout(r, 0));
203
+ });
204
+
205
+ it("should support middleware pattern with override", async () => {
206
+ const errors: ErrorInfo[] = [];
207
+
208
+ // First handler
209
+ onErrorHook.override(() => (info) => {
210
+ errors.push({ ...info, error: "first" });
211
+ });
212
+
213
+ // Add middleware - chains with previous
214
+ onErrorHook.override((prev) => (info) => {
215
+ prev?.(info);
216
+ errors.push({ ...info, error: "second" });
217
+ });
218
+
219
+ const source$ = atom(1);
220
+ derived(({ read }) => {
221
+ throw new Error("Test");
222
+ return read(source$);
223
+ })
224
+ .get()
225
+ .catch(() => {});
226
+
227
+ await new Promise((r) => setTimeout(r, 0));
228
+
229
+ expect(errors.length).toBe(2);
230
+ expect(errors[0].error).toBe("first");
231
+ expect(errors[1].error).toBe("second");
232
+ });
233
+
234
+ it("should support reset", async () => {
235
+ const hookFn = vi.fn();
236
+ onErrorHook.override(() => hookFn);
237
+
238
+ onErrorHook.reset();
239
+
240
+ const source$ = atom(1);
241
+ derived(({ read }) => {
242
+ throw new Error("Test");
243
+ return read(source$);
244
+ })
245
+ .get()
246
+ .catch(() => {});
247
+
248
+ await new Promise((r) => setTimeout(r, 0));
249
+
250
+ expect(hookFn).not.toHaveBeenCalled();
251
+ });
252
+ });
253
+
254
+ describe("real-world scenarios", () => {
255
+ it("should enable global error logging", async () => {
256
+ const errorLog: Array<{ type: string; key?: string; error: string }> = [];
257
+
258
+ onErrorHook.override(() => (info) => {
259
+ errorLog.push({
260
+ type: info.source.type,
261
+ key: info.source.key,
262
+ error: String(info.error),
263
+ });
264
+ });
265
+
266
+ const source$ = atom(0);
267
+
268
+ // Create multiple derived/effects that will error
269
+ const derived1$ = derived(
270
+ ({ read }) => {
271
+ if (read(source$) > 0) throw new Error("Derived 1 failed");
272
+ return read(source$);
273
+ },
274
+ { meta: { key: "derived1" } }
275
+ );
276
+
277
+ const derived2$ = derived(
278
+ ({ read }) => {
279
+ if (read(source$) > 0) throw new Error("Derived 2 failed");
280
+ return read(source$);
281
+ },
282
+ { meta: { key: "derived2" } }
283
+ );
284
+
285
+ effect(
286
+ ({ read }) => {
287
+ if (read(source$) > 0) throw new Error("Effect 1 failed");
288
+ },
289
+ { meta: { key: "effect1" } }
290
+ );
291
+
292
+ // Trigger initial computation for derived atoms (they're lazy)
293
+ await derived1$.get();
294
+ await derived2$.get();
295
+ await new Promise((r) => setTimeout(r, 0));
296
+ expect(errorLog.length).toBe(0);
297
+
298
+ // Trigger all errors
299
+ source$.set(1);
300
+ derived1$.get().catch(() => {});
301
+ derived2$.get().catch(() => {});
302
+ await new Promise((r) => setTimeout(r, 20));
303
+
304
+ expect(errorLog.length).toBe(3);
305
+ expect(errorLog.map((e) => e.key).sort()).toEqual([
306
+ "derived1",
307
+ "derived2",
308
+ "effect1",
309
+ ]);
310
+ });
311
+
312
+ it("should enable error monitoring service integration", async () => {
313
+ const sentryMock = {
314
+ captureException: vi.fn(),
315
+ };
316
+
317
+ onErrorHook.override(() => (info) => {
318
+ sentryMock.captureException(info.error, {
319
+ tags: {
320
+ source_type: info.source.type,
321
+ source_key: info.source.key,
322
+ },
323
+ });
324
+ });
325
+
326
+ const source$ = atom(1);
327
+ derived(
328
+ ({ read }) => {
329
+ throw new Error("Critical error");
330
+ return read(source$);
331
+ },
332
+ { meta: { key: "criticalDerived" } }
333
+ )
334
+ .get()
335
+ .catch(() => {});
336
+
337
+ await new Promise((r) => setTimeout(r, 0));
338
+
339
+ expect(sentryMock.captureException).toHaveBeenCalledWith(
340
+ expect.any(Error),
341
+ {
342
+ tags: {
343
+ source_type: "derived",
344
+ source_key: "criticalDerived",
345
+ },
346
+ }
347
+ );
348
+ });
349
+ });
350
+ });
@@ -0,0 +1,52 @@
1
+ import { hook } from "./hook";
2
+ import { CreateInfo } from "./onCreateHook";
3
+
4
+ /**
5
+ * Information provided when an error occurs in an atom, derived, or effect.
6
+ */
7
+ export interface ErrorInfo {
8
+ /** The source that produced the error (atom, derived, or effect) */
9
+ source: CreateInfo;
10
+ /** The error that was thrown */
11
+ error: unknown;
12
+ }
13
+
14
+ /**
15
+ * Global hook that fires whenever an error occurs in a derived atom or effect.
16
+ *
17
+ * This is useful for:
18
+ * - **Global error logging** - capture all errors in one place
19
+ * - **Error monitoring** - send errors to monitoring services (Sentry, etc.)
20
+ * - **DevTools integration** - show errors in developer tools
21
+ * - **Debugging** - track which atoms/effects are failing
22
+ *
23
+ * **IMPORTANT**: Always use `.override()` to preserve the hook chain.
24
+ * Direct assignment to `.current` will break existing handlers.
25
+ *
26
+ * @example Basic logging
27
+ * ```ts
28
+ * onErrorHook.override((prev) => (info) => {
29
+ * prev?.(info); // call existing handlers first
30
+ * console.error(`Error in ${info.source.type}: ${info.source.key ?? "anonymous"}`, info.error);
31
+ * });
32
+ * ```
33
+ *
34
+ * @example Send to monitoring service
35
+ * ```ts
36
+ * onErrorHook.override((prev) => (info) => {
37
+ * prev?.(info); // preserve chain
38
+ * Sentry.captureException(info.error, {
39
+ * tags: {
40
+ * source_type: info.source.type,
41
+ * source_key: info.source.key,
42
+ * },
43
+ * });
44
+ * });
45
+ * ```
46
+ *
47
+ * @example Reset to default (disable all handlers)
48
+ * ```ts
49
+ * onErrorHook.reset();
50
+ * ```
51
+ */
52
+ export const onErrorHook = hook<(info: ErrorInfo) => void>();
@@ -7,9 +7,9 @@ import {
7
7
  isFulfilled,
8
8
  isRejected,
9
9
  unwrap,
10
- getAtomState,
11
10
  isDerived,
12
11
  } from "./promiseCache";
12
+ import { getAtomState } from "./getAtomState";
13
13
  import { atom } from "./atom";
14
14
  import { derived } from "./derived";
15
15
 
@@ -187,7 +187,7 @@ describe("promiseCache", () => {
187
187
 
188
188
  it("should return true for derived atom", () => {
189
189
  const a$ = atom(0);
190
- const d$ = derived(({ get }) => get(a$) * 2);
190
+ const d$ = derived(({ read }) => read(a$) * 2);
191
191
  expect(isDerived(d$)).toBe(true);
192
192
  });
193
193
 
@@ -227,7 +227,9 @@ describe("promiseCache", () => {
227
227
 
228
228
  it("should return loading state for derived with fallback during loading", async () => {
229
229
  const asyncValue$ = atom(new Promise<number>(() => {}));
230
- const derived$ = derived(({ get }) => get(asyncValue$), { fallback: 0 });
230
+ const derived$ = derived(({ read }) => read(asyncValue$), {
231
+ fallback: 0,
232
+ });
231
233
 
232
234
  // Derived atoms return their state directly via state()
233
235
  // State is loading, but staleValue provides the fallback
@@ -1,5 +1,80 @@
1
+ import { shallow2Equal } from "./equality";
1
2
  import { isPromiseLike } from "./isPromiseLike";
2
- import { Atom, AtomState, DerivedAtom, SYMBOL_DERIVED } from "./types";
3
+ import { AnyFunc, DerivedAtom, SYMBOL_DERIVED } from "./types";
4
+
5
+ /**
6
+ * Metadata attached to combined promises for comparison.
7
+ */
8
+ export interface CombinedPromiseMeta {
9
+ type: "all" | "race" | "allSettled";
10
+ promises: Promise<unknown>[];
11
+ }
12
+
13
+ /**
14
+ * WeakMap cache for combined promise metadata.
15
+ * Using WeakMap allows promises to be garbage collected when no longer referenced.
16
+ */
17
+ const combinedPromiseCache = new WeakMap<
18
+ PromiseLike<unknown>,
19
+ CombinedPromiseMeta
20
+ >();
21
+
22
+ /**
23
+ * Gets the metadata for a combined promise, if any.
24
+ * Used internally by promisesEqual for comparison.
25
+ */
26
+ export function getCombinedPromiseMetadata(
27
+ promise: PromiseLike<unknown>
28
+ ): CombinedPromiseMeta | undefined {
29
+ return combinedPromiseCache.get(promise);
30
+ }
31
+
32
+ /**
33
+ * Create a combined promise with metadata for comparison.
34
+ * If only one promise, returns it directly (no metadata needed).
35
+ */
36
+ export function createCombinedPromise(
37
+ type: "all" | "race" | "allSettled",
38
+ promises: Promise<unknown>[]
39
+ ): PromiseLike<unknown> {
40
+ if (promises.length === 1) {
41
+ // Single promise - no need for metadata, just return it
42
+ // For allSettled, we still need to wrap to prevent rejection propagation
43
+ if (type === "allSettled") {
44
+ const combined = Promise.allSettled(promises).then(() => undefined);
45
+ combinedPromiseCache.set(combined, { type, promises });
46
+ return combined;
47
+ }
48
+ return promises[0];
49
+ }
50
+
51
+ const combined = (Promise[type] as AnyFunc)(promises);
52
+ // Attach no-op catch to prevent unhandled rejection warnings
53
+ combined.catch(() => {});
54
+ combinedPromiseCache.set(combined, { type, promises });
55
+ return combined;
56
+ }
57
+
58
+ /**
59
+ * Compare two promises, considering combined promise metadata.
60
+ * Returns true if promises are considered equal.
61
+ */
62
+ export function promisesEqual(
63
+ a: PromiseLike<unknown> | undefined,
64
+ b: PromiseLike<unknown> | undefined
65
+ ): boolean {
66
+ // Same reference
67
+ if (a === b) return true;
68
+
69
+ // One is undefined
70
+ if (!a || !b) return false;
71
+
72
+ // Compare by metadata (type + source promises array)
73
+ const metaA = getCombinedPromiseMetadata(a);
74
+ const metaB = getCombinedPromiseMetadata(b);
75
+
76
+ return !!metaA && !!metaB && shallow2Equal(metaA, metaB);
77
+ }
3
78
 
4
79
  /**
5
80
  * Represents the state of a tracked Promise.
@@ -98,76 +173,6 @@ export function isDerived<T>(value: unknown): value is DerivedAtom<T, boolean> {
98
173
  );
99
174
  }
100
175
 
101
- /**
102
- * Returns the current state of an atom as a discriminated union.
103
- *
104
- * For DerivedAtom:
105
- * - Returns atom.state() directly (derived atoms track their own state)
106
- *
107
- * For MutableAtom:
108
- * - If value is not a Promise: returns ready state
109
- * - If value is a Promise: tracks and returns its state (ready/error/loading)
110
- *
111
- * @param atom - The atom to get state from
112
- * @returns AtomState discriminated union (ready | error | loading)
113
- *
114
- * @example
115
- * ```ts
116
- * const state = getAtomState(myAtom$);
117
- *
118
- * switch (state.status) {
119
- * case "ready":
120
- * console.log(state.value); // T
121
- * break;
122
- * case "error":
123
- * console.log(state.error);
124
- * break;
125
- * case "loading":
126
- * console.log(state.promise);
127
- * break;
128
- * }
129
- * ```
130
- */
131
- export function getAtomState<T>(atom: Atom<T>): AtomState<Awaited<T>> {
132
- // For derived atoms, use their own state method
133
- if (isDerived<T>(atom)) {
134
- return atom.state() as AtomState<Awaited<T>>;
135
- }
136
-
137
- const value = atom.value;
138
-
139
- // 1. Sync value - ready
140
- if (!isPromiseLike(value)) {
141
- return {
142
- status: "ready",
143
- value: value as Awaited<T>,
144
- };
145
- }
146
-
147
- // 2. Promise value - check state via promiseCache
148
- const state = trackPromise(value);
149
-
150
- switch (state.status) {
151
- case "fulfilled":
152
- return {
153
- status: "ready",
154
- value: state.value as Awaited<T>,
155
- };
156
-
157
- case "rejected":
158
- return {
159
- status: "error",
160
- error: state.error,
161
- };
162
-
163
- case "pending":
164
- return {
165
- status: "loading",
166
- promise: state.promise as Promise<Awaited<T>>,
167
- };
168
- }
169
- }
170
-
171
176
  /**
172
177
  * Unwraps a value that may be a Promise.
173
178
  * - If not a Promise, returns the value directly.