[REF] web, *: remove legacy DropPrevious

In our continued effort to remove legacy code, this commit replaces
usage of the legacy DropPrevious util with the KeepLast util. The legacy
DropPrevious has been removed.

Part-of task-id 3439226

Part-of: odoo/odoo#132559
This commit is contained in:
Jorge Pinna Puissant
2023-08-23 11:12:24 +02:00
parent 390ec46c8d
commit 5ce4f3dd93
5 changed files with 9 additions and 113 deletions
@@ -76,64 +76,6 @@ const concurrency = {
return res;
},
}),
/**
* The DropPrevious abstraction is useful when you have a sequence of
* operations that you want to execute, but you only care of the result of
* the last operation.
*
* For example, let us say that we have a _fetch method on a widget which
* fetches data. We want to rerender the widget after. We could do this::
*
* this._fetch().then(function (result) {
* self.state = result;
* self.render();
* });
*
* Now, we have at least two problems:
*
* - if this code is called twice and the second _fetch completes before the
* first, the end state will be the result of the first _fetch, which is
* not what we expect
* - in any cases, the user interface will rerender twice, which is bad.
*
* Now, if we have a DropPrevious::
*
* this.dropPrevious = new DropPrevious();
*
* Then we can wrap the _fetch in a DropPrevious and have the expected
* result::
*
* this.dropPrevious
* .add(this._fetch())
* .then(function (result) {
* self.state = result;
* self.render();
* });
*/
DropPrevious: Class.extend({
/**
* Registers a new promise and rejects the previous one
*
* @param {Promise} promise the new promise
* @returns {Promise}
*/
add: function (promise) {
if (this.currentDef) {
this.currentDef.reject();
}
var rejection;
var res = new Promise(function (resolve, reject) {
rejection = reject;
promise.then(resolve).catch(function (reason) {
reject(reason);
});
});
this.currentDef = res;
this.currentDef.reject = rejection;
return res;
}
}),
/**
* A MutexedDropPrevious is a primitive for serializing computations while
* skipping the ones that where executed between a current one and before
@@ -238,5 +180,4 @@ const concurrency = {
};
export const delay = concurrency.delay
export const DropPrevious = concurrency.DropPrevious
export default concurrency;
@@ -140,51 +140,6 @@ QUnit.module('core', {}, function () {
assert.verifySteps(['mutex unlocked']);
});
QUnit.test('DropPrevious: basic usecase', async function (assert) {
assert.expect(4);
var dp = new concurrency.DropPrevious();
var prom1 = makeTestPromise(assert, 'prom1');
var prom2 = makeTestPromise(assert, 'prom2');
dp.add(prom1).then(() => assert.step('should not go here'))
.catch(()=> assert.step("rejected dp1"));
dp.add(prom2).then(() => assert.step("ok dp2"));
await testUtils.nextMicrotaskTick();
assert.verifySteps(['rejected dp1']);
prom2.resolve();
await testUtils.nextMicrotaskTick();
assert.verifySteps(['ok dp2']);
});
QUnit.test('DropPrevious: resolve first before last', async function (assert) {
assert.expect(4);
var dp = new concurrency.DropPrevious();
var prom1 = makeTestPromise(assert, 'prom1');
var prom2 = makeTestPromise(assert, 'prom2');
dp.add(prom1).then(() => assert.step('should not go here'))
.catch(()=> assert.step("rejected dp1"));
dp.add(prom2).then(() => assert.step("ok dp2"));
await testUtils.nextMicrotaskTick();
assert.verifySteps(['rejected dp1']);
prom1.resolve();
prom2.resolve();
await testUtils.nextMicrotaskTick();
assert.verifySteps(['ok dp2']);
});
QUnit.test('DropMisordered: resolve all correctly ordered, sync', async function (assert) {
assert.expect(1);
@@ -1,6 +1,6 @@
/** @odoo-module **/
import concurrency from '@web/legacy/js/core/concurrency';
import { KeepLast } from "@web/core/utils/concurrency";
import publicWidget from '@web/legacy/js/public/public_widget';
import { renderToElement, renderToString } from "@web/core/utils/render";
@@ -23,7 +23,7 @@ publicWidget.registry.searchBar = publicWidget.Widget.extend({
init: function () {
this._super.apply(this, arguments);
this._dp = new concurrency.DropPrevious();
this.keepLast = new KeepLast();
this._onInput = debounce(this._onInput, 400);
this._onFocusOut = debounce(this._onFocusOut, 100);
@@ -224,7 +224,7 @@ publicWidget.registry.searchBar = publicWidget.Widget.extend({
if (this.searchType === 'all' && !this.$input.val().trim().length) {
this._render();
} else {
this._dp.add(this._fetch()).then(this._render.bind(this));
this.keepLast.add(this._fetch()).then(this._render.bind(this));
}
},
/**
@@ -4,7 +4,7 @@ import core from "@web/legacy/js/services/core";
import publicWidget from "@web/legacy/js/public/public_widget";
import { _t } from "@web/core/l10n/translation";
import { renderToElement } from "@web/core/utils/render";
import concurrency from "@web/legacy/js/core/concurrency";
import { KeepLast } from "@web/core/utils/concurrency";
publicWidget.registry.websiteSaleDelivery = publicWidget.Widget.extend({
selector: '.oe_website_sale',
@@ -22,7 +22,7 @@ publicWidget.registry.websiteSaleDelivery = publicWidget.Widget.extend({
*/
start: async function () {
this.carriers = Array.from(document.querySelectorAll('input[name="delivery_type"]'));
this.dp = new concurrency.DropPrevious();
this.keepLast = new KeepLast();
// Workaround to:
// - update the amount/error on the label at first rendering
// - prevent clicking on 'Pay Now' if the shipper rating fails
@@ -201,7 +201,7 @@ publicWidget.registry.websiteSaleDelivery = publicWidget.Widget.extend({
_checkCarrier: async function (ev, carrier_id) {
ev.stopPropagation();
await this.dp.add(this._rpc({
await this.keepLast.add(this._rpc({
route: '/shop/update_carrier',
params: {
carrier_id: carrier_id,
@@ -1,7 +1,7 @@
/** @odoo-module */
import publicWidget from '@web/legacy/js/public/public_widget';
import { DropPrevious } from '@web/legacy/js/core/concurrency';
import { KeepLast } from "@web/core/utils/concurrency";
import { debounce } from "@web/core/utils/timing";
import { renderToElement } from "@web/core/utils/render";
@@ -17,7 +17,7 @@ publicWidget.registry.AddressForm = publicWidget.Widget.extend({
this.zipInput = document.querySelector('input[name="zip"]');
this.countrySelect = document.querySelector('select[name="country_id"]');
this.stateSelect = document.querySelector('select[name="state_id"]');
this.dp = new DropPrevious();
this.keepLast = new KeepLast();
this.sessionId = this._generateUUID();
this._onChangeStreet = debounce(this._onChangeStreet, 200);
@@ -46,7 +46,7 @@ publicWidget.registry.AddressForm = publicWidget.Widget.extend({
_onChangeStreet: async function (ev) {
const inputContainer = ev.currentTarget.parentNode;
if (ev.currentTarget.value.length >= 5) {
this.dp.add(
this.keepLast.add(
this._rpc({
route: '/autocomplete/address',
params: {