From 3ea0b115ca3c32c0ec296a9dd93e49af8e6b38a4 Mon Sep 17 00:00:00 2001 From: "Michael (mcm)" Date: Thu, 10 Aug 2023 14:08:16 +0000 Subject: [PATCH] [IMP] web: remove legacy service system This commit removes the legacy service classes: - AbstractService - AjaxService - AbstractStorageService - LocalStorageService, - SessionStorageService Only AjaxService was still used as a service so it is moves in the "common_env" file. task 3439226 closes odoo/odoo#131683 Signed-off-by: Samuel Degueldre (sad) --- addons/im_livechat/__manifest__.py | 1 - addons/mail/__manifest__.py | 1 - addons/web/__manifest__.py | 10 --- addons/web/static/src/legacy/js/common_env.js | 19 ++++ .../src/legacy/js/core/abstract_service.js | 89 ------------------- .../js/core/abstract_storage_service.js | 85 ------------------ .../src/legacy/js/public/public_root.js | 2 - .../src/legacy/js/services/ajax_service.js | 29 ------ .../web/static/src/legacy/js/services/core.js | 5 -- .../js/services/local_storage_service.js | 17 ---- .../js/services/session_storage_service.js | 17 ---- addons/web/static/src/legacy/legacy_setup.js | 2 - addons/web/static/src/legacy/utils.js | 8 -- .../static/tests/legacy/core/widget_tests.js | 5 +- addons/website_slides/__manifest__.py | 5 -- 15 files changed, 21 insertions(+), 274 deletions(-) delete mode 100644 addons/web/static/src/legacy/js/core/abstract_service.js delete mode 100644 addons/web/static/src/legacy/js/core/abstract_storage_service.js delete mode 100644 addons/web/static/src/legacy/js/services/ajax_service.js delete mode 100644 addons/web/static/src/legacy/js/services/local_storage_service.js delete mode 100644 addons/web/static/src/legacy/js/services/session_storage_service.js diff --git a/addons/im_livechat/__manifest__.py b/addons/im_livechat/__manifest__.py index bfda8dd83ea..43aede2da51 100644 --- a/addons/im_livechat/__manifest__.py +++ b/addons/im_livechat/__manifest__.py @@ -116,7 +116,6 @@ Help your customers with this chat, and analyse their feedback. 'web/static/src/core/utils/transitions.scss', 'web/static/src/core/**/*', 'web/static/src/env.js', - ('remove', 'web/static/src/legacy/js/services/ajax_service.js'), ('remove', 'web/static/src/legacy/js/common_env.js'), ('remove', 'web/static/src/core/emoji_picker/emoji_data.js'), 'web/static/src/views/fields/formatters.js', diff --git a/addons/mail/__manifest__.py b/addons/mail/__manifest__.py index ce06177d735..3706a601b20 100644 --- a/addons/mail/__manifest__.py +++ b/addons/mail/__manifest__.py @@ -174,7 +174,6 @@ For more specific needs, you may also assign custom-defined actions 'web/static/src/core/utils/transitions.scss', # included early because used by other files 'web/static/src/core/**/*', 'web/static/src/env.js', - ('remove', 'web/static/src/legacy/js/services/ajax_service.js'), ('remove', 'web/static/src/core/emoji_picker/emoji_data.js'), 'web/static/src/views/fields/formatters.js', 'web/static/src/views/fields/file_handler.*', diff --git a/addons/web/__manifest__.py b/addons/web/__manifest__.py index fa8f24fa8ce..8f039d29582 100644 --- a/addons/web/__manifest__.py +++ b/addons/web/__manifest__.py @@ -119,8 +119,6 @@ This module provides the core of the Odoo Web Client. 'web/static/src/legacy/js/libs/pdfjs.js', 'web/static/src/legacy/js/libs/zoomodoo.js', 'web/static/src/legacy/js/libs/jSignatureCustom.js', - 'web/static/src/legacy/js/core/abstract_service.js', - 'web/static/src/legacy/js/core/abstract_storage_service.js', 'web/static/src/legacy/js/core/ajax.js', 'web/static/src/legacy/js/core/bus.js', 'web/static/src/legacy/js/core/class.js', @@ -141,11 +139,8 @@ This module provides the core of the Odoo Web Client. 'web/static/src/legacy/js/core/translation.js', 'web/static/src/legacy/js/core/utils.js', 'web/static/src/legacy/js/core/widget.js', - 'web/static/src/legacy/js/services/ajax_service.js', 'web/static/src/legacy/js/services/config.js', 'web/static/src/legacy/js/services/core.js', - 'web/static/src/legacy/js/services/local_storage_service.js', - 'web/static/src/legacy/js/services/session_storage_service.js', 'web/static/src/legacy/js/common_env.js', 'web/static/src/legacy/js/widgets/name_and_signature.js', 'web/static/src/legacy/xml/name_and_signature.xml', @@ -314,8 +309,6 @@ This module provides the core of the Odoo Web Client. 'web/static/src/legacy/js/libs/pdfjs.js', 'web/static/src/legacy/js/libs/zoomodoo.js', 'web/static/src/legacy/js/libs/jSignatureCustom.js', - 'web/static/src/legacy/js/core/abstract_service.js', - 'web/static/src/legacy/js/core/abstract_storage_service.js', 'web/static/src/legacy/js/core/ajax.js', 'web/static/src/legacy/js/core/bus.js', 'web/static/src/legacy/js/core/class.js', @@ -336,11 +329,8 @@ This module provides the core of the Odoo Web Client. 'web/static/src/legacy/js/core/translation.js', 'web/static/src/legacy/js/core/utils.js', 'web/static/src/legacy/js/core/widget.js', - 'web/static/src/legacy/js/services/ajax_service.js', 'web/static/src/legacy/js/services/config.js', 'web/static/src/legacy/js/services/core.js', - 'web/static/src/legacy/js/services/local_storage_service.js', - 'web/static/src/legacy/js/services/session_storage_service.js', 'web/static/src/legacy/js/common_env.js', 'web/static/src/legacy/js/widgets/name_and_signature.js', 'web/static/src/legacy/xml/name_and_signature.xml', diff --git a/addons/web/static/src/legacy/js/common_env.js b/addons/web/static/src/legacy/js/common_env.js index dbbfb621ffe..e2bb7bfca96 100644 --- a/addons/web/static/src/legacy/js/common_env.js +++ b/addons/web/static/src/legacy/js/common_env.js @@ -50,6 +50,25 @@ device, isDebug, services: { + ajax: { + rpc(route, args, options, target) { + let rpcPromise = null; + const promise = new Promise(function (resolve, reject) { + rpcPromise = session.rpc(route, args, options); + rpcPromise.then(function (result) { + if (!target.isDestroyed()) { + resolve(result); + } + }).guardedCatch(function (reason) { + if (!target.isDestroyed()) { + reject(reason); + } + }); + }); + promise.abort = rpcPromise.abort.bind(rpcPromise); + return promise; + }, + }, ajaxJsonRPC() { return jsonRpc(...arguments); }, diff --git a/addons/web/static/src/legacy/js/core/abstract_service.js b/addons/web/static/src/legacy/js/core/abstract_service.js deleted file mode 100644 index f729821ee25..00000000000 --- a/addons/web/static/src/legacy/js/core/abstract_service.js +++ /dev/null @@ -1,89 +0,0 @@ -/** @odoo-module **/ - -import Class from "@web/legacy/js/core/class"; -import { serviceRegistry } from "@web/legacy/js/services/core"; -import Mixins from "@web/legacy/js/core/mixins"; -import ServicesMixin from "@web/legacy/js/core/service_mixins"; - -var AbstractService = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, { - dependencies: [], - init: function (env) { - Mixins.EventDispatcherMixin.init.call(this, arguments); - this.env = env; - }, - /** - * @abstract - */ - start: function () {}, - /** - * Directly calls the requested service, instead of triggering a - * 'call_service' event up, which wouldn't work as services have no parent - * - * @param {OdooEvent} ev - */ - _trigger_up: function (ev) { - Mixins.EventDispatcherMixin._trigger_up.apply(this, arguments); - if (ev.is_stopped()) { - return; - } - const payload = ev.data; - if (ev.name === 'call_service') { - let args = payload.args || []; - if (payload.service === 'ajax' && payload.method === 'rpc') { - // ajax service uses an extra 'target' argument for rpc - args = args.concat(ev.target); - } - const service = this.env.services[payload.service]; - const result = service[payload.method].apply(service, args); - payload.callback(result); - } else if (ev.name === 'do_action') { - this.env.bus.trigger('do-action', payload); - } - }, - - //-------------------------------------------------------------------------- - // Static - //-------------------------------------------------------------------------- - - /** - * Deploy services in the env (specializations of AbstractService registered - * into the serviceRegistry). - * - * @static - * @param {Object} env - */ - deployServices(env) { - const UndeployedServices = Object.assign({}, serviceRegistry.map); - function _deployServices() { - let done = false; - while (!done) { - // find a service with no missing dependency - const serviceName = Object.keys(UndeployedServices).find(serviceName => { - const Service = UndeployedServices[serviceName]; - return Service.prototype.dependencies.every(depName => { - return env.services[depName]; - }); - }); - if (serviceName) { - const Service = UndeployedServices[serviceName]; - const service = new Service(env); - env.services[serviceName] = service; - delete UndeployedServices[serviceName]; - service.start(); - } else { - done = true; - } - } - } - serviceRegistry.onAdd((serviceName, Service) => { - if (serviceName in env.services || serviceName in UndeployedServices) { - throw new Error(`Service ${serviceName} is already loaded.`); - } - UndeployedServices[serviceName] = Service; - _deployServices(); - }); - _deployServices(); - } -}); - -export default AbstractService; diff --git a/addons/web/static/src/legacy/js/core/abstract_storage_service.js b/addons/web/static/src/legacy/js/core/abstract_storage_service.js deleted file mode 100644 index a675798ca61..00000000000 --- a/addons/web/static/src/legacy/js/core/abstract_storage_service.js +++ /dev/null @@ -1,85 +0,0 @@ -/** @odoo-module **/ - -/** - * This module defines an abstraction for services that write into Storage - * objects (e.g. localStorage or sessionStorage). - */ - -import AbstractService from "@web/legacy/js/core/abstract_service"; - -var AbstractStorageService = AbstractService.extend({ - // the 'storage' attribute must be set by actual StorageServices extending - // this abstraction - storage: null, - - /** - * @override - */ - destroy: function () { - // storage can be permanent or transient, destroy transient ones - if ((this.storage || {}).destroy) { - this.storage.destroy(); - } - this._super.apply(this, arguments); - }, - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - /** - * Removes all data from the storage - */ - clear: function() { - this.storage.clear(); - }, - /** - * Returns the value associated with a given key in the storage - * - * @param {string} key - * @returns {string} - */ - getItem: function(key, defaultValue) { - var val = this.storage.getItem(key); - return val ? JSON.parse(val) : defaultValue; - }, - /** - * @param {integer} index - * @return {string} - */ - key: function (index) { - return this.storage.key(index); - }, - /** - * @return {integer} - */ - length: function () { - return this.storage.length; - }, - /** - * Removes the given key from the storage - * - * @param {string} key - */ - removeItem: function(key) { - this.storage.removeItem(key); - }, - /** - * Sets the value of a given key in the storage - * - * @param {string} key - * @param {string} value - */ - setItem: function(key, value) { - this.storage.setItem(key, JSON.stringify(value)); - }, - /** - * Add an handler on storage event - * - */ - onStorage: function () { - this.storage.on.apply(this.storage, ["storage"].concat(Array.prototype.slice.call(arguments))); - }, -}); - -export default AbstractStorageService; diff --git a/addons/web/static/src/legacy/js/public/public_root.js b/addons/web/static/src/legacy/js/public/public_root.js index 0297bea5822..2067cbc0cdb 100644 --- a/addons/web/static/src/legacy/js/public/public_root.js +++ b/addons/web/static/src/legacy/js/public/public_root.js @@ -7,7 +7,6 @@ import {getCookie} from '@web/legacy/js/core/cookie_utils'; import publicWidget from '@web/legacy/js/public/public_widget'; import { registry } from '@web/core/registry'; -import AbstractService from "@web/legacy/js/core/abstract_service"; import lazyloader from "@web/legacy/js/public/lazyloader"; import { @@ -361,7 +360,6 @@ owl.Component.env = legacyEnv; */ export async function createPublicRoot(RootWidget) { await lazyloader.allScriptsLoaded; - AbstractService.prototype.deployServices(legacyEnv); // add a bunch of mapping services that will redirect service calls from the legacy env // to the wowl env serviceRegistry.add("legacy_session", makeLegacySessionService(legacyEnv, session)); diff --git a/addons/web/static/src/legacy/js/services/ajax_service.js b/addons/web/static/src/legacy/js/services/ajax_service.js deleted file mode 100644 index 1ef4981c93b..00000000000 --- a/addons/web/static/src/legacy/js/services/ajax_service.js +++ /dev/null @@ -1,29 +0,0 @@ -/** @odoo-module **/ - -import AbstractService from "@web/legacy/js/core/abstract_service"; -import core from "@web/legacy/js/services/core"; -import session from "web.session"; - -var AjaxService = AbstractService.extend({ - rpc: function (route, args, options, target) { - var rpcPromise; - var promise = new Promise(function (resolve, reject) { - rpcPromise = session.rpc(route, args, options); - rpcPromise.then(function (result) { - if (!target.isDestroyed()) { - resolve(result); - } - }).guardedCatch(function (reason) { - if (!target.isDestroyed()) { - reject(reason); - } - }); - }); - promise.abort = rpcPromise.abort.bind(rpcPromise); - return promise; - }, -}); - -core.serviceRegistry.add('ajax', AjaxService); - -export default AjaxService; diff --git a/addons/web/static/src/legacy/js/services/core.js b/addons/web/static/src/legacy/js/services/core.js index 81d76e76b71..80b69ce2226 100644 --- a/addons/web/static/src/legacy/js/services/core.js +++ b/addons/web/static/src/legacy/js/services/core.js @@ -2,7 +2,6 @@ import Bus from "@web/legacy/js/core/bus"; import Class from "@web/legacy/js/core/class"; -import Registry from "@web/legacy/js/core/registry"; import translation from "@web/legacy/js/core/translation"; /** @@ -25,7 +24,6 @@ export var bus = new Bus(); export const _t = translation._t; export const _lt = translation._lt; -export const serviceRegistry = new Registry(); export const csrf_token = odoo.csrf_token; export default { @@ -34,9 +32,6 @@ export default { bus: bus, _t: _t, _lt: _lt, - - // registries - serviceRegistry: serviceRegistry, /** * @type {String} */ diff --git a/addons/web/static/src/legacy/js/services/local_storage_service.js b/addons/web/static/src/legacy/js/services/local_storage_service.js deleted file mode 100644 index eac0ad2b59c..00000000000 --- a/addons/web/static/src/legacy/js/services/local_storage_service.js +++ /dev/null @@ -1,17 +0,0 @@ -/** @odoo-module **/ - -/** - * This module defines a service to access the localStorage object. - */ - -import AbstractStorageService from "@web/legacy/js/core/abstract_storage_service"; -import core from "@web/legacy/js/services/core"; -import localStorage from "@web/legacy/js/core/local_storage"; - -var LocalStorageService = AbstractStorageService.extend({ - storage: localStorage, -}); - -core.serviceRegistry.add('local_storage', LocalStorageService); - -export default LocalStorageService; diff --git a/addons/web/static/src/legacy/js/services/session_storage_service.js b/addons/web/static/src/legacy/js/services/session_storage_service.js deleted file mode 100644 index 7815232aa93..00000000000 --- a/addons/web/static/src/legacy/js/services/session_storage_service.js +++ /dev/null @@ -1,17 +0,0 @@ -/** @odoo-module **/ - -/** - * This module defines a service to access the sessionStorage object. - */ - -import AbstractStorageService from "@web/legacy/js/core/abstract_storage_service"; -import core from "@web/legacy/js/services/core"; -import sessionStorage from "@web/legacy/js/core/session_storage"; - -var SessionStorageService = AbstractStorageService.extend({ - storage: sessionStorage, -}); - -core.serviceRegistry.add('session_storage', SessionStorageService); - -export default SessionStorageService; diff --git a/addons/web/static/src/legacy/legacy_setup.js b/addons/web/static/src/legacy/legacy_setup.js index 9a4cb52342d..d14c41c0a91 100644 --- a/addons/web/static/src/legacy/legacy_setup.js +++ b/addons/web/static/src/legacy/legacy_setup.js @@ -10,7 +10,6 @@ import { makeLegacyDropdownService, } from "./utils"; import { makeLegacyActionManagerService } from "./backend_utils"; -import AbstractService from "@web/legacy/js/core/abstract_service"; import legacyEnv from "@web/legacy/js/env"; import session from "web.session"; import { templates } from "@web/core/assets"; @@ -25,7 +24,6 @@ export const legacySetupProm = new Promise((resolve) => { // build the legacy env and set it on Component (this was done in main.js, // with the starting of the webclient) (async () => { - AbstractService.prototype.deployServices(legacyEnv); Component.env = legacyEnv; const legacyActionManagerService = makeLegacyActionManagerService(legacyEnv); const serviceRegistry = registry.category("services"); diff --git a/addons/web/static/src/legacy/utils.js b/addons/web/static/src/legacy/utils.js index 4c6e2ada78b..02f7a0e7b6f 100644 --- a/addons/web/static/src/legacy/utils.js +++ b/addons/web/static/src/legacy/utils.js @@ -1,7 +1,6 @@ /** @odoo-module **/ import { browser } from "../core/browser/browser"; -import AbstractStorageService from "@web/legacy/js/core/abstract_storage_service"; import { ConnectionAbortedError, RPCError, @@ -169,14 +168,7 @@ export function mapLegacyEnvToWowlEnv(legacyEnv, wowlEnv) { prom.abort = rejection; return prom; }; - // Storages - function mapStorage(storage) { - const StorageService = AbstractStorageService.extend({ storage }); - return new StorageService(); - } - legacyEnv.services.local_storage = mapStorage(browser.localStorage); - legacyEnv.services.session_storage = mapStorage(browser.sessionStorage); legacyEnv.services.dialog = wowlEnv.services.dialog; // map WebClientReady wowlEnv.bus.addEventListener("WEB_CLIENT_READY", () => { diff --git a/addons/web/static/tests/legacy/core/widget_tests.js b/addons/web/static/tests/legacy/core/widget_tests.js index 512dacbe439..aaed38199de 100644 --- a/addons/web/static/tests/legacy/core/widget_tests.js +++ b/addons/web/static/tests/legacy/core/widget_tests.js @@ -1,12 +1,12 @@ /** @odoo-module **/ -import AjaxService from "@web/legacy/js/services/ajax_service"; import Dialog from "@web/legacy/js/core/dialog"; import Widget from "@web/legacy/js/core/widget"; import session from "web.session"; import { patchWithCleanup } from "@web/../tests/helpers/utils"; import testUtils from "@web/../tests/legacy/helpers/test_utils"; import { renderToString } from "@web/core/utils/render"; +import env from "@web/legacy/js/common_env"; QUnit.module('core', {}, function () { @@ -412,10 +412,9 @@ QUnit.module('core', {}, function () { return def; }, }); - var ajaxService = new AjaxService(); testUtils.mock.intercept(parent, 'call_service', function ({ data }) { if (data.service === "ajax" && data.method === "rpc") { - data.callback(ajaxService.rpc(...data.args)); + data.callback(env.services.ajax.rpc(...data.args)); } }); var widget = new Widget(parent); diff --git a/addons/website_slides/__manifest__.py b/addons/website_slides/__manifest__.py index 928d52959cd..7ead77a1ebc 100644 --- a/addons/website_slides/__manifest__.py +++ b/addons/website_slides/__manifest__.py @@ -200,8 +200,6 @@ Featuring 'web/static/src/legacy/js/libs/pdfjs.js', 'web/static/src/legacy/js/libs/zoomodoo.js', 'web/static/src/legacy/js/libs/jSignatureCustom.js', - 'web/static/src/legacy/js/core/abstract_service.js', - 'web/static/src/legacy/js/core/abstract_storage_service.js', 'web/static/src/legacy/js/core/ajax.js', 'web/static/src/legacy/js/core/bus.js', 'web/static/src/legacy/js/core/class.js', @@ -222,11 +220,8 @@ Featuring 'web/static/src/legacy/js/core/translation.js', 'web/static/src/legacy/js/core/utils.js', 'web/static/src/legacy/js/core/widget.js', - 'web/static/src/legacy/js/services/ajax_service.js', 'web/static/src/legacy/js/services/config.js', 'web/static/src/legacy/js/services/core.js', - 'web/static/src/legacy/js/services/local_storage_service.js', - 'web/static/src/legacy/js/services/session_storage_service.js', 'web/static/src/legacy/js/common_env.js', 'web/static/src/legacy/js/widgets/name_and_signature.js', 'web/static/src/legacy/xml/name_and_signature.xml',