From 5ce4f3dd9311cd51ea1f52b6414a4a9b8c4ac730 Mon Sep 17 00:00:00 2001 From: Jorge Pinna Puissant Date: Mon, 21 Aug 2023 08:17:30 +0000 Subject: [PATCH] [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 --- .../static/src/legacy/js/core/concurrency.js | 59 ------------------- .../tests/legacy/core/concurrency_tests.js | 45 -------------- .../static/src/snippets/s_searchbar/000.js | 6 +- .../static/src/js/website_sale_delivery.js | 6 +- .../static/src/js/address_form.js | 6 +- 5 files changed, 9 insertions(+), 113 deletions(-) diff --git a/addons/web/static/src/legacy/js/core/concurrency.js b/addons/web/static/src/legacy/js/core/concurrency.js index d801d3777f4..37319c50469 100644 --- a/addons/web/static/src/legacy/js/core/concurrency.js +++ b/addons/web/static/src/legacy/js/core/concurrency.js @@ -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; diff --git a/addons/web/static/tests/legacy/core/concurrency_tests.js b/addons/web/static/tests/legacy/core/concurrency_tests.js index 074a993c019..42a5ed0fa1b 100644 --- a/addons/web/static/tests/legacy/core/concurrency_tests.js +++ b/addons/web/static/tests/legacy/core/concurrency_tests.js @@ -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); diff --git a/addons/website/static/src/snippets/s_searchbar/000.js b/addons/website/static/src/snippets/s_searchbar/000.js index c7c8f923780..578ce90621e 100644 --- a/addons/website/static/src/snippets/s_searchbar/000.js +++ b/addons/website/static/src/snippets/s_searchbar/000.js @@ -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)); } }, /** diff --git a/addons/website_sale/static/src/js/website_sale_delivery.js b/addons/website_sale/static/src/js/website_sale_delivery.js index 10a9e27e04a..505c25a062f 100644 --- a/addons/website_sale/static/src/js/website_sale_delivery.js +++ b/addons/website_sale/static/src/js/website_sale_delivery.js @@ -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, diff --git a/addons/website_sale_autocomplete/static/src/js/address_form.js b/addons/website_sale_autocomplete/static/src/js/address_form.js index 865430b5e5d..010dd372e99 100644 --- a/addons/website_sale_autocomplete/static/src/js/address_form.js +++ b/addons/website_sale_autocomplete/static/src/js/address_form.js @@ -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: {