Files
odoo_source/addons/web/static/tests/owl2_compatibility_app.js
T
+2 6d13271aac [REF] web: adapt code to owl 2
Owl 2 changelog: https://github.com/odoo/owl/blob/a9f29c4caad4f32d06be1ec4780572825781cd9b/CHANGELOG.md

Part-of: odoo/odoo#80156
Co-authored-by: Aaron Bohy <aab@odoo.com>
Co-authored-by: Bruno Boi <boi@odoo.com>
Co-authored-by: Géry Debongnie <ged@odoo.com>
Co-authored-by: Samuel Degueldre <sad@odoo.com>
Co-authored-by: Mathieu Duckerts-Antoine <dam@odoo.com>
Co-authored-by: Simon Genin (ges) <ges@odoo.com>
Co-authored-by: Francois (fge) <fge@odoo.com>
Co-authored-by: Michael Mattiello (mcm) <mcm@odoo.com>
Co-authored-by: Julien Mougenot <jum@odoo.com>
Co-authored-by: luvi <luvi@odoo.com>
Co-authored-by: Lucas Perais (lpe) <lpe@odoo.com>
Co-authored-by: Jorge Pinna Puissant <jpp@odoo.com>
2022-02-10 07:46:12 +00:00

84 lines
3.0 KiB
JavaScript

(function () {
const App = owl.App;
const stopPromises = [];
const schedulers = new Set();
function hookIntoScheduler(scheduler) {
const { stop: originalStop } = scheduler;
scheduler.stop = function stop() {
const wasRunning = this.isRunning;
originalStop.call(this);
if (wasRunning) {
while (stopPromises.length) {
stopPromises.pop().resolve();
}
}
};
}
owl.App = class extends App {
constructor() {
super(...arguments);
this.setup();
hookIntoScheduler(this.scheduler);
schedulers.add(this.scheduler);
}
destroy() {
schedulers.delete(this.scheduler);
super.destroy();
}
setup() {}
};
/**
* Returns a promise resolved the next time OWL stops rendering.
*
* @param {function} func function which, when called, is
* expected to trigger OWL render(s).
* @param {number} [timeoutDelay=5000] in ms
* @returns {Promise}
*/
owl.App.afterNextRender = async function afterNextRender(func, timeoutDelay = 5000) {
// Define the potential errors outside of the promise to get a proper
// trace if they happen.
const startError = new Error("Timeout: the render didn't start.");
const stopError = new Error("Timeout: the render didn't stop.");
// Set up the timeout to reject if no render happens.
let timeoutNoRender;
const timeoutProm = new Promise((resolve, reject) => {
timeoutNoRender = setTimeout(() => {
let error = startError;
if ([...schedulers].some(({ isRunning }) => isRunning)) {
error = stopError;
}
console.error(error);
reject(error);
}, timeoutDelay);
});
// Set up the promise to resolve if a render happens.
let resolve;
const prom = new Promise((res) => {
resolve = res;
});
prom.resolve = resolve;
stopPromises.push(prom);
// Start the function expected to trigger a render after the promise
// has been registered to not miss any potential render.
const funcRes = func();
// Make them race (first to resolve/reject wins).
await Promise.race([prom, timeoutProm]);
clearTimeout(timeoutNoRender);
// Wait the end of the function to ensure all potential effects are
// taken into account during the following verification step.
await funcRes;
// Wait one more frame to make sure no new render has been queued.
await new Promise(function (resolve) {
setTimeout(() => window.requestAnimationFrame(() => resolve()));
});
if ([...schedulers].some(({ isRunning }) => isRunning)) {
await afterNextRender(() => {}, timeoutDelay);
}
};
})();