[IMP] web: stop using guardedCatch in Mutex

Guarded catch promotes bad patterns (using promise rejections as control
flow instead of as errors), as such, we should not use it in non-legacy
code. This commit reworks the exec method of the Mutex utility class to
avoid using it.

closes odoo/odoo#93081

Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
This commit is contained in:
Samuel Degueldre
2022-06-08 13:39:04 +02:00
parent cf91caa284
commit b74febe128
+15 -22
View File
@@ -83,31 +83,24 @@ export class Mutex {
* @returns {Promise}
*/
async exec(action) {
const currentLock = this._lock;
let result;
this._queueSize++;
this._unlockedProm =
this._unlockedProm ||
new Promise((resolve) => {
this._unlock = resolve;
});
this._lock = new Promise((unlockCurrent) => {
currentLock.then(() => {
result = action();
const always = (returnedResult) => {
unlockCurrent();
this._queueSize--;
if (this._queueSize === 0) {
this._unlockedProm = undefined;
this._unlock();
}
return returnedResult;
if (!this._unlockedProm) {
this._unlockedProm = new Promise((resolve) => {
this._unlock = () => {
resolve();
this._unlockedProm = undefined;
};
Promise.resolve(result).then(always).guardedCatch(always);
});
});
await this._lock;
return result;
}
const always = () => {
return Promise.resolve(action()).finally(() => {
if (--this._queueSize === 0) {
this._unlock();
}
});
};
this._lock = this._lock.then(always, always);
return this._lock;
}
/**
* @returns {Promise} resolved as soon as the Mutex is unlocked