From 7ad08a6825bd6b760dae830f5c46b44c8090ae06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Wed, 11 Jan 2023 14:52:35 +0100 Subject: [PATCH] [TEST] experiment qith queuemicrotask --- src/runtime/utils.ts | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/runtime/utils.ts b/src/runtime/utils.ts index 2bc5c8c0..aa112866 100644 --- a/src/runtime/utils.ts +++ b/src/runtime/utils.ts @@ -10,20 +10,19 @@ export type Callback = () => void; */ export function batched(callback: Callback): Callback { let called = false; - return async () => { - // This await blocks all calls to the callback here, then releases them sequentially - // in the next microtick. This line decides the granularity of the batch. - await Promise.resolve(); - if (!called) { - called = true; - // wait for all calls in this microtick to fall through before resetting "called" - // so that only the first call to the batched function calls the original callback. - // 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 - // a chance to execute the callback again - Promise.resolve().then(() => (called = false)); - callback(); - } + return () => { + queueMicrotask(() => { + if (!called) { + called = true; + // wait for all calls in this microtick to fall through before resetting "called" + // so that only the first call to the batched function calls the original callback. + // 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 + // a chance to execute the callback again + queueMicrotask(() => (called = false)); + callback(); + } + }); }; }