atomirx 0.0.1
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.
- package/README.md +1666 -0
- package/coverage/base.css +224 -0
- package/coverage/block-navigation.js +87 -0
- package/coverage/clover.xml +1440 -0
- package/coverage/coverage-final.json +14 -0
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +131 -0
- package/coverage/prettify.css +1 -0
- package/coverage/prettify.js +2 -0
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +210 -0
- package/coverage/src/core/atom.ts.html +889 -0
- package/coverage/src/core/batch.ts.html +223 -0
- package/coverage/src/core/define.ts.html +805 -0
- package/coverage/src/core/emitter.ts.html +919 -0
- package/coverage/src/core/equality.ts.html +631 -0
- package/coverage/src/core/hook.ts.html +460 -0
- package/coverage/src/core/index.html +281 -0
- package/coverage/src/core/isAtom.ts.html +100 -0
- package/coverage/src/core/isPromiseLike.ts.html +133 -0
- package/coverage/src/core/onCreateHook.ts.html +136 -0
- package/coverage/src/core/scheduleNotifyHook.ts.html +94 -0
- package/coverage/src/core/types.ts.html +523 -0
- package/coverage/src/core/withUse.ts.html +253 -0
- package/coverage/src/index.html +116 -0
- package/coverage/src/index.ts.html +106 -0
- package/dist/core/atom.d.ts +63 -0
- package/dist/core/atom.test.d.ts +1 -0
- package/dist/core/atomState.d.ts +104 -0
- package/dist/core/atomState.test.d.ts +1 -0
- package/dist/core/batch.d.ts +126 -0
- package/dist/core/batch.test.d.ts +1 -0
- package/dist/core/define.d.ts +173 -0
- package/dist/core/define.test.d.ts +1 -0
- package/dist/core/derived.d.ts +102 -0
- package/dist/core/derived.test.d.ts +1 -0
- package/dist/core/effect.d.ts +120 -0
- package/dist/core/effect.test.d.ts +1 -0
- package/dist/core/emitter.d.ts +237 -0
- package/dist/core/emitter.test.d.ts +1 -0
- package/dist/core/equality.d.ts +62 -0
- package/dist/core/equality.test.d.ts +1 -0
- package/dist/core/hook.d.ts +134 -0
- package/dist/core/hook.test.d.ts +1 -0
- package/dist/core/isAtom.d.ts +9 -0
- package/dist/core/isPromiseLike.d.ts +9 -0
- package/dist/core/isPromiseLike.test.d.ts +1 -0
- package/dist/core/onCreateHook.d.ts +79 -0
- package/dist/core/promiseCache.d.ts +134 -0
- package/dist/core/promiseCache.test.d.ts +1 -0
- package/dist/core/scheduleNotifyHook.d.ts +51 -0
- package/dist/core/select.d.ts +151 -0
- package/dist/core/selector.test.d.ts +1 -0
- package/dist/core/types.d.ts +279 -0
- package/dist/core/withUse.d.ts +38 -0
- package/dist/core/withUse.test.d.ts +1 -0
- package/dist/index-2ok7ilik.js +1217 -0
- package/dist/index-B_5SFzfl.cjs +1 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +20 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/react/index.cjs +30 -0
- package/dist/react/index.d.ts +7 -0
- package/dist/react/index.js +823 -0
- package/dist/react/rx.d.ts +250 -0
- package/dist/react/rx.test.d.ts +1 -0
- package/dist/react/strictModeTest.d.ts +10 -0
- package/dist/react/useAction.d.ts +381 -0
- package/dist/react/useAction.test.d.ts +1 -0
- package/dist/react/useStable.d.ts +183 -0
- package/dist/react/useStable.test.d.ts +1 -0
- package/dist/react/useValue.d.ts +134 -0
- package/dist/react/useValue.test.d.ts +1 -0
- package/package.json +57 -0
- package/scripts/publish.js +198 -0
- package/src/core/atom.test.ts +369 -0
- package/src/core/atom.ts +189 -0
- package/src/core/atomState.test.ts +342 -0
- package/src/core/atomState.ts +256 -0
- package/src/core/batch.test.ts +257 -0
- package/src/core/batch.ts +172 -0
- package/src/core/define.test.ts +342 -0
- package/src/core/define.ts +243 -0
- package/src/core/derived.test.ts +381 -0
- package/src/core/derived.ts +339 -0
- package/src/core/effect.test.ts +196 -0
- package/src/core/effect.ts +184 -0
- package/src/core/emitter.test.ts +364 -0
- package/src/core/emitter.ts +392 -0
- package/src/core/equality.test.ts +392 -0
- package/src/core/equality.ts +182 -0
- package/src/core/hook.test.ts +227 -0
- package/src/core/hook.ts +177 -0
- package/src/core/isAtom.ts +27 -0
- package/src/core/isPromiseLike.test.ts +72 -0
- package/src/core/isPromiseLike.ts +16 -0
- package/src/core/onCreateHook.ts +92 -0
- package/src/core/promiseCache.test.ts +239 -0
- package/src/core/promiseCache.ts +279 -0
- package/src/core/scheduleNotifyHook.ts +53 -0
- package/src/core/select.ts +454 -0
- package/src/core/selector.test.ts +257 -0
- package/src/core/types.ts +311 -0
- package/src/core/withUse.test.ts +249 -0
- package/src/core/withUse.ts +56 -0
- package/src/index.test.ts +80 -0
- package/src/index.ts +51 -0
- package/src/react/index.ts +20 -0
- package/src/react/rx.test.tsx +416 -0
- package/src/react/rx.tsx +300 -0
- package/src/react/strictModeTest.tsx +71 -0
- package/src/react/useAction.test.ts +989 -0
- package/src/react/useAction.ts +605 -0
- package/src/react/useStable.test.ts +553 -0
- package/src/react/useStable.ts +288 -0
- package/src/react/useValue.test.ts +182 -0
- package/src/react/useValue.ts +261 -0
- package/tsconfig.json +9 -0
- package/v2.md +725 -0
- package/vite.config.ts +39 -0
|
@@ -0,0 +1,257 @@
|
|
|
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.value).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.value).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.value).toBe(2);
|
|
91
|
+
expect(b.value).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.value).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.value !== undefined && a.value > 0) {
|
|
142
|
+
b.set(a.value * 2);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
a.on(listenerA);
|
|
147
|
+
b.on(listenerB);
|
|
148
|
+
|
|
149
|
+
batch(() => {
|
|
150
|
+
a.set(5);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
expect(a.value).toBe(5);
|
|
154
|
+
expect(b.value).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.value).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
|
+
});
|
|
@@ -0,0 +1,172 @@
|
|
|
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.value));
|
|
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.value, b.value);
|
|
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.value * 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
|
+
}
|