atomirx 0.0.8 → 0.1.0

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 (138) hide show
  1. package/README.md +198 -2234
  2. package/bin/cli.js +90 -0
  3. package/dist/core/derived.d.ts +2 -2
  4. package/dist/core/effect.d.ts +3 -2
  5. package/dist/core/onCreateHook.d.ts +15 -2
  6. package/dist/core/onErrorHook.d.ts +4 -1
  7. package/dist/core/pool.d.ts +78 -0
  8. package/dist/core/pool.test.d.ts +1 -0
  9. package/dist/core/select-boolean.test.d.ts +1 -0
  10. package/dist/core/select-pool.test.d.ts +1 -0
  11. package/dist/core/select.d.ts +278 -86
  12. package/dist/core/types.d.ts +233 -1
  13. package/dist/core/withAbort.d.ts +95 -0
  14. package/dist/core/withReady.d.ts +3 -3
  15. package/dist/devtools/constants.d.ts +41 -0
  16. package/dist/devtools/index.cjs +1 -0
  17. package/dist/devtools/index.d.ts +29 -0
  18. package/dist/devtools/index.js +429 -0
  19. package/dist/devtools/registry.d.ts +98 -0
  20. package/dist/devtools/registry.test.d.ts +1 -0
  21. package/dist/devtools/setup.d.ts +61 -0
  22. package/dist/devtools/types.d.ts +311 -0
  23. package/dist/index-BZEnfIcB.cjs +1 -0
  24. package/dist/index-BbPZhsDl.js +1653 -0
  25. package/dist/index.cjs +1 -1
  26. package/dist/index.d.ts +4 -3
  27. package/dist/index.js +18 -14
  28. package/dist/onDispatchHook-C8yLzr-o.cjs +1 -0
  29. package/dist/onDispatchHook-SKbiIUaJ.js +5 -0
  30. package/dist/onErrorHook-BGGy3tqK.js +38 -0
  31. package/dist/onErrorHook-DHBASmYw.cjs +1 -0
  32. package/dist/react/index.cjs +1 -1
  33. package/dist/react/index.js +191 -151
  34. package/dist/react/onDispatchHook.d.ts +106 -0
  35. package/dist/react/useAction.d.ts +4 -1
  36. package/dist/react-devtools/DevToolsPanel.d.ts +93 -0
  37. package/dist/react-devtools/EntityDetails.d.ts +10 -0
  38. package/dist/react-devtools/EntityList.d.ts +15 -0
  39. package/dist/react-devtools/LogList.d.ts +12 -0
  40. package/dist/react-devtools/hooks.d.ts +50 -0
  41. package/dist/react-devtools/index.cjs +1 -0
  42. package/dist/react-devtools/index.d.ts +31 -0
  43. package/dist/react-devtools/index.js +1589 -0
  44. package/dist/react-devtools/styles.d.ts +148 -0
  45. package/package.json +26 -2
  46. package/skills/atomirx/SKILL.md +456 -0
  47. package/skills/atomirx/references/async-patterns.md +188 -0
  48. package/skills/atomirx/references/atom-patterns.md +238 -0
  49. package/skills/atomirx/references/deferred-loading.md +191 -0
  50. package/skills/atomirx/references/derived-patterns.md +428 -0
  51. package/skills/atomirx/references/effect-patterns.md +426 -0
  52. package/skills/atomirx/references/error-handling.md +140 -0
  53. package/skills/atomirx/references/hooks.md +322 -0
  54. package/skills/atomirx/references/pool-patterns.md +229 -0
  55. package/skills/atomirx/references/react-integration.md +411 -0
  56. package/skills/atomirx/references/rules.md +407 -0
  57. package/skills/atomirx/references/select-context.md +309 -0
  58. package/skills/atomirx/references/service-template.md +172 -0
  59. package/skills/atomirx/references/store-template.md +205 -0
  60. package/skills/atomirx/references/testing-patterns.md +431 -0
  61. package/coverage/base.css +0 -224
  62. package/coverage/block-navigation.js +0 -87
  63. package/coverage/clover.xml +0 -1440
  64. package/coverage/coverage-final.json +0 -14
  65. package/coverage/favicon.png +0 -0
  66. package/coverage/index.html +0 -131
  67. package/coverage/prettify.css +0 -1
  68. package/coverage/prettify.js +0 -2
  69. package/coverage/sort-arrow-sprite.png +0 -0
  70. package/coverage/sorter.js +0 -210
  71. package/coverage/src/core/atom.ts.html +0 -889
  72. package/coverage/src/core/batch.ts.html +0 -223
  73. package/coverage/src/core/define.ts.html +0 -805
  74. package/coverage/src/core/emitter.ts.html +0 -919
  75. package/coverage/src/core/equality.ts.html +0 -631
  76. package/coverage/src/core/hook.ts.html +0 -460
  77. package/coverage/src/core/index.html +0 -281
  78. package/coverage/src/core/isAtom.ts.html +0 -100
  79. package/coverage/src/core/isPromiseLike.ts.html +0 -133
  80. package/coverage/src/core/onCreateHook.ts.html +0 -138
  81. package/coverage/src/core/scheduleNotifyHook.ts.html +0 -94
  82. package/coverage/src/core/types.ts.html +0 -523
  83. package/coverage/src/core/withUse.ts.html +0 -253
  84. package/coverage/src/index.html +0 -116
  85. package/coverage/src/index.ts.html +0 -106
  86. package/dist/index-CBVj1kSj.js +0 -1350
  87. package/dist/index-Cxk9v0um.cjs +0 -1
  88. package/scripts/publish.js +0 -198
  89. package/src/core/atom.test.ts +0 -633
  90. package/src/core/atom.ts +0 -311
  91. package/src/core/atomState.test.ts +0 -342
  92. package/src/core/atomState.ts +0 -256
  93. package/src/core/batch.test.ts +0 -257
  94. package/src/core/batch.ts +0 -172
  95. package/src/core/define.test.ts +0 -343
  96. package/src/core/define.ts +0 -243
  97. package/src/core/derived.test.ts +0 -1215
  98. package/src/core/derived.ts +0 -450
  99. package/src/core/effect.test.ts +0 -802
  100. package/src/core/effect.ts +0 -188
  101. package/src/core/emitter.test.ts +0 -364
  102. package/src/core/emitter.ts +0 -392
  103. package/src/core/equality.test.ts +0 -392
  104. package/src/core/equality.ts +0 -182
  105. package/src/core/getAtomState.ts +0 -69
  106. package/src/core/hook.test.ts +0 -227
  107. package/src/core/hook.ts +0 -177
  108. package/src/core/isAtom.ts +0 -27
  109. package/src/core/isPromiseLike.test.ts +0 -72
  110. package/src/core/isPromiseLike.ts +0 -16
  111. package/src/core/onCreateHook.ts +0 -107
  112. package/src/core/onErrorHook.test.ts +0 -350
  113. package/src/core/onErrorHook.ts +0 -52
  114. package/src/core/promiseCache.test.ts +0 -241
  115. package/src/core/promiseCache.ts +0 -284
  116. package/src/core/scheduleNotifyHook.ts +0 -53
  117. package/src/core/select.ts +0 -729
  118. package/src/core/selector.test.ts +0 -799
  119. package/src/core/types.ts +0 -389
  120. package/src/core/withReady.test.ts +0 -534
  121. package/src/core/withReady.ts +0 -191
  122. package/src/core/withUse.test.ts +0 -249
  123. package/src/core/withUse.ts +0 -56
  124. package/src/index.test.ts +0 -80
  125. package/src/index.ts +0 -65
  126. package/src/react/index.ts +0 -21
  127. package/src/react/rx.test.tsx +0 -571
  128. package/src/react/rx.tsx +0 -531
  129. package/src/react/strictModeTest.tsx +0 -71
  130. package/src/react/useAction.test.ts +0 -987
  131. package/src/react/useAction.ts +0 -607
  132. package/src/react/useSelector.test.ts +0 -182
  133. package/src/react/useSelector.ts +0 -292
  134. package/src/react/useStable.test.ts +0 -553
  135. package/src/react/useStable.ts +0 -288
  136. package/tsconfig.json +0 -9
  137. package/v2.md +0 -725
  138. package/vite.config.ts +0 -42
@@ -1,987 +0,0 @@
1
- import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2
- import { useAction } from "./useAction";
3
- import { atom } from "../core/atom";
4
- import { act } from "react";
5
- import { wrappers } from "./strictModeTest";
6
-
7
- describe.each(wrappers)("useAction - $mode", ({ mode, renderHook }) => {
8
- beforeEach(() => {
9
- vi.useFakeTimers();
10
- });
11
-
12
- afterEach(() => {
13
- vi.useRealTimers();
14
- });
15
-
16
- describe("initial state", () => {
17
- it("should start with idle status when lazy is true (default)", () => {
18
- const fn = vi.fn(() => "result");
19
-
20
- const { result } = renderHook(() => useAction(fn));
21
-
22
- expect(result.current.status).toBe("idle");
23
- expect(result.current.result).toBeUndefined();
24
- expect(result.current.error).toBeUndefined();
25
- expect(fn).not.toHaveBeenCalled();
26
- });
27
-
28
- it("should execute on mount when lazy is false", () => {
29
- const fn = vi.fn(() => "result");
30
-
31
- const { result } = renderHook(() => useAction(fn, { lazy: false }));
32
-
33
- // Sync function completes immediately, so status is success
34
- expect(result.current.status).toBe("success");
35
- expect(result.current.result).toBe("result");
36
- // In strict mode, effects run twice
37
- expect(fn).toHaveBeenCalledTimes(mode === "strict" ? 2 : 1);
38
- });
39
-
40
- it("should start with loading status when lazy is false with async fn", () => {
41
- const fn = vi.fn(
42
- () =>
43
- new Promise<string>((resolve) => {
44
- setTimeout(() => resolve("result"), 1000);
45
- })
46
- );
47
-
48
- const { result } = renderHook(() => useAction(fn, { lazy: false }));
49
-
50
- expect(result.current.status).toBe("loading");
51
- // In strict mode, effects run twice
52
- expect(fn).toHaveBeenCalledTimes(mode === "strict" ? 2 : 1);
53
- });
54
- });
55
-
56
- describe("return shape", () => {
57
- it("should return callable function with status, result, error, abort, reset properties", () => {
58
- const fn = vi.fn(() => "result");
59
-
60
- const { result } = renderHook(() => useAction(fn));
61
-
62
- // Should be callable
63
- expect(typeof result.current).toBe("function");
64
- // Should have state properties
65
- expect(result.current).toHaveProperty("status");
66
- expect(result.current).toHaveProperty("result");
67
- expect(result.current).toHaveProperty("error");
68
- // Should have API methods
69
- expect(result.current).toHaveProperty("abort");
70
- expect(typeof result.current.abort).toBe("function");
71
- expect(result.current).toHaveProperty("reset");
72
- expect(typeof result.current.reset).toBe("function");
73
- });
74
- });
75
-
76
- describe("synchronous actions", () => {
77
- it("should transition to success on sync function", () => {
78
- const fn = vi.fn(() => "sync-result");
79
-
80
- const { result } = renderHook(() => useAction(fn));
81
-
82
- act(() => {
83
- result.current();
84
- });
85
-
86
- expect(result.current.status).toBe("success");
87
- expect(result.current.result).toBe("sync-result");
88
- expect(result.current.error).toBeUndefined();
89
- });
90
-
91
- it("should transition to error when sync function throws", async () => {
92
- const error = new Error("sync-error");
93
- const fn = vi.fn((): void => {
94
- throw error;
95
- });
96
-
97
- const { result } = renderHook(() => useAction(fn));
98
-
99
- await act(async () => {
100
- // calling action returns rejected promise, doesn't throw
101
- const promise = result.current();
102
- await expect(promise).rejects.toBe(error);
103
- });
104
-
105
- expect(result.current.status).toBe("error");
106
- expect(result.current.result).toBeUndefined();
107
- expect(result.current.error).toBe(error);
108
- });
109
-
110
- it("should return rejected promise for sync errors (no re-throw)", async () => {
111
- const error = new Error("sync-error");
112
- const fn = vi.fn((): string => {
113
- throw error;
114
- });
115
-
116
- const { result } = renderHook(() => useAction(fn));
117
-
118
- await act(async () => {
119
- // Does not throw synchronously
120
- const promise = result.current();
121
- // But the promise rejects
122
- await expect(promise).rejects.toBe(error);
123
- });
124
- });
125
-
126
- it("should return promise that resolves with sync result", async () => {
127
- const fn = vi.fn(() => 42);
128
-
129
- const { result } = renderHook(() => useAction(fn));
130
-
131
- let returnValue: number | undefined;
132
- await act(async () => {
133
- returnValue = await result.current();
134
- });
135
-
136
- expect(returnValue).toBe(42);
137
- });
138
-
139
- it("should return abortable promise even for sync functions", () => {
140
- const fn = vi.fn(() => 42);
141
-
142
- const { result } = renderHook(() => useAction(fn));
143
-
144
- let promise: PromiseLike<number> & { abort: () => void };
145
- act(() => {
146
- promise = result.current();
147
- });
148
-
149
- expect(promise!).toHaveProperty("abort");
150
- expect(typeof promise!.abort).toBe("function");
151
- expect(typeof promise!.then).toBe("function");
152
- });
153
- });
154
-
155
- describe("asynchronous actions", () => {
156
- it("should transition through loading to success", async () => {
157
- let resolve: (value: string) => void;
158
- const promise = new Promise<string>((r) => {
159
- resolve = r;
160
- });
161
- const fn = vi.fn(() => promise);
162
-
163
- const { result } = renderHook(() => useAction(fn));
164
-
165
- act(() => {
166
- result.current();
167
- });
168
-
169
- expect(result.current.status).toBe("loading");
170
- expect(result.current.result).toBeUndefined();
171
- expect(result.current.error).toBeUndefined();
172
-
173
- await act(async () => {
174
- resolve!("async-result");
175
- await promise;
176
- });
177
-
178
- expect(result.current.status).toBe("success");
179
- expect(result.current.result).toBe("async-result");
180
- expect(result.current.error).toBeUndefined();
181
- });
182
-
183
- it("should transition through loading to error on rejection", async () => {
184
- const error = new Error("async-error");
185
- let reject: (error: Error) => void;
186
- const promise = new Promise<string>((_, r) => {
187
- reject = r;
188
- });
189
- const fn = vi.fn(() => promise);
190
-
191
- const { result } = renderHook(() => useAction(fn));
192
-
193
- act(() => {
194
- result.current();
195
- });
196
-
197
- expect(result.current.status).toBe("loading");
198
-
199
- await act(async () => {
200
- reject!(error);
201
- await promise.catch(() => {});
202
- });
203
-
204
- expect(result.current.status).toBe("error");
205
- expect(result.current.result).toBeUndefined();
206
- expect(result.current.error).toBe(error);
207
- });
208
-
209
- it("should return abortable promise from action call", async () => {
210
- let resolve: (value: string) => void;
211
- const promise = new Promise<string>((r) => {
212
- resolve = r;
213
- });
214
- const fn = vi.fn(() => promise);
215
-
216
- const { result } = renderHook(() => useAction(fn));
217
-
218
- let returnValue: PromiseLike<string> & { abort: () => void };
219
- act(() => {
220
- returnValue = result.current() as any;
221
- });
222
-
223
- expect(returnValue!).toHaveProperty("abort");
224
- expect(typeof returnValue!.abort).toBe("function");
225
- expect(typeof returnValue!.then).toBe("function");
226
-
227
- await act(async () => {
228
- resolve!("result");
229
- await returnValue;
230
- });
231
- });
232
- });
233
-
234
- describe("AbortSignal", () => {
235
- it("should pass AbortSignal to the function", () => {
236
- const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
237
- expect(signal).toBeInstanceOf(AbortSignal);
238
- return "result";
239
- });
240
-
241
- const { result } = renderHook(() => useAction(fn));
242
-
243
- act(() => {
244
- result.current();
245
- });
246
-
247
- expect(fn).toHaveBeenCalledWith(
248
- expect.objectContaining({ signal: expect.any(AbortSignal) })
249
- );
250
- });
251
-
252
- it("should create new AbortSignal per call", () => {
253
- const signals: AbortSignal[] = [];
254
- const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
255
- signals.push(signal);
256
- return "result";
257
- });
258
-
259
- const { result } = renderHook(() => useAction(fn, { exclusive: false }));
260
-
261
- act(() => {
262
- result.current();
263
- result.current();
264
- });
265
-
266
- expect(signals).toHaveLength(2);
267
- expect(signals[0]).not.toBe(signals[1]);
268
- });
269
-
270
- it("should abort previous signal when exclusive is true (default)", async () => {
271
- const signals: AbortSignal[] = [];
272
- const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
273
- signals.push(signal);
274
- return new Promise<string>((resolve) => {
275
- setTimeout(() => resolve("result"), 1000);
276
- });
277
- });
278
-
279
- const { result } = renderHook(() => useAction(fn));
280
-
281
- act(() => {
282
- result.current();
283
- });
284
-
285
- expect(signals[0].aborted).toBe(false);
286
-
287
- act(() => {
288
- result.current();
289
- });
290
-
291
- expect(signals[0].aborted).toBe(true);
292
- expect(signals[1].aborted).toBe(false);
293
- });
294
-
295
- it("should NOT abort previous signal when exclusive is false", () => {
296
- const signals: AbortSignal[] = [];
297
- const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
298
- signals.push(signal);
299
- return new Promise<string>((resolve) => {
300
- setTimeout(() => resolve("result"), 1000);
301
- });
302
- });
303
-
304
- const { result } = renderHook(() => useAction(fn, { exclusive: false }));
305
-
306
- act(() => {
307
- result.current();
308
- result.current();
309
- });
310
-
311
- expect(signals[0].aborted).toBe(false);
312
- expect(signals[1].aborted).toBe(false);
313
- });
314
-
315
- it("should abort on unmount when exclusive is true", () => {
316
- let capturedSignal: AbortSignal;
317
- const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
318
- capturedSignal = signal;
319
- return new Promise<string>((resolve) => {
320
- setTimeout(() => resolve("result"), 1000);
321
- });
322
- });
323
-
324
- const { result, unmount } = renderHook(() => useAction(fn));
325
-
326
- act(() => {
327
- result.current();
328
- });
329
-
330
- expect(capturedSignal!.aborted).toBe(false);
331
-
332
- unmount();
333
-
334
- expect(capturedSignal!.aborted).toBe(true);
335
- });
336
-
337
- it("should NOT abort on unmount when exclusive is false", () => {
338
- let capturedSignal: AbortSignal;
339
- const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
340
- capturedSignal = signal;
341
- return new Promise<string>((resolve) => {
342
- setTimeout(() => resolve("result"), 1000);
343
- });
344
- });
345
-
346
- const { result, unmount } = renderHook(() =>
347
- useAction(fn, { exclusive: false })
348
- );
349
-
350
- act(() => {
351
- result.current();
352
- });
353
-
354
- expect(capturedSignal!.aborted).toBe(false);
355
-
356
- unmount();
357
-
358
- expect(capturedSignal!.aborted).toBe(false);
359
- });
360
-
361
- it("should allow manual abort via abort() method", () => {
362
- let capturedSignal: AbortSignal;
363
- const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
364
- capturedSignal = signal;
365
- return new Promise<string>((resolve) => {
366
- setTimeout(() => resolve("result"), 1000);
367
- });
368
- });
369
-
370
- const { result } = renderHook(() => useAction(fn, { exclusive: false }));
371
-
372
- act(() => {
373
- result.current();
374
- });
375
-
376
- expect(capturedSignal!.aborted).toBe(false);
377
-
378
- act(() => {
379
- result.current.abort();
380
- });
381
-
382
- expect(capturedSignal!.aborted).toBe(true);
383
- });
384
-
385
- it("should allow manual abort via returned promise.abort()", () => {
386
- let capturedSignal: AbortSignal;
387
- const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
388
- capturedSignal = signal;
389
- return new Promise<string>((resolve) => {
390
- setTimeout(() => resolve("result"), 1000);
391
- });
392
- });
393
-
394
- const { result } = renderHook(() => useAction(fn, { exclusive: false }));
395
-
396
- let returnedPromise: PromiseLike<string> & { abort: () => void };
397
- act(() => {
398
- returnedPromise = result.current() as any;
399
- });
400
-
401
- expect(capturedSignal!.aborted).toBe(false);
402
-
403
- act(() => {
404
- returnedPromise.abort();
405
- });
406
-
407
- expect(capturedSignal!.aborted).toBe(true);
408
- });
409
- });
410
-
411
- describe("reset", () => {
412
- it("should reset state to idle after success", async () => {
413
- const fn = vi.fn(() => "result");
414
-
415
- const { result } = renderHook(() => useAction(fn));
416
-
417
- await act(async () => {
418
- await result.current();
419
- });
420
-
421
- expect(result.current.status).toBe("success");
422
- expect(result.current.result).toBe("result");
423
-
424
- act(() => {
425
- result.current.reset();
426
- });
427
-
428
- expect(result.current.status).toBe("idle");
429
- expect(result.current.result).toBeUndefined();
430
- expect(result.current.error).toBeUndefined();
431
- });
432
-
433
- it("should reset state to idle after error", async () => {
434
- const error = new Error("test-error");
435
- const fn = vi.fn(() => {
436
- throw error;
437
- });
438
-
439
- const { result } = renderHook(() => useAction(fn));
440
-
441
- await act(async () => {
442
- await result.current().then(
443
- () => {},
444
- () => {}
445
- );
446
- });
447
-
448
- expect(result.current.status).toBe("error");
449
- expect(result.current.error).toBe(error);
450
-
451
- act(() => {
452
- result.current.reset();
453
- });
454
-
455
- expect(result.current.status).toBe("idle");
456
- expect(result.current.result).toBeUndefined();
457
- expect(result.current.error).toBeUndefined();
458
- });
459
-
460
- it("should abort in-flight request when reset is called with exclusive: true", () => {
461
- let capturedSignal: AbortSignal;
462
- const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
463
- capturedSignal = signal;
464
- return new Promise<string>((resolve) => {
465
- setTimeout(() => resolve("result"), 1000);
466
- });
467
- });
468
-
469
- const { result } = renderHook(() => useAction(fn));
470
-
471
- act(() => {
472
- result.current();
473
- });
474
-
475
- expect(result.current.status).toBe("loading");
476
- expect(capturedSignal!.aborted).toBe(false);
477
-
478
- act(() => {
479
- result.current.reset();
480
- });
481
-
482
- expect(result.current.status).toBe("idle");
483
- expect(capturedSignal!.aborted).toBe(true);
484
- });
485
-
486
- it("should NOT abort in-flight request when reset is called with exclusive: false", () => {
487
- let capturedSignal: AbortSignal;
488
- const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
489
- capturedSignal = signal;
490
- return new Promise<string>((resolve) => {
491
- setTimeout(() => resolve("result"), 1000);
492
- });
493
- });
494
-
495
- const { result } = renderHook(() => useAction(fn, { exclusive: false }));
496
-
497
- act(() => {
498
- result.current();
499
- });
500
-
501
- expect(result.current.status).toBe("loading");
502
- expect(capturedSignal!.aborted).toBe(false);
503
-
504
- act(() => {
505
- result.current.reset();
506
- });
507
-
508
- // State is reset but request continues
509
- expect(result.current.status).toBe("idle");
510
- expect(capturedSignal!.aborted).toBe(false);
511
- });
512
-
513
- it("should allow re-dispatch after reset", async () => {
514
- let counter = 0;
515
- const fn = vi.fn(() => ++counter);
516
-
517
- const { result } = renderHook(() => useAction(fn));
518
-
519
- await act(async () => {
520
- await result.current();
521
- });
522
-
523
- expect(result.current.result).toBe(1);
524
-
525
- act(() => {
526
- result.current.reset();
527
- });
528
-
529
- expect(result.current.status).toBe("idle");
530
-
531
- await act(async () => {
532
- await result.current();
533
- });
534
-
535
- expect(result.current.status).toBe("success");
536
- expect(result.current.result).toBe(2);
537
- });
538
- });
539
-
540
- describe("eager execution (lazy: false)", () => {
541
- it("should execute on mount when lazy is false", () => {
542
- const fn = vi.fn(() => "result");
543
-
544
- renderHook(() => useAction(fn, { lazy: false }));
545
-
546
- // In strict mode, effects run twice
547
- expect(fn).toHaveBeenCalledTimes(mode === "strict" ? 2 : 1);
548
- });
549
-
550
- it("should re-execute when deps change", () => {
551
- const fn = vi.fn(() => "result");
552
-
553
- const { rerender } = renderHook(
554
- ({ dep }) => useAction(fn, { lazy: false, deps: [dep] }),
555
- { initialProps: { dep: 1 } }
556
- );
557
-
558
- // In strict mode, effects run twice on mount
559
- const initialCalls = mode === "strict" ? 2 : 1;
560
- expect(fn).toHaveBeenCalledTimes(initialCalls);
561
-
562
- rerender({ dep: 2 });
563
-
564
- // +1 for dep change
565
- expect(fn).toHaveBeenCalledTimes(initialCalls + 1);
566
- });
567
-
568
- it("should NOT re-execute when deps are the same", () => {
569
- const fn = vi.fn(() => "result");
570
-
571
- const { rerender } = renderHook(
572
- ({ dep }) => useAction(fn, { lazy: false, deps: [dep] }),
573
- { initialProps: { dep: 1 } }
574
- );
575
-
576
- // In strict mode, effects run twice on mount
577
- const initialCalls = mode === "strict" ? 2 : 1;
578
- expect(fn).toHaveBeenCalledTimes(initialCalls);
579
-
580
- rerender({ dep: 1 });
581
-
582
- // No additional calls when deps are the same
583
- expect(fn).toHaveBeenCalledTimes(initialCalls);
584
- });
585
-
586
- it("should abort previous when deps change and exclusive is true", async () => {
587
- const signals: AbortSignal[] = [];
588
- const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
589
- signals.push(signal);
590
- return new Promise<string>((resolve) => {
591
- setTimeout(() => resolve("result"), 1000);
592
- });
593
- });
594
-
595
- const { rerender } = renderHook(
596
- ({ dep }) => useAction(fn, { lazy: false, deps: [dep] }),
597
- { initialProps: { dep: 1 } }
598
- );
599
-
600
- // In strict mode, first signal is aborted by second mount effect
601
- const firstSignalIndex = mode === "strict" ? 1 : 0;
602
- expect(signals[firstSignalIndex].aborted).toBe(false);
603
-
604
- rerender({ dep: 2 });
605
-
606
- // Previous signal should be aborted
607
- expect(signals[firstSignalIndex].aborted).toBe(true);
608
- // New signal should not be aborted
609
- expect(signals[signals.length - 1].aborted).toBe(false);
610
- });
611
- });
612
-
613
- describe("stale closure prevention", () => {
614
- it("should ignore stale async results", async () => {
615
- let resolvers: Array<(value: string) => void> = [];
616
- const fn = vi.fn(
617
- () =>
618
- new Promise<string>((resolve) => {
619
- resolvers.push(resolve);
620
- })
621
- );
622
-
623
- const { result } = renderHook(() => useAction(fn));
624
-
625
- // First call
626
- act(() => {
627
- result.current();
628
- });
629
-
630
- // Second call (should abort first)
631
- act(() => {
632
- result.current();
633
- });
634
-
635
- // Resolve first (stale) - should be ignored
636
- await act(async () => {
637
- resolvers[0]("first-result");
638
- await Promise.resolve();
639
- });
640
-
641
- // State should still be loading (waiting for second)
642
- expect(result.current.status).toBe("loading");
643
-
644
- // Resolve second
645
- await act(async () => {
646
- resolvers[1]("second-result");
647
- await Promise.resolve();
648
- });
649
-
650
- expect(result.current.status).toBe("success");
651
- expect(result.current.result).toBe("second-result");
652
- });
653
- });
654
-
655
- describe("edge cases", () => {
656
- it("should handle rapid calls", async () => {
657
- let counter = 0;
658
- const fn = vi.fn(
659
- () =>
660
- new Promise<number>((resolve) => {
661
- const current = ++counter;
662
- setTimeout(() => resolve(current), 100);
663
- })
664
- );
665
-
666
- const { result } = renderHook(() => useAction(fn));
667
-
668
- // Rapid fire calls
669
- act(() => {
670
- result.current();
671
- result.current();
672
- result.current();
673
- });
674
-
675
- await act(async () => {
676
- vi.advanceTimersByTime(100);
677
- await Promise.resolve();
678
- });
679
-
680
- // Only the last result should be used
681
- expect(result.current.status).toBe("success");
682
- expect(result.current.result).toBe(3);
683
- });
684
-
685
- it("should handle re-call after error", async () => {
686
- let shouldFail = true;
687
- const fn = vi.fn(() => {
688
- if (shouldFail) {
689
- throw new Error("fail");
690
- }
691
- return "success";
692
- });
693
-
694
- const { result } = renderHook(() => useAction(fn));
695
-
696
- await act(async () => {
697
- // First call returns rejected promise
698
- await result.current().then(
699
- () => {},
700
- () => {}
701
- );
702
- });
703
-
704
- expect(result.current.status).toBe("error");
705
-
706
- shouldFail = false;
707
-
708
- await act(async () => {
709
- await result.current();
710
- });
711
-
712
- expect(result.current.status).toBe("success");
713
- expect(result.current.result).toBe("success");
714
- });
715
-
716
- it("should handle re-call after success", async () => {
717
- let counter = 0;
718
- const fn = vi.fn(() => ++counter);
719
-
720
- const { result } = renderHook(() => useAction(fn));
721
-
722
- await act(async () => {
723
- await result.current();
724
- });
725
-
726
- expect(result.current.result).toBe(1);
727
-
728
- await act(async () => {
729
- await result.current();
730
- });
731
-
732
- expect(result.current.result).toBe(2);
733
- });
734
- });
735
-
736
- describe("chaining operations", () => {
737
- it("should support async/await chaining with sync functions", async () => {
738
- const fn1 = vi.fn(() => 1);
739
- const fn2 = vi.fn(() => 2);
740
-
741
- const { result: result1 } = renderHook(() => useAction(fn1));
742
- const { result: result2 } = renderHook(() => useAction(fn2));
743
-
744
- let values: number[] = [];
745
- await act(async () => {
746
- const value1 = await result1.current();
747
- values.push(value1);
748
- const value2 = await result2.current();
749
- values.push(value2);
750
- });
751
-
752
- expect(values).toEqual([1, 2]);
753
- });
754
-
755
- it("should support async/await chaining with async functions", async () => {
756
- const fn1 = vi.fn(async () => {
757
- return 1;
758
- });
759
- const fn2 = vi.fn(async () => {
760
- return 2;
761
- });
762
-
763
- const { result: result1 } = renderHook(() => useAction(fn1));
764
- const { result: result2 } = renderHook(() => useAction(fn2));
765
-
766
- let values: number[] = [];
767
- await act(async () => {
768
- values.push(await result1.current());
769
- values.push(await result2.current());
770
- });
771
-
772
- expect(values).toEqual([1, 2]);
773
- });
774
-
775
- it("should stop chain on error with try/catch", async () => {
776
- const error = new Error("chain-error");
777
- const fn1 = vi.fn(() => {
778
- throw error;
779
- });
780
- const fn2 = vi.fn(() => "should not run");
781
-
782
- const { result: result1 } = renderHook(() => useAction(fn1));
783
- const { result: result2 } = renderHook(() => useAction(fn2));
784
-
785
- let caughtError: unknown;
786
- await act(async () => {
787
- try {
788
- await result1.current();
789
- await result2.current();
790
- } catch (e) {
791
- caughtError = e;
792
- }
793
- });
794
-
795
- expect(caughtError).toBe(error);
796
- expect(fn2).not.toHaveBeenCalled();
797
- });
798
- });
799
-
800
- describe("atom deps", () => {
801
- it("should execute when atom in deps changes", () => {
802
- const userId = atom(1);
803
- const fn = vi.fn(() => `user-${userId.get()}`);
804
-
805
- const { result } = renderHook(() =>
806
- useAction(fn, { lazy: false, deps: [userId] })
807
- );
808
-
809
- // In strict mode, effects run twice on mount
810
- const initialCalls = mode === "strict" ? 2 : 1;
811
- expect(fn).toHaveBeenCalledTimes(initialCalls);
812
- expect(result.current.result).toBe("user-1");
813
-
814
- // Change atom value
815
- act(() => {
816
- userId.set(2);
817
- });
818
-
819
- expect(fn).toHaveBeenCalledTimes(initialCalls + 1);
820
- expect(result.current.result).toBe("user-2");
821
- });
822
-
823
- it("should NOT re-execute when atom value is shallowly equal", () => {
824
- // Use atom with shallow equals so it doesn't notify on shallow equal values
825
- const config = atom({ page: 1 }, { equals: "shallow" });
826
- const fn = vi.fn(() => `page-${config.get()?.page}`);
827
-
828
- renderHook(() => useAction(fn, { lazy: false, deps: [config] }));
829
-
830
- // In strict mode, effects run twice on mount
831
- const initialCalls = mode === "strict" ? 2 : 1;
832
- expect(fn).toHaveBeenCalledTimes(initialCalls);
833
-
834
- // Set same value (different reference but shallow equal)
835
- // Atom with equals: "shallow" won't notify
836
- act(() => {
837
- config.set({ page: 1 });
838
- });
839
-
840
- // Should NOT re-execute because atom didn't notify (shallow equal)
841
- expect(fn).toHaveBeenCalledTimes(initialCalls);
842
- });
843
-
844
- it("should NOT re-execute when selected atom values are shallowly equal", () => {
845
- // Even if atom notifies, if the selected values are shallow equal,
846
- // the effect should not re-run
847
- const userId = atom(1);
848
- const fn = vi.fn(() => `user-${userId.get()}`);
849
-
850
- renderHook(() => useAction(fn, { lazy: false, deps: [userId] }));
851
-
852
- // In strict mode, effects run twice on mount
853
- const initialCalls = mode === "strict" ? 2 : 1;
854
- expect(fn).toHaveBeenCalledTimes(initialCalls);
855
-
856
- // Set same primitive value - atom won't notify (strict equal)
857
- act(() => {
858
- userId.set(1);
859
- });
860
-
861
- // Should NOT re-execute
862
- expect(fn).toHaveBeenCalledTimes(initialCalls);
863
- });
864
-
865
- it("should re-execute when atom value changes (not shallow equal)", () => {
866
- const config = atom({ page: 1 });
867
- const fn = vi.fn(() => `page-${config.get()?.page}`);
868
-
869
- const { result } = renderHook(() =>
870
- useAction(fn, { lazy: false, deps: [config] })
871
- );
872
-
873
- // In strict mode, effects run twice on mount
874
- const initialCalls = mode === "strict" ? 2 : 1;
875
- expect(fn).toHaveBeenCalledTimes(initialCalls);
876
- expect(result.current.result).toBe("page-1");
877
-
878
- // Set different value
879
- act(() => {
880
- config.set({ page: 2 });
881
- });
882
-
883
- expect(fn).toHaveBeenCalledTimes(initialCalls + 1);
884
- expect(result.current.result).toBe("page-2");
885
- });
886
-
887
- it("should work with mixed deps (atoms and regular values)", () => {
888
- const userId = atom(1);
889
- const fn = vi.fn(() => "result");
890
-
891
- const { rerender } = renderHook(
892
- ({ extraDep }) =>
893
- useAction(fn, { lazy: false, deps: [userId, extraDep] }),
894
- { initialProps: { extraDep: "a" } }
895
- );
896
-
897
- // In strict mode, effects run twice on mount
898
- const initialCalls = mode === "strict" ? 2 : 1;
899
- expect(fn).toHaveBeenCalledTimes(initialCalls);
900
-
901
- // Change atom
902
- act(() => {
903
- userId.set(2);
904
- });
905
-
906
- expect(fn).toHaveBeenCalledTimes(initialCalls + 1);
907
-
908
- // Change regular dep
909
- rerender({ extraDep: "b" });
910
-
911
- expect(fn).toHaveBeenCalledTimes(initialCalls + 2);
912
- });
913
-
914
- it("should NOT track atoms when lazy is true (default)", () => {
915
- const userId = atom(1);
916
- const fn = vi.fn(() => `user-${userId.get()}`);
917
-
918
- renderHook(() => useAction(fn, { lazy: true, deps: [userId] }));
919
-
920
- // Should not execute at all
921
- expect(fn).not.toHaveBeenCalled();
922
-
923
- // Change atom - should still not execute
924
- act(() => {
925
- userId.set(2);
926
- });
927
-
928
- expect(fn).not.toHaveBeenCalled();
929
- });
930
-
931
- it("should abort previous request when atom changes and exclusive is true", () => {
932
- const userId = atom(1);
933
- const signals: AbortSignal[] = [];
934
- const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
935
- signals.push(signal);
936
- return new Promise<string>((resolve) => {
937
- setTimeout(() => resolve(`user-${userId.get()}`), 1000);
938
- });
939
- });
940
-
941
- renderHook(() => useAction(fn, { lazy: false, deps: [userId] }));
942
-
943
- // In strict mode, first signal is aborted by second mount effect
944
- const firstSignalIndex = mode === "strict" ? 1 : 0;
945
- expect(signals[firstSignalIndex].aborted).toBe(false);
946
-
947
- // Change atom - should abort previous
948
- act(() => {
949
- userId.set(2);
950
- });
951
-
952
- expect(signals[firstSignalIndex].aborted).toBe(true);
953
- expect(signals[signals.length - 1].aborted).toBe(false);
954
- });
955
-
956
- it("should work with multiple atoms in deps", () => {
957
- const userId = atom(1);
958
- const orgId = atom(100);
959
- const fn = vi.fn(() => `user-${userId.get()}-org-${orgId.get()}`);
960
-
961
- const { result } = renderHook(() =>
962
- useAction(fn, { lazy: false, deps: [userId, orgId] })
963
- );
964
-
965
- // In strict mode, effects run twice on mount
966
- const initialCalls = mode === "strict" ? 2 : 1;
967
- expect(fn).toHaveBeenCalledTimes(initialCalls);
968
- expect(result.current.result).toBe("user-1-org-100");
969
-
970
- // Change first atom
971
- act(() => {
972
- userId.set(2);
973
- });
974
-
975
- expect(fn).toHaveBeenCalledTimes(initialCalls + 1);
976
- expect(result.current.result).toBe("user-2-org-100");
977
-
978
- // Change second atom
979
- act(() => {
980
- orgId.set(200);
981
- });
982
-
983
- expect(fn).toHaveBeenCalledTimes(initialCalls + 2);
984
- expect(result.current.result).toBe("user-2-org-200");
985
- });
986
- });
987
- });