Skip to content

Commit f613c1b

Browse files
authored
Delete batched callback before calling (#202)
If a `callback` within `batchedCallbacks` calls `assign` we have an infinite loop. Removing the callback from `batchedCallbacks` before executing it breaks this loop. Consider a callback calling `assign`. `assign` will call `batch`, which will loop through all `batchedCallbacks` and execute each one. If one of those callbacks calls `assign` we will continue indefinitely. By first removing the callback from `batchedCallbacks`, then executing we ensure that when the callback calls `assign` the callback is no longer part of the enqueued `batchedCallbacks`.
1 parent bbb5863 commit f613c1b

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/observable.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ export function batch(callback: () => void) {
3737
callback();
3838
batchDepth--;
3939
if (batchDepth === 0) {
40-
for (const [callback, state] of batchedCallbacks.entries()) callback(state);
41-
batchedCallbacks.clear();
40+
for (const [callback, state] of batchedCallbacks.entries()) {
41+
batchedCallbacks.delete(callback);
42+
callback(state);
43+
}
4244
}
4345
}
4446

0 commit comments

Comments
 (0)