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,633 +0,0 @@
1
- import { describe, it, expect, vi } from "vitest";
2
- import { atom } from "./atom";
3
- import { SYMBOL_ATOM } from "./types";
4
-
5
- describe("atom", () => {
6
- describe("basic functionality", () => {
7
- it("should create an atom with initial value", () => {
8
- const count$ = atom(0);
9
- expect(count$.get()).toBe(0);
10
- });
11
-
12
- it("should have SYMBOL_ATOM marker", () => {
13
- const count$ = atom(0);
14
- expect(count$[SYMBOL_ATOM]).toBe(true);
15
- });
16
-
17
- it("should set value directly", () => {
18
- const count$ = atom(0);
19
- count$.set(5);
20
- expect(count$.get()).toBe(5);
21
- });
22
-
23
- it("should set value with reducer", () => {
24
- const count$ = atom(0);
25
- count$.set((prev) => prev + 1);
26
- expect(count$.get()).toBe(1);
27
- count$.set((prev) => prev * 2);
28
- expect(count$.get()).toBe(2);
29
- });
30
-
31
- it("should reset to initial value", () => {
32
- const count$ = atom(10);
33
- count$.set(42);
34
- expect(count$.get()).toBe(42);
35
- count$.reset();
36
- expect(count$.get()).toBe(10);
37
- });
38
-
39
- it("should store objects", () => {
40
- const user$ = atom({ name: "John", age: 30 });
41
- expect(user$.get()).toEqual({ name: "John", age: 30 });
42
- user$.set({ name: "Jane", age: 25 });
43
- expect(user$.get()).toEqual({ name: "Jane", age: 25 });
44
- });
45
-
46
- it("should store null and undefined", () => {
47
- const nullable$ = atom<string | null>(null);
48
- expect(nullable$.get()).toBe(null);
49
- nullable$.set("hello");
50
- expect(nullable$.get()).toBe("hello");
51
- nullable$.set(null);
52
- expect(nullable$.get()).toBe(null);
53
-
54
- const undef$ = atom<string | undefined>(undefined);
55
- expect(undef$.get()).toBe(undefined);
56
- undef$.set("world");
57
- expect(undef$.get()).toBe("world");
58
- });
59
-
60
- it("should store arrays", () => {
61
- const items$ = atom<number[]>([1, 2, 3]);
62
- expect(items$.get()).toEqual([1, 2, 3]);
63
- items$.set((prev) => [...prev, 4]);
64
- expect(items$.get()).toEqual([1, 2, 3, 4]);
65
- });
66
- });
67
-
68
- describe("subscriptions", () => {
69
- it("should notify subscribers on value change", () => {
70
- const count$ = atom(0);
71
- const listener = vi.fn();
72
- count$.on(listener);
73
- count$.set(1);
74
- expect(listener).toHaveBeenCalledTimes(1);
75
- });
76
-
77
- it("should not notify if value is the same (strict equality)", () => {
78
- const count$ = atom(5);
79
- const listener = vi.fn();
80
- count$.on(listener);
81
- count$.set(5);
82
- expect(listener).not.toHaveBeenCalled();
83
- });
84
-
85
- it("should allow unsubscribing", () => {
86
- const count$ = atom(0);
87
- const listener = vi.fn();
88
- const unsub = count$.on(listener);
89
- count$.set(1);
90
- expect(listener).toHaveBeenCalledTimes(1);
91
- unsub();
92
- count$.set(2);
93
- expect(listener).toHaveBeenCalledTimes(1);
94
- });
95
-
96
- it("should support multiple subscribers", () => {
97
- const count$ = atom(0);
98
- const listener1 = vi.fn();
99
- const listener2 = vi.fn();
100
- count$.on(listener1);
101
- count$.on(listener2);
102
- count$.set(1);
103
- expect(listener1).toHaveBeenCalledTimes(1);
104
- expect(listener2).toHaveBeenCalledTimes(1);
105
- });
106
-
107
- it("should notify on reset if value changed", () => {
108
- const count$ = atom(0);
109
- const listener = vi.fn();
110
- count$.set(5);
111
- count$.on(listener);
112
- count$.reset();
113
- expect(listener).toHaveBeenCalledTimes(1);
114
- });
115
-
116
- it("should not notify on reset if already at initial value", () => {
117
- const count$ = atom(0);
118
- const listener = vi.fn();
119
- count$.on(listener);
120
- count$.reset();
121
- expect(listener).not.toHaveBeenCalled();
122
- });
123
- });
124
-
125
- describe("dirty", () => {
126
- it("should return false when just initialized", () => {
127
- const count$ = atom(42);
128
- expect(count$.dirty()).toBe(false);
129
- });
130
-
131
- it("should return true after value is set", () => {
132
- const count$ = atom(0);
133
- expect(count$.dirty()).toBe(false);
134
-
135
- count$.set(10);
136
- expect(count$.dirty()).toBe(true);
137
- });
138
-
139
- it("should return false after reset", () => {
140
- const count$ = atom(0);
141
- count$.set(10);
142
- expect(count$.dirty()).toBe(true);
143
-
144
- count$.reset();
145
- expect(count$.dirty()).toBe(false);
146
- });
147
-
148
- it("should stay dirty after multiple sets", () => {
149
- const count$ = atom(0);
150
- count$.set(1);
151
- count$.set(2);
152
- count$.set(3);
153
- expect(count$.dirty()).toBe(true);
154
- });
155
-
156
- it("should not become dirty if set to same value (equality check)", () => {
157
- const count$ = atom(42);
158
- count$.set(42); // Same value, equality check prevents change
159
- expect(count$.dirty()).toBe(false);
160
- });
161
-
162
- it("should work with objects and shallow equality", () => {
163
- const obj$ = atom({ a: 1 }, { equals: "shallow" });
164
- expect(obj$.dirty()).toBe(false);
165
-
166
- obj$.set({ a: 1 }); // Shallow equal, no change
167
- expect(obj$.dirty()).toBe(false);
168
-
169
- obj$.set({ a: 2 }); // Different value
170
- expect(obj$.dirty()).toBe(true);
171
-
172
- obj$.reset();
173
- expect(obj$.dirty()).toBe(false);
174
- });
175
-
176
- it("should be useful for tracking unsaved changes", () => {
177
- const form$ = atom({ name: "", email: "" });
178
-
179
- expect(form$.dirty()).toBe(false); // No changes yet
180
-
181
- form$.set({ name: "John", email: "" });
182
- expect(form$.dirty()).toBe(true); // Has unsaved changes
183
-
184
- form$.reset();
185
- expect(form$.dirty()).toBe(false); // Changes discarded
186
- });
187
- });
188
-
189
- describe("equality options", () => {
190
- it("should use strict equality by default", () => {
191
- const obj$ = atom({ a: 1 });
192
- const listener = vi.fn();
193
- obj$.on(listener);
194
- obj$.set({ a: 1 }); // Different object, same content
195
- expect(listener).toHaveBeenCalledTimes(1);
196
- });
197
-
198
- it("should support shallow equality", () => {
199
- const obj$ = atom({ a: 1 }, { equals: "shallow" });
200
- const listener = vi.fn();
201
- obj$.on(listener);
202
- obj$.set({ a: 1 }); // Same content
203
- expect(listener).not.toHaveBeenCalled();
204
- obj$.set({ a: 2 }); // Different content
205
- expect(listener).toHaveBeenCalledTimes(1);
206
- });
207
-
208
- it("should support custom equality function", () => {
209
- const user$ = atom(
210
- { id: 1, name: "John" },
211
- { equals: (a, b) => a.id === b.id }
212
- );
213
- const listener = vi.fn();
214
- user$.on(listener);
215
- user$.set({ id: 1, name: "Jane" }); // Same id
216
- expect(listener).not.toHaveBeenCalled();
217
- user$.set({ id: 2, name: "Jane" }); // Different id
218
- expect(listener).toHaveBeenCalledTimes(1);
219
- });
220
- });
221
-
222
- describe("Promise storage (raw)", () => {
223
- it("should store Promise as-is", () => {
224
- const promise = Promise.resolve(42);
225
- const data$ = atom(promise);
226
- expect(data$.get()).toBe(promise);
227
- });
228
-
229
- it("should store a new Promise on set", () => {
230
- const promise1 = Promise.resolve(1);
231
- const promise2 = Promise.resolve(2);
232
- const data$ = atom(promise1);
233
- expect(data$.get()).toBe(promise1);
234
- data$.set(promise2);
235
- expect(data$.get()).toBe(promise2);
236
- });
237
-
238
- it("should reset to original Promise object", () => {
239
- const originalPromise = Promise.resolve(42);
240
- const data$ = atom(originalPromise);
241
- data$.set(Promise.resolve(100));
242
- expect(data$.get()).not.toBe(originalPromise);
243
- data$.reset();
244
- expect(data$.get()).toBe(originalPromise);
245
- });
246
- });
247
-
248
- describe("reducer errors", () => {
249
- it("should throw synchronously if reducer throws", () => {
250
- const count$ = atom(0);
251
- const error = new Error("Reducer failed");
252
- expect(() => {
253
- count$.set(() => {
254
- throw error;
255
- });
256
- }).toThrow(error);
257
- // Value should remain unchanged
258
- expect(count$.get()).toBe(0);
259
- });
260
- });
261
-
262
- describe("lazy initialization", () => {
263
- it("should support lazy initializer function", () => {
264
- const initializer = vi.fn(() => 42);
265
- const count$ = atom(initializer);
266
- expect(initializer).toHaveBeenCalledTimes(1);
267
- expect(count$.get()).toBe(42);
268
- });
269
-
270
- it("should only call initializer once", () => {
271
- const initializer = vi.fn(() => ({ data: "expensive" }));
272
- const obj$ = atom(initializer);
273
- expect(initializer).toHaveBeenCalledTimes(1);
274
- // Access value multiple times
275
- obj$.get();
276
- obj$.get();
277
- expect(initializer).toHaveBeenCalledTimes(1);
278
- });
279
-
280
- it("should re-run initializer on reset", () => {
281
- let callCount = 0;
282
- const initializer = () => {
283
- callCount++;
284
- return callCount; // Returns different value each call
285
- };
286
- const count$ = atom(initializer);
287
- expect(count$.get()).toBe(1); // First call returns 1
288
- expect(callCount).toBe(1);
289
-
290
- count$.set(100);
291
- expect(count$.get()).toBe(100);
292
-
293
- count$.reset();
294
- expect(count$.get()).toBe(2); // Re-runs initializer, gets fresh value
295
- expect(callCount).toBe(2); // Initializer called again
296
- });
297
-
298
- it("should work with lazy initializer returning object", () => {
299
- const obj$ = atom(() => ({ count: 0, items: [] as number[] }));
300
- expect(obj$.get()).toEqual({ count: 0, items: [] });
301
- obj$.set((prev) => ({ ...prev, count: 1 }));
302
- expect(obj$.get()).toEqual({ count: 1, items: [] });
303
- });
304
-
305
- it("should work with lazy initializer returning Promise", () => {
306
- const promise = Promise.resolve(42);
307
- const data$ = atom(() => promise);
308
- expect(data$.get()).toBe(promise);
309
- });
310
-
311
- it("should still work with direct value (non-function)", () => {
312
- const count$ = atom(10);
313
- expect(count$.get()).toBe(10);
314
- });
315
- });
316
-
317
- describe("metadata", () => {
318
- it("should store meta", () => {
319
- const count$ = atom(0, { meta: { key: "count" } });
320
- expect(count$.meta).toEqual({ key: "count" });
321
- });
322
-
323
- it("should have undefined meta if not provided", () => {
324
- const count$ = atom(0);
325
- expect(count$.meta).toBe(undefined);
326
- });
327
- });
328
-
329
- describe("AtomContext (init function with context)", () => {
330
- describe("signal", () => {
331
- it("should pass context with signal to init function", () => {
332
- let receivedSignal: AbortSignal | undefined;
333
- atom((ctx) => {
334
- receivedSignal = ctx.signal;
335
- return 42;
336
- });
337
- expect(receivedSignal).toBeInstanceOf(AbortSignal);
338
- expect(receivedSignal!.aborted).toBe(false);
339
- });
340
-
341
- it("should abort signal when set() is called", () => {
342
- let receivedSignal: AbortSignal | undefined;
343
- const count$ = atom((ctx) => {
344
- receivedSignal = ctx.signal;
345
- return 0;
346
- });
347
- expect(receivedSignal!.aborted).toBe(false);
348
-
349
- count$.set(1);
350
- expect(receivedSignal!.aborted).toBe(true);
351
- });
352
-
353
- it("should abort signal when reset() is called", () => {
354
- const signals: AbortSignal[] = [];
355
- const count$ = atom((ctx) => {
356
- signals.push(ctx.signal);
357
- return 0;
358
- });
359
- expect(signals[0].aborted).toBe(false);
360
-
361
- count$.reset();
362
- // Original signal should be aborted
363
- expect(signals[0].aborted).toBe(true);
364
- });
365
-
366
- it("should provide fresh signal on reset", () => {
367
- const signals: AbortSignal[] = [];
368
- const count$ = atom((ctx) => {
369
- signals.push(ctx.signal);
370
- return signals.length;
371
- });
372
-
373
- expect(signals).toHaveLength(1);
374
- expect(signals[0].aborted).toBe(false);
375
-
376
- count$.reset();
377
- expect(signals).toHaveLength(2);
378
- expect(signals[0].aborted).toBe(true);
379
- expect(signals[1].aborted).toBe(false);
380
- });
381
-
382
- it("should not create new signal on subsequent set() calls", () => {
383
- let signalRef: AbortSignal | undefined;
384
- const count$ = atom((ctx) => {
385
- signalRef = ctx.signal;
386
- return 0;
387
- });
388
-
389
- const firstSignal = signalRef;
390
- count$.set(1);
391
- expect(firstSignal!.aborted).toBe(true);
392
-
393
- // After set, the signal is already aborted, no new context is created
394
- // because set() doesn't re-run the initializer
395
- count$.set(2);
396
- expect(firstSignal!.aborted).toBe(true);
397
- });
398
-
399
- it("should abort with reason", () => {
400
- let receivedSignal: AbortSignal | undefined;
401
- const count$ = atom((ctx) => {
402
- receivedSignal = ctx.signal;
403
- return 0;
404
- });
405
-
406
- count$.set(1);
407
- expect(receivedSignal!.aborted).toBe(true);
408
- expect(receivedSignal!.reason).toBe("value changed");
409
- });
410
-
411
- it("should abort with 'reset' reason on reset", () => {
412
- const signals: AbortSignal[] = [];
413
- const count$ = atom((ctx) => {
414
- signals.push(ctx.signal);
415
- return 0;
416
- });
417
-
418
- count$.reset();
419
- // Original signal should be aborted with 'reset' reason
420
- expect(signals[0].aborted).toBe(true);
421
- expect(signals[0].reason).toBe("reset");
422
- });
423
- });
424
-
425
- describe("onCleanup", () => {
426
- it("should call cleanup when set() is called", () => {
427
- const cleanup = vi.fn();
428
- const count$ = atom((ctx) => {
429
- ctx.onCleanup(cleanup);
430
- return 0;
431
- });
432
-
433
- expect(cleanup).not.toHaveBeenCalled();
434
- count$.set(1);
435
- expect(cleanup).toHaveBeenCalledTimes(1);
436
- });
437
-
438
- it("should call cleanup when reset() is called", () => {
439
- const cleanup = vi.fn();
440
- const count$ = atom((ctx) => {
441
- ctx.onCleanup(cleanup);
442
- return 0;
443
- });
444
-
445
- expect(cleanup).not.toHaveBeenCalled();
446
- count$.reset();
447
- expect(cleanup).toHaveBeenCalledTimes(1);
448
- });
449
-
450
- it("should call multiple cleanups in FIFO order", () => {
451
- const order: number[] = [];
452
- const count$ = atom((ctx) => {
453
- ctx.onCleanup(() => order.push(1));
454
- ctx.onCleanup(() => order.push(2));
455
- ctx.onCleanup(() => order.push(3));
456
- return 0;
457
- });
458
-
459
- count$.set(1);
460
- expect(order).toEqual([1, 2, 3]);
461
- });
462
-
463
- it("should clear cleanups after calling them", () => {
464
- const cleanup = vi.fn();
465
- const count$ = atom((ctx) => {
466
- ctx.onCleanup(cleanup);
467
- return 0;
468
- });
469
-
470
- count$.set(1);
471
- expect(cleanup).toHaveBeenCalledTimes(1);
472
-
473
- // Second set should not call cleanup again (cleanup was from init, not from set)
474
- count$.set(2);
475
- expect(cleanup).toHaveBeenCalledTimes(1);
476
- });
477
-
478
- it("should call cleanup from reset, then register new cleanups on next reset", () => {
479
- let cleanupCallCount = 0;
480
- const count$ = atom((ctx) => {
481
- ctx.onCleanup(() => cleanupCallCount++);
482
- return cleanupCallCount;
483
- });
484
-
485
- expect(cleanupCallCount).toBe(0);
486
-
487
- count$.reset();
488
- expect(cleanupCallCount).toBe(1); // First cleanup called
489
-
490
- count$.reset();
491
- expect(cleanupCallCount).toBe(2); // Second cleanup called (registered in previous reset)
492
- });
493
-
494
- it("should work with async operations that check signal", async () => {
495
- let cleanupCalled = false;
496
- let operationAborted = false;
497
-
498
- const data$ = atom((ctx) => {
499
- ctx.onCleanup(() => {
500
- cleanupCalled = true;
501
- });
502
-
503
- // Simulate an async operation that respects the signal
504
- const timeoutId = setTimeout(() => {
505
- if (!ctx.signal.aborted) {
506
- // Would do something here
507
- } else {
508
- operationAborted = true;
509
- }
510
- }, 100);
511
-
512
- ctx.onCleanup(() => clearTimeout(timeoutId));
513
-
514
- return "initial";
515
- });
516
-
517
- // Trigger abort
518
- data$.set("changed");
519
-
520
- expect(cleanupCalled).toBe(true);
521
-
522
- // Wait for the timeout to potentially fire
523
- await new Promise((r) => setTimeout(r, 150));
524
-
525
- // The operation should have been cleaned up/aborted
526
- expect(operationAborted).toBe(false); // timeout was cleared by cleanup
527
- });
528
- });
529
-
530
- describe("combined signal and cleanup", () => {
531
- it("should abort signal and call cleanup on set", () => {
532
- let signal: AbortSignal | undefined;
533
- const cleanup = vi.fn();
534
-
535
- const count$ = atom((ctx) => {
536
- signal = ctx.signal;
537
- ctx.onCleanup(cleanup);
538
- return 0;
539
- });
540
-
541
- expect(signal!.aborted).toBe(false);
542
- expect(cleanup).not.toHaveBeenCalled();
543
-
544
- count$.set(1);
545
-
546
- expect(signal!.aborted).toBe(true);
547
- expect(cleanup).toHaveBeenCalledTimes(1);
548
- });
549
-
550
- it("should abort signal and call cleanup on reset, then provide fresh context", () => {
551
- const signals: AbortSignal[] = [];
552
- const cleanups: number[] = [];
553
- let callCount = 0;
554
-
555
- const count$ = atom((ctx) => {
556
- callCount++;
557
- signals.push(ctx.signal);
558
- ctx.onCleanup(() => cleanups.push(callCount));
559
- return callCount;
560
- });
561
-
562
- expect(signals).toHaveLength(1);
563
- expect(cleanups).toEqual([]);
564
-
565
- count$.reset();
566
-
567
- expect(signals).toHaveLength(2);
568
- expect(signals[0].aborted).toBe(true);
569
- expect(signals[1].aborted).toBe(false);
570
- expect(cleanups).toEqual([1]); // cleanup from first init was called
571
- });
572
- });
573
-
574
- describe("non-function initializer (no context)", () => {
575
- it("should work normally with direct value", () => {
576
- const count$ = atom(42);
577
- expect(count$.get()).toBe(42);
578
-
579
- count$.set(100);
580
- expect(count$.get()).toBe(100);
581
-
582
- count$.reset();
583
- expect(count$.get()).toBe(42);
584
- });
585
-
586
- it("should not have cleanup issues with direct value", () => {
587
- const count$ = atom(0);
588
- // These should not throw
589
- count$.set(1);
590
- count$.set(2);
591
- count$.reset();
592
- count$.reset();
593
- });
594
- });
595
- });
596
-
597
- describe("plugin system (use)", () => {
598
- it("should support .use() for extensions", () => {
599
- // Note: Don't use ...source spread as it copies values, not getters
600
- // Instead, access source properties through the reference
601
- const base$ = atom(0);
602
- const count$ = base$.use((source) => ({
603
- get: source.get,
604
- set: source.set,
605
- reset: source.reset,
606
- on: source.on,
607
- increment: () => source.set((v) => v + 1),
608
- }));
609
-
610
- expect(count$.get()).toBe(0);
611
- count$.increment();
612
- expect(count$.get()).toBe(1);
613
- });
614
-
615
- it("should support .use() with source reference pattern", () => {
616
- // Simpler pattern: just reference the original atom
617
- const base$ = atom(0);
618
- const enhanced$ = base$.use(() => ({
619
- get: () => base$.get(),
620
- increment: () => base$.set((v) => v + 1),
621
- decrement: () => base$.set((v) => v - 1),
622
- }));
623
-
624
- expect(enhanced$.get()).toBe(0);
625
- enhanced$.increment();
626
- expect(enhanced$.get()).toBe(1);
627
- enhanced$.increment();
628
- expect(enhanced$.get()).toBe(2);
629
- enhanced$.decrement();
630
- expect(enhanced$.get()).toBe(1);
631
- });
632
- });
633
- });