Skip to content

Latest commit

 

History

History
56 lines (40 loc) · 1.26 KB

File metadata and controls

56 lines (40 loc) · 1.26 KB

Utils

Owl export a few useful utility functions, to help with common issues. Those functions are all available in the owl.utils namespace.

whenReady

The function whenReady returns a Promise resolved when the DOM is ready (if not ready yet, resolved directly otherwise). If called with a callback as argument, it executes it as soon as the DOM ready (or directly).

const { whenReady } = owl;

await whenReady();
// do something

or alternatively:

whenReady(function () {
  // do something
});

EventBus

It is a simple EventBus, with the same API as usual DOM elements, and an additional trigger method to dispatch events:

const bus = new EventBus();
bus.addEventListener("event", () => console.log("something happened"));

bus.trigger("event"); // 'something happened' is logged

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.

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