Compare commits

...

1 Commits

Author SHA1 Message Date
Géry Debongnie 7ad08a6825 [TEST] experiment qith queuemicrotask 2023-01-11 14:52:35 +01:00
+13 -14
View File
@@ -10,20 +10,19 @@ export type Callback = () => void;
*/ */
export function batched(callback: Callback): Callback { export function batched(callback: Callback): Callback {
let called = false; let called = false;
return async () => { return () => {
// This await blocks all calls to the callback here, then releases them sequentially queueMicrotask(() => {
// in the next microtick. This line decides the granularity of the batch. if (!called) {
await Promise.resolve(); called = true;
if (!called) { // wait for all calls in this microtick to fall through before resetting "called"
called = true; // so that only the first call to the batched function calls the original callback.
// wait for all calls in this microtick to fall through before resetting "called" // Schedule this before calling the callback so that calls to the batched function
// so that only the first call to the batched function calls the original callback. // within the callback will proceed only after resetting called to false, and have
// Schedule this before calling the callback so that calls to the batched function // a chance to execute the callback again
// within the callback will proceed only after resetting called to false, and have queueMicrotask(() => (called = false));
// a chance to execute the callback again callback();
Promise.resolve().then(() => (called = false)); }
callback(); });
}
}; };
} }