mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7ad08a6825 |
+13
-14
@@ -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();
|
});
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user