atomirx 0.0.7 → 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 -30
  33. package/dist/react/index.js +206 -791
  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 -39
@@ -1,256 +0,0 @@
1
- import { emitter, Emitter } from "./emitter";
2
- import { resolveEquality } from "./equality";
3
- import { scheduleNotifyHook } from "./scheduleNotifyHook";
4
- import { Equality } from "./types";
5
-
6
- /**
7
- * Options for creating an atomState.
8
- */
9
- export interface AtomStateOptions<T, TFallback = undefined> {
10
- /** Equality strategy for change detection (default: "strict") */
11
- equals?: Equality<T>;
12
- /**
13
- * Fallback value to use during loading or error states.
14
- * When set, enables "stale" mode where value is never undefined.
15
- */
16
- fallback?: TFallback;
17
- /**
18
- * Whether fallback mode is enabled.
19
- * When true, getValue() returns fallback/lastResolved during loading/error.
20
- */
21
- hasFallback?: boolean;
22
- }
23
-
24
- /**
25
- * API for managing atom state with async support.
26
- */
27
- export interface AtomStateAPI<T, TFallback = undefined> {
28
- /** Get the current value (undefined if loading or error, unless fallback mode) */
29
- getValue(): TFallback extends undefined ? T | undefined : T | TFallback;
30
- /** Get the loading state */
31
- getLoading(): boolean;
32
- /** Get the error (undefined if no error) */
33
- getError(): any;
34
- /** Get the current promise */
35
- getPromise(): PromiseLike<T>;
36
-
37
- /** Set the value (clears loading and error, notifies if changed) */
38
- setValue(value: T, silent?: boolean): void;
39
- /** Set loading state with a promise (clears value and error, notifies) */
40
- setLoading(promise: PromiseLike<T>, silent?: boolean): void;
41
- /** Set error state (clears value and loading, notifies if changed) */
42
- setError(error: any, silent?: boolean): void;
43
- /** Reset to initial state (notifies if was not already initial) */
44
- reset(): void;
45
-
46
- /** Get current version (for race condition handling) */
47
- getVersion(): number;
48
- /** Check if a version is stale (older than current) */
49
- isVersionStale(version: number): boolean;
50
-
51
- /**
52
- * Returns true if fallback mode is enabled AND (loading OR error).
53
- * When true, getValue() returns fallback or last resolved value.
54
- */
55
- stale(): boolean;
56
-
57
- /**
58
- * Returns true if value has been changed by setValue() (not during init).
59
- */
60
- isDirty(): boolean;
61
-
62
- /**
63
- * Marks the state as dirty (called when set() is used).
64
- */
65
- markDirty(): void;
66
-
67
- /**
68
- * Clears the dirty flag (called on reset).
69
- */
70
- clearDirty(): void;
71
-
72
- /** Subscribe to state changes */
73
- on: Emitter["on"];
74
- }
75
-
76
- /**
77
- * Creates a state container for atoms with async support.
78
- *
79
- * Handles:
80
- * - Value, loading, and error states
81
- * - Version tracking for race condition handling
82
- * - Equality checking for change detection
83
- * - Notification scheduling
84
- * - Fallback mode for stale-while-revalidate pattern
85
- *
86
- * @template T - The type of value stored
87
- * @template TFallback - The type of fallback value (default: undefined)
88
- * @param options - Configuration options
89
- * @returns AtomStateAPI for managing the state
90
- *
91
- * @example
92
- * ```ts
93
- * const state = atomState<number>();
94
- *
95
- * state.setValue(42);
96
- * console.log(state.getValue()); // 42
97
- *
98
- * state.setLoading(fetchData());
99
- * console.log(state.getLoading()); // true
100
- *
101
- * state.on(() => console.log('State changed!'));
102
- * ```
103
- *
104
- * @example With fallback
105
- * ```ts
106
- * const state = atomState<User, User>({
107
- * fallback: { name: 'Guest' },
108
- * hasFallback: true
109
- * });
110
- *
111
- * state.setLoading(fetchUser());
112
- * console.log(state.getValue()); // { name: 'Guest' }
113
- * console.log(state.isStale()); // true
114
- * ```
115
- */
116
- export function atomState<T, TFallback = undefined>(
117
- options: AtomStateOptions<T, TFallback> = {}
118
- ): AtomStateAPI<T, TFallback> {
119
- const changeEmitter = emitter();
120
- const eq = resolveEquality(options.equals as Equality<unknown>);
121
-
122
- // Fallback configuration
123
- const hasFallback = options.hasFallback ?? false;
124
- const fallbackValue = options.fallback as TFallback;
125
-
126
- // Internal state
127
- let value: T | undefined = undefined;
128
- let loading = false;
129
- let error: any = undefined;
130
- let promise: PromiseLike<T> = Promise.resolve(undefined as T);
131
- let version = 0;
132
-
133
- // Track last resolved value for stale-while-revalidate
134
- let lastResolvedValue: T | undefined = undefined;
135
-
136
- // Track if value has been modified by set()
137
- let dirty = false;
138
-
139
- /**
140
- * Schedules notification to all subscribers.
141
- * Each listener is scheduled individually to enable deduping in batch().
142
- */
143
- const notify = () => {
144
- changeEmitter.forEach((listener) => {
145
- scheduleNotifyHook.current(listener);
146
- });
147
- };
148
-
149
- /**
150
- * Checks if state is in initial state (no value, not loading, no error).
151
- */
152
- const isInitialState = () => {
153
- return value === undefined && !loading && error === undefined;
154
- };
155
-
156
- const api: AtomStateAPI<T, TFallback> = {
157
- getValue: () => {
158
- // If fallback mode enabled and in loading/error state
159
- if (hasFallback && (loading || error !== undefined)) {
160
- // Return last resolved value if available, otherwise fallback
161
- return (lastResolvedValue ?? fallbackValue) as any;
162
- }
163
- return value as any;
164
- },
165
- getLoading: () => loading,
166
- getError: () => error,
167
- getPromise: () => promise,
168
- getVersion: () => version,
169
-
170
- isVersionStale: (v: number) => v !== version,
171
-
172
- stale: () => {
173
- // Returns true if fallback mode enabled AND (loading OR error)
174
- return hasFallback && (loading || error !== undefined);
175
- },
176
-
177
- isDirty: () => dirty,
178
-
179
- markDirty: () => {
180
- dirty = true;
181
- },
182
-
183
- clearDirty: () => {
184
- dirty = false;
185
- },
186
-
187
- setValue: (newValue: T, silent = false) => {
188
- // Check equality before updating (only if we have a previous value)
189
- if (
190
- value !== undefined &&
191
- eq(newValue, value) &&
192
- !loading &&
193
- error === undefined
194
- ) {
195
- return;
196
- }
197
-
198
- value = newValue;
199
- loading = false;
200
- error = undefined;
201
- promise = Promise.resolve(newValue);
202
- version++;
203
-
204
- // Track last resolved value for fallback mode
205
- lastResolvedValue = newValue;
206
-
207
- if (!silent) notify();
208
- },
209
-
210
- setLoading: (newPromise: PromiseLike<T>, silent = false) => {
211
- // In fallback mode, don't clear value - it will be returned via getValue()
212
- // But internally we track that we're loading
213
- value = undefined;
214
- loading = true;
215
- error = undefined;
216
- promise = newPromise;
217
- version++;
218
- if (!silent) notify();
219
- },
220
-
221
- setError: (newError: any, silent = false) => {
222
- // Check if error is the same
223
- if (Object.is(error, newError) && !loading && value === undefined) {
224
- return;
225
- }
226
-
227
- value = undefined;
228
- loading = false;
229
- error = newError;
230
- version++;
231
- if (!silent) notify();
232
- },
233
-
234
- reset: () => {
235
- // Don't notify if already in initial state
236
- if (isInitialState() && lastResolvedValue === undefined && !dirty) {
237
- return;
238
- }
239
-
240
- value = undefined;
241
- loading = false;
242
- error = undefined;
243
- promise = Promise.resolve(undefined as T);
244
- version++;
245
- // Clear last resolved value on reset
246
- lastResolvedValue = undefined;
247
- // Clear dirty flag on reset
248
- dirty = false;
249
- notify();
250
- },
251
-
252
- on: changeEmitter.on,
253
- };
254
-
255
- return api;
256
- }
@@ -1,257 +0,0 @@
1
- import { describe, it, expect, vi } from "vitest";
2
- import { batch } from "./batch";
3
- import { atom } from "./atom";
4
-
5
- describe("batch", () => {
6
- describe("basic batching", () => {
7
- it("should batch multiple updates into single notification", () => {
8
- const count = atom(0);
9
- const listener = vi.fn();
10
- count.on(listener);
11
-
12
- batch(() => {
13
- count.set(1);
14
- count.set(2);
15
- count.set(3);
16
- });
17
-
18
- // All updates batched - listener called once at the end
19
- expect(count.get()).toBe(3);
20
- expect(listener).toHaveBeenCalledTimes(1);
21
- });
22
-
23
- it("should return the function result", () => {
24
- const result = batch(() => {
25
- return "hello";
26
- });
27
-
28
- expect(result).toBe("hello");
29
- });
30
-
31
- it("should return complex values", () => {
32
- const result = batch(() => {
33
- return { value: 42, items: [1, 2, 3] };
34
- });
35
-
36
- expect(result).toEqual({ value: 42, items: [1, 2, 3] });
37
- });
38
- });
39
-
40
- describe("nested batching", () => {
41
- it("should support nested batch calls", () => {
42
- const count = atom(0);
43
- const listener = vi.fn();
44
- count.on(listener);
45
-
46
- batch(() => {
47
- count.set(1);
48
-
49
- batch(() => {
50
- count.set(2);
51
- count.set(3);
52
- });
53
-
54
- count.set(4);
55
- });
56
-
57
- expect(count.get()).toBe(4);
58
- // All updates batched together
59
- expect(listener).toHaveBeenCalledTimes(1);
60
- });
61
-
62
- it("should return value from nested batch", () => {
63
- const result = batch(() => {
64
- const inner = batch(() => {
65
- return "inner";
66
- });
67
- return `outer-${inner}`;
68
- });
69
-
70
- expect(result).toBe("outer-inner");
71
- });
72
- });
73
-
74
- describe("multiple atoms", () => {
75
- it("should batch updates across multiple atoms", () => {
76
- const a = atom(0);
77
- const b = atom(0);
78
- const listenerA = vi.fn();
79
- const listenerB = vi.fn();
80
- a.on(listenerA);
81
- b.on(listenerB);
82
-
83
- batch(() => {
84
- a.set(1);
85
- b.set(1);
86
- a.set(2);
87
- b.set(2);
88
- });
89
-
90
- expect(a.get()).toBe(2);
91
- expect(b.get()).toBe(2);
92
- expect(listenerA).toHaveBeenCalledTimes(1);
93
- expect(listenerB).toHaveBeenCalledTimes(1);
94
- });
95
- });
96
-
97
- describe("error handling", () => {
98
- it("should propagate errors", () => {
99
- expect(() => {
100
- batch(() => {
101
- throw new Error("test error");
102
- });
103
- }).toThrow("test error");
104
- });
105
-
106
- it("should still process notifications after error in nested batch", () => {
107
- const count = atom(0);
108
- const listener = vi.fn();
109
- count.on(listener);
110
-
111
- expect(() => {
112
- batch(() => {
113
- count.set(1);
114
-
115
- try {
116
- batch(() => {
117
- count.set(2);
118
- throw new Error("inner error");
119
- });
120
- } catch {
121
- // Catch inner error
122
- }
123
-
124
- count.set(3);
125
- });
126
- }).not.toThrow();
127
-
128
- expect(count.get()).toBe(3);
129
- });
130
- });
131
-
132
- describe("cascading updates", () => {
133
- it("should handle cascading updates within batch", () => {
134
- const a = atom(0);
135
- const b = atom(0);
136
- const listenerA = vi.fn();
137
- const listenerB = vi.fn();
138
-
139
- // When a changes, update b
140
- a.on(() => {
141
- if (a.get() !== undefined && a.get() > 0) {
142
- b.set(a.get() * 2);
143
- }
144
- });
145
-
146
- a.on(listenerA);
147
- b.on(listenerB);
148
-
149
- batch(() => {
150
- a.set(5);
151
- });
152
-
153
- expect(a.get()).toBe(5);
154
- expect(b.get()).toBe(10);
155
- });
156
- });
157
-
158
- describe("without batch", () => {
159
- it("should notify immediately without batch", () => {
160
- const count = atom(0);
161
- const listener = vi.fn();
162
- count.on(listener);
163
-
164
- count.set(1);
165
- expect(listener).toHaveBeenCalledTimes(1);
166
-
167
- count.set(2);
168
- expect(listener).toHaveBeenCalledTimes(2);
169
-
170
- count.set(3);
171
- expect(listener).toHaveBeenCalledTimes(3);
172
- });
173
- });
174
-
175
- describe("listener deduping", () => {
176
- it("should dedupe same listener subscribed to multiple atoms", () => {
177
- const a = atom(0);
178
- const b = atom(0);
179
- const c = atom(0);
180
-
181
- // Same listener subscribed to all three atoms
182
- const sharedListener = vi.fn();
183
- a.on(sharedListener);
184
- b.on(sharedListener);
185
- c.on(sharedListener);
186
-
187
- batch(() => {
188
- a.set(1);
189
- b.set(1);
190
- c.set(1);
191
- });
192
-
193
- // Listener should only be called once (deduped), not 3 times
194
- expect(sharedListener).toHaveBeenCalledTimes(1);
195
- });
196
-
197
- it("should call different listeners separately", () => {
198
- const a = atom(0);
199
- const b = atom(0);
200
-
201
- const listenerA = vi.fn();
202
- const listenerB = vi.fn();
203
- a.on(listenerA);
204
- b.on(listenerB);
205
-
206
- batch(() => {
207
- a.set(1);
208
- b.set(1);
209
- });
210
-
211
- // Different listeners should each be called once
212
- expect(listenerA).toHaveBeenCalledTimes(1);
213
- expect(listenerB).toHaveBeenCalledTimes(1);
214
- });
215
-
216
- it("should dedupe listener when same atom updated multiple times", () => {
217
- const count = atom(0);
218
- const listener = vi.fn();
219
- count.on(listener);
220
-
221
- batch(() => {
222
- count.set(1);
223
- count.set(2);
224
- count.set(3);
225
- });
226
-
227
- // Listener called once at the end with final value
228
- expect(listener).toHaveBeenCalledTimes(1);
229
- expect(count.get()).toBe(3);
230
- });
231
-
232
- it("should handle mixed scenario with shared and unique listeners", () => {
233
- const a = atom(0);
234
- const b = atom(0);
235
-
236
- const sharedListener = vi.fn();
237
- const uniqueListenerA = vi.fn();
238
- const uniqueListenerB = vi.fn();
239
-
240
- a.on(sharedListener);
241
- a.on(uniqueListenerA);
242
- b.on(sharedListener);
243
- b.on(uniqueListenerB);
244
-
245
- batch(() => {
246
- a.set(1);
247
- b.set(1);
248
- });
249
-
250
- // Shared listener deduped to 1 call
251
- expect(sharedListener).toHaveBeenCalledTimes(1);
252
- // Unique listeners called once each
253
- expect(uniqueListenerA).toHaveBeenCalledTimes(1);
254
- expect(uniqueListenerB).toHaveBeenCalledTimes(1);
255
- });
256
- });
257
- });
package/src/core/batch.ts DELETED
@@ -1,172 +0,0 @@
1
- import { hook } from "./hook";
2
- import { scheduleNotifyHook } from "./scheduleNotifyHook";
3
-
4
- let batchDepth = 0;
5
-
6
- /**
7
- * Batches multiple state updates into a single reactive update cycle.
8
- *
9
- * Without batching, each `atom.set()` call triggers immediate notifications to all
10
- * subscribers. With `batch()`, all updates are collected and subscribers are notified
11
- * once at the end with the final values.
12
- *
13
- * ## Key Behavior
14
- *
15
- * 1. **Multiple updates to same atom**: Only 1 notification with final value
16
- * 2. **Listener deduplication**: Same listener subscribed to multiple atoms = 1 call
17
- * 3. **Nested batches**: Inner batches are merged into outer batch
18
- * 4. **Cascading updates**: Updates triggered by listeners are also batched
19
- *
20
- * ## When to Use
21
- *
22
- * - Updating multiple related atoms together
23
- * - Preventing intermediate render states
24
- * - Performance optimization for bulk updates
25
- * - Ensuring consistent state during complex operations
26
- *
27
- * ## How It Works
28
- *
29
- * ```
30
- * batch(() => {
31
- * a.set(1); // Queued, no notification yet
32
- * b.set(2); // Queued, no notification yet
33
- * c.set(3); // Queued, no notification yet
34
- * });
35
- * // All listeners notified once here (deduped)
36
- * ```
37
- *
38
- * @template T - Return type of the batched function
39
- * @param fn - Function containing multiple state updates
40
- * @returns The return value of fn
41
- *
42
- * @example Basic batching - prevent intermediate states
43
- * ```ts
44
- * const firstName = atom("John");
45
- * const lastName = atom("Doe");
46
- *
47
- * // Without batch: component renders twice (once per set)
48
- * firstName.set("Jane");
49
- * lastName.set("Smith");
50
- *
51
- * // With batch: component renders once with final state
52
- * batch(() => {
53
- * firstName.set("Jane");
54
- * lastName.set("Smith");
55
- * });
56
- * ```
57
- *
58
- * @example Multiple updates to same atom
59
- * ```ts
60
- * const counter = atom(0);
61
- *
62
- * counter.on(() => console.log("Counter:", counter.get()));
63
- *
64
- * batch(() => {
65
- * counter.set(1);
66
- * counter.set(2);
67
- * counter.set(3);
68
- * });
69
- * // Logs once: "Counter: 3"
70
- * ```
71
- *
72
- * @example Listener deduplication
73
- * ```ts
74
- * const a = atom(0);
75
- * const b = atom(0);
76
- *
77
- * // Same listener subscribed to both atoms
78
- * const listener = () => console.log("Changed!", a.get(), b.get());
79
- * a.on(listener);
80
- * b.on(listener);
81
- *
82
- * batch(() => {
83
- * a.set(1);
84
- * b.set(2);
85
- * });
86
- * // Logs once: "Changed! 1 2" (not twice)
87
- * ```
88
- *
89
- * @example Nested batches
90
- * ```ts
91
- * batch(() => {
92
- * a.set(1);
93
- * batch(() => {
94
- * b.set(2);
95
- * c.set(3);
96
- * });
97
- * d.set(4);
98
- * });
99
- * // All updates batched together, listeners notified once at outer batch end
100
- * ```
101
- *
102
- * @example Return value
103
- * ```ts
104
- * const result = batch(() => {
105
- * counter.set(10);
106
- * return counter.get() * 2;
107
- * });
108
- * console.log(result); // 20
109
- * ```
110
- *
111
- * @example With async operations (be careful!)
112
- * ```ts
113
- * // ❌ Wrong: async operations escape the batch
114
- * batch(async () => {
115
- * a.set(1);
116
- * await delay(100);
117
- * b.set(2); // This is OUTSIDE the batch!
118
- * });
119
- *
120
- * // ✅ Correct: batch sync operations only
121
- * batch(() => {
122
- * a.set(1);
123
- * b.set(2);
124
- * });
125
- * await delay(100);
126
- * batch(() => {
127
- * c.set(3);
128
- * });
129
- * ```
130
- */
131
- export function batch<T>(fn: () => T): T {
132
- batchDepth++;
133
-
134
- // First batch - set up the notification hook with deduping
135
- if (batchDepth === 1) {
136
- // Use Set to dedupe listeners - if same listener is scheduled multiple times,
137
- // it only gets called once (e.g., component subscribed to multiple atoms)
138
- let pendingListeners = new Set<VoidFunction>();
139
-
140
- // Schedule listener to be called at batch end (deduped by Set)
141
- const scheduleListener = (listener: VoidFunction) => {
142
- pendingListeners.add(listener);
143
- };
144
-
145
- try {
146
- return hook.use([scheduleNotifyHook(() => scheduleListener)], fn);
147
- } finally {
148
- batchDepth--;
149
-
150
- // Process pending listeners, handling cascading updates
151
- // Keep the hook active so any updates triggered by listeners are also batched
152
- hook.use([scheduleNotifyHook(() => scheduleListener)], () => {
153
- while (pendingListeners.size > 0) {
154
- // Snapshot and clear before calling to handle re-entrancy
155
- const listeners = pendingListeners;
156
- pendingListeners = new Set();
157
-
158
- for (const listener of listeners) {
159
- listener();
160
- }
161
- }
162
- });
163
- }
164
- }
165
-
166
- // Nested batch - just run the function (outer batch handles notifications)
167
- try {
168
- return fn();
169
- } finally {
170
- batchDepth--;
171
- }
172
- }