atomirx 0.0.4 → 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.
@@ -1,3 +1,4 @@
1
+ import { Effect } from "./effect";
1
2
  import { hook } from "./hook";
2
3
  import {
3
4
  MutableAtomMeta,
@@ -5,12 +6,13 @@ import {
5
6
  MutableAtom,
6
7
  DerivedAtom,
7
8
  ModuleMeta,
9
+ EffectMeta,
8
10
  } from "./types";
9
11
 
10
12
  /**
11
13
  * Information provided when a mutable atom is created.
12
14
  */
13
- export interface MutableAtomCreateInfo {
15
+ export interface MutableInfo {
14
16
  /** Discriminator for mutable atoms */
15
17
  type: "mutable";
16
18
  /** Optional key from atom options (for debugging/devtools) */
@@ -18,13 +20,13 @@ export interface MutableAtomCreateInfo {
18
20
  /** Optional metadata from atom options */
19
21
  meta: MutableAtomMeta | undefined;
20
22
  /** The created mutable atom instance */
21
- atom: MutableAtom<unknown>;
23
+ instance: MutableAtom<unknown>;
22
24
  }
23
25
 
24
26
  /**
25
27
  * Information provided when a derived atom is created.
26
28
  */
27
- export interface DerivedAtomCreateInfo {
29
+ export interface DerivedInfo {
28
30
  /** Discriminator for derived atoms */
29
31
  type: "derived";
30
32
  /** Optional key from derived options (for debugging/devtools) */
@@ -32,18 +34,32 @@ export interface DerivedAtomCreateInfo {
32
34
  /** Optional metadata from derived options */
33
35
  meta: DerivedAtomMeta | undefined;
34
36
  /** The created derived atom instance */
35
- atom: DerivedAtom<unknown, boolean>;
37
+ instance: DerivedAtom<unknown, boolean>;
36
38
  }
37
39
 
38
40
  /**
39
- * Union type for atom creation info (mutable or derived).
41
+ * Information provided when an effect is created.
40
42
  */
41
- export type AtomCreateInfo = MutableAtomCreateInfo | DerivedAtomCreateInfo;
43
+ export interface EffectInfo {
44
+ /** Discriminator for effects */
45
+ type: "effect";
46
+ /** Optional key from effect options (for debugging/devtools) */
47
+ key: string | undefined;
48
+ /** Optional metadata from effect options */
49
+ meta: EffectMeta | undefined;
50
+ /** The created effect instance */
51
+ instance: Effect;
52
+ }
53
+
54
+ /**
55
+ * Union type for atom/derived/effect creation info.
56
+ */
57
+ export type CreateInfo = MutableInfo | DerivedInfo | EffectInfo;
42
58
 
43
59
  /**
44
60
  * Information provided when a module (via define()) is created.
45
61
  */
46
- export interface ModuleCreateInfo {
62
+ export interface ModuleInfo {
47
63
  /** Discriminator for modules */
48
64
  type: "module";
49
65
  /** Optional key from define options (for debugging/devtools) */
@@ -51,7 +67,7 @@ export interface ModuleCreateInfo {
51
67
  /** Optional metadata from define options */
52
68
  meta: ModuleMeta | undefined;
53
69
  /** The created module instance */
54
- module: unknown;
70
+ instance: unknown;
55
71
  }
56
72
 
57
73
  /**
@@ -62,31 +78,30 @@ export interface ModuleCreateInfo {
62
78
  * - **Debugging** - log atom creation for troubleshooting
63
79
  * - **Testing** - verify expected atoms are created
64
80
  *
81
+ * **IMPORTANT**: Always use `.override()` to preserve the hook chain.
82
+ * Direct assignment to `.current` will break existing handlers.
83
+ *
65
84
  * @example Basic logging
66
85
  * ```ts
67
- * onCreateHook.current = (info) => {
86
+ * onCreateHook.override((prev) => (info) => {
87
+ * prev?.(info); // call existing handlers first
68
88
  * console.log(`Created ${info.type}: ${info.key ?? "anonymous"}`);
69
- * };
89
+ * });
70
90
  * ```
71
91
  *
72
92
  * @example DevTools integration
73
93
  * ```ts
74
- * const atoms = new Map();
75
- * const modules = new Map();
94
+ * const registry = new Map();
76
95
  *
77
- * onCreateHook.current = (info) => {
78
- * if (info.type === "module") {
79
- * modules.set(info.key, info.module);
80
- * } else {
81
- * atoms.set(info.key, info.atom);
82
- * }
83
- * };
96
+ * onCreateHook.override((prev) => (info) => {
97
+ * prev?.(info); // preserve chain
98
+ * registry.set(info.key, info.instance);
99
+ * });
84
100
  * ```
85
101
  *
86
- * @example Cleanup (disable hook)
102
+ * @example Reset to default (disable all handlers)
87
103
  * ```ts
88
- * onCreateHook.current = undefined;
104
+ * onCreateHook.reset();
89
105
  * ```
90
106
  */
91
- export const onCreateHook =
92
- hook<(info: AtomCreateInfo | ModuleCreateInfo) => void>();
107
+ export const onCreateHook = hook<(info: CreateInfo | ModuleInfo) => void>();
@@ -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>();
package/src/core/types.ts CHANGED
@@ -47,9 +47,8 @@ export interface Pipeable {
47
47
  /**
48
48
  * Optional metadata for atoms.
49
49
  */
50
- export interface AtomMeta {
50
+ export interface AtomMeta extends AtomirxMeta {
51
51
  key?: string;
52
- [key: string]: unknown;
53
52
  }
54
53
 
55
54
  /**
@@ -278,13 +277,64 @@ export interface DerivedOptions<T> {
278
277
  meta?: DerivedAtomMeta;
279
278
  /** Equality strategy for change detection (default: "strict") */
280
279
  equals?: Equality<T>;
280
+ /**
281
+ * Callback invoked when the derived computation throws an error.
282
+ * This is called for actual errors, NOT for Promise throws (Suspense).
283
+ *
284
+ * @param error - The error thrown during computation
285
+ *
286
+ * @example
287
+ * ```ts
288
+ * const data$ = derived(
289
+ * ({ read }) => {
290
+ * const raw = read(source$);
291
+ * return JSON.parse(raw); // May throw SyntaxError
292
+ * },
293
+ * {
294
+ * onError: (error) => {
295
+ * console.error('Derived computation failed:', error);
296
+ * reportToSentry(error);
297
+ * }
298
+ * }
299
+ * );
300
+ * ```
301
+ */
302
+ onError?: (error: unknown) => void;
281
303
  }
282
304
 
283
305
  /**
284
306
  * Configuration options for effects.
285
307
  */
286
308
  export interface EffectOptions {
287
- /** Optional key for debugging */
309
+ meta?: EffectMeta;
310
+ /**
311
+ * Callback invoked when the effect computation throws an error.
312
+ * This is called for actual errors, NOT for Promise throws (Suspense).
313
+ *
314
+ * @param error - The error thrown during effect execution
315
+ *
316
+ * @example
317
+ * ```ts
318
+ * effect(
319
+ * ({ read }) => {
320
+ * const data = read(source$);
321
+ * riskyOperation(data); // May throw
322
+ * },
323
+ * {
324
+ * onError: (error) => {
325
+ * console.error('Effect failed:', error);
326
+ * showErrorNotification(error);
327
+ * }
328
+ * }
329
+ * );
330
+ * ```
331
+ */
332
+ onError?: (error: unknown) => void;
333
+ }
334
+
335
+ export interface AtomirxMeta {}
336
+
337
+ export interface EffectMeta extends AtomirxMeta {
288
338
  key?: string;
289
339
  }
290
340