Compare commits

...

1 Commits

Author SHA1 Message Date
Géry Debongnie 7ad08a6825 [TEST] experiment qith queuemicrotask 2023-01-11 14:52:35 +01:00
+4 -5
View File
@@ -10,10 +10,8 @@ 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.
await Promise.resolve();
if (!called) { if (!called) {
called = true; called = true;
// wait for all calls in this microtick to fall through before resetting "called" // wait for all calls in this microtick to fall through before resetting "called"
@@ -21,9 +19,10 @@ export function batched(callback: Callback): Callback {
// Schedule this before calling the callback so that calls to the batched function // Schedule this before calling the callback so that calls to the batched function
// within the callback will proceed only after resetting called to false, and have // within the callback will proceed only after resetting called to false, and have
// a chance to execute the callback again // a chance to execute the callback again
Promise.resolve().then(() => (called = false)); queueMicrotask(() => (called = false));
callback(); callback();
} }
});
}; };
} }