mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[REF] runtime: simplify implementation of batched
Currently the implementation of batched is quite complicated and difficult to read. This is because this approach tried to block all calls at the same point and then only let the first one go through, but an alternative approach is to simply throw away the calls that are made after the first one has been scheduled. This change makes the implementation much simpler to understand. Co-authored-by: Aaron Bohy <aab@odoo.com>
This commit is contained in:
committed by
Géry Debongnie
parent
e4c296a7d2
commit
3e9ba9ca8e
+7
-14
@@ -9,20 +9,13 @@ export type Callback = () => void;
|
|||||||
* @returns a batched version of the original callback
|
* @returns a batched version of the original callback
|
||||||
*/
|
*/
|
||||||
export function batched(callback: Callback): Callback {
|
export function batched(callback: Callback): Callback {
|
||||||
let called = false;
|
let scheduled = false;
|
||||||
return async () => {
|
return async (...args) => {
|
||||||
// This await blocks all calls to the callback here, then releases them sequentially
|
if (!scheduled) {
|
||||||
// in the next microtick. This line decides the granularity of the batch.
|
scheduled = true;
|
||||||
await Promise.resolve();
|
await Promise.resolve();
|
||||||
if (!called) {
|
scheduled = false;
|
||||||
called = true;
|
callback(...args);
|
||||||
// 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();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user