Skip to content

Commit

Permalink
[IMP] index: export batched utility function
Browse files Browse the repository at this point in the history
'batched' will be used in odoo/o-spreadsheet.
There's also a copied version (slightly modified) in odoo/odoo.

It shows it could be useful outside owl.
  • Loading branch information
LucasLefevre committed May 22, 2024
1 parent 7952f31 commit 3d1b15a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions doc/reference/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ functions are all available in the `owl.utils` namespace.
- [`loadFile`](#loadfile): loading a file (useful for templates)
- [`EventBus`](#eventbus): a simple EventBus
- [`validate`](#validate): a validation function
- [`batched`](#batched): batch function calls

## `whenReady`

Expand Down Expand Up @@ -78,3 +79,22 @@ validate(
// - 'id' is missing (should be a number),
// - 'url' is missing (should be a boolean or list of numbers),
```

## `batched`

The `batched` function creates a batched version of a callback so that multiple calls to it within the same microtick will only result in a single invocation of the original callback.

```js
function hello() {
console.log("hello");
}

const batchedHello = batched(hello);
batchedHello();
// Nothing is logged
batchedHello();
// Still not logged

await Promise.resolve(); // Await the next microtick
// "hello" is logged only once
```
2 changes: 1 addition & 1 deletion src/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export { useComponent, useState } from "./component_node";
export { status } from "./status";
export { reactive, markRaw, toRaw } from "./reactivity";
export { useEffect, useEnv, useExternalListener, useRef, useChildSubEnv, useSubEnv } from "./hooks";
export { EventBus, whenReady, loadFile, markup } from "./utils";
export { batched, EventBus, whenReady, loadFile, markup } from "./utils";
export {
onWillStart,
onMounted,
Expand Down

0 comments on commit 3d1b15a

Please sign in to comment.