summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/core
diff options
context:
space:
mode:
authorJulien Mougenot <jum@odoo.com>2020-07-13 13:01:18 +0000
committerMathieu Duckerts-Antoine <dam@odoo.com>2020-07-23 08:21:09 +0000
commitea1741bc743b2066e871b526fc0d7cbf30b08fef (patch)
tree497b99e5bf28e0d80c4d8b41fe28f2def5da91cc /addons/web/static/src/js/core
parent20caa733c656c6f76cd075829e7854b2b2a22bf9 (diff)
[REF] web: Registry goes ESNext
Converted the Registry class to an ES6 class with the new framework standards.
Diffstat (limited to 'addons/web/static/src/js/core')
-rw-r--r--addons/web/static/src/js/core/registry.js262
1 files changed, 129 insertions, 133 deletions
diff --git a/addons/web/static/src/js/core/registry.js b/addons/web/static/src/js/core/registry.js
index 6089ccc827e..7b2c69db180 100644
--- a/addons/web/static/src/js/core/registry.js
+++ b/addons/web/static/src/js/core/registry.js
@@ -1,147 +1,143 @@
-odoo.define('web.Registry', function (require) {
-"use strict";
+odoo.define("web.Registry", function (require) {
+ "use strict";
-var Class = require('web.Class');
+ const { sortBy } = require("web.utils");
-/**
- * The registry is really pretty much only a mapping from some keys to some
- * values. The Registry class only add a few simple methods around that to make
- * it nicer and slightly safer.
- *
- * Note that registries have a fundamental problem: the value that you try to
- * get in a registry might not have been added yet, so of course, you need to
- * make sure that your dependencies are solid. For this reason, it is a good
- * practice to avoid using the registry if you can simply import what you need
- * with the 'require' statement.
- *
- * However, on the flip side, sometimes you cannot just simply import something
- * because we would have a dependency cycle. In that case, registries might
- * help.
- */
-var Registry = Class.extend({
/**
- * @constructor
- * @param {Object} [mapping] the initial data in the registry
- */
- init: function (mapping) {
- this.map = Object.create(mapping || null);
- this._scoreMapping = Object.create(null);
- this._sortedKeys = null;
- this.listeners = []; // listening callbacks on newly added items.
- },
-
- //--------------------------------------------------------------------------
- // Public
- //--------------------------------------------------------------------------
-
- /**
- * Add a key (and a value) to the registry.
+ * The registry is really pretty much only a mapping from some keys to some
+ * values. The Registry class only add a few simple methods around that to make
+ * it nicer and slightly safer.
*
- * Notify the listeners on newly added item in the registry.
+ * Note that registries have a fundamental problem: the value that you try to
+ * get in a registry might not have been added yet, so of course, you need to
+ * make sure that your dependencies are solid. For this reason, it is a good
+ * practice to avoid using the registry if you can simply import what you need
+ * with the 'require' statement.
*
- * @param {string} key
- * @param {any} value
- * @param {number} [score] if given, this value will be used to order keys
- * @returns {Registry} can be used to chain add calls.
+ * However, on the flip side, sometimes you cannot just simply import something
+ * because we would have a dependency cycle. In that case, registries might
+ * help.
*/
- add: function (key, value, score) {
- this._scoreMapping[key] = score === undefined ? key : score;
- this._sortedKeys = null;
- this.map[key] = value;
- _.each(this.listeners, function (callback) {
- callback(key, value);
- });
- return this;
- },
- /**
- * Check if the registry contains the key
- *
- * @param {string} key
- * @returns {boolean}
- */
- contains: function (key) {
- return (key in this.map);
- },
- /**
- * Returns the content of the registry (an object mapping keys to values)
- *
- * @returns {Object}
- */
- entries: function () {
- var entries = {};
- var keys = this.keys();
- for (var i = 0; i < keys.length; i++) {
- entries[keys[i]] = this.map[keys[i]];
+ class Registry {
+ /**
+ * @param {Object} [mapping] the initial data in the registry
+ */
+ constructor(mapping) {
+ this.map = Object.create(mapping || null);
+ this._scoreMapping = Object.create(null);
+ this._sortedKeys = null;
+ this.listeners = []; // listening callbacks on newly added items.
}
- return entries;
- },
- /**
- * Returns the value associated to the given key.
- *
- * @param {string} key
- * @returns {any}
- */
- get: function (key) {
- return this.map[key];
- },
- /**
- * Tries a number of keys, and returns the first object matching one of
- * the keys.
- *
- * @param {string[]} keys a sequence of keys to fetch the object for
- * @returns {any} the first result found matching an object
- */
- getAny: function (keys) {
- for (var i=0; i<keys.length; i++) {
- if (keys[i] in this.map) {
- return this.map[keys[i]];
+
+ //--------------------------------------------------------------------------
+ // Public
+ //--------------------------------------------------------------------------
+
+ /**
+ * Add a key (and a value) to the registry.
+ * Notify the listeners on newly added item in the registry.
+ * @param {string} key
+ * @param {any} value
+ * @param {number} [score] if given, this value will be used to order keys
+ * @returns {Registry} can be used to chain add calls.
+ */
+ add(key, value, score) {
+ this._scoreMapping[key] = score === undefined ? key : score;
+ this._sortedKeys = null;
+ this.map[key] = value;
+ for (const callback of this.listeners) {
+ callback(key, value);
}
+ return this;
}
- return null;
- },
- /**
- * Return the list of keys in map object.
- *
- * The registry guarantees that the keys have a consistent order, defined by
- * the 'score' value when the item has been added.
- *
- * @returns {string[]}
- */
- keys: function () {
- var self = this;
- if (!this._sortedKeys) {
- var keys = [];
- for (var key in this.map) {
- keys.push(key);
+
+ /**
+ * Check if the registry contains the key
+ * @param {string} key
+ * @returns {boolean}
+ */
+ contains(key) {
+ return key in this.map;
+ }
+
+ /**
+ * Returns the content of the registry (an object mapping keys to values)
+ * @returns {Object}
+ */
+ entries() {
+ const entries = {};
+ const keys = this.keys();
+ for (const key of keys) {
+ entries[key] = this.map[key];
}
- this._sortedKeys = _.sortBy(keys, function (key) {
- return self._scoreMapping[key] || 0;
- });
+ return entries;
+ }
+
+ /**
+ * Returns the value associated to the given key.
+ * @param {string} key
+ * @returns {any}
+ */
+ get(key) {
+ return this.map[key];
}
- return this._sortedKeys;
- },
- /**
- * Register a callback to execute when items are added to the registry.
- *
- * @param {function} callback function with parameters (key, value).
- */
- onAdd: function (callback) {
- this.listeners.push(callback);
- },
- /**
- * Return the list of values in map object
- *
- * @returns {string[]}
- */
- values: function () {
- var self = this;
- return this.keys().map(function (key) {
- return self.map[key];
- });
- },
-});
-return Registry;
+ /**
+ * Tries a number of keys, and returns the first object matching one of
+ * the keys.
+ * @param {string[]} keys a sequence of keys to fetch the object for
+ * @returns {any} the first result found matching an object
+ */
+ getAny(keys) {
+ for (const key of keys) {
+ if (key in this.map) {
+ return this.map[key];
+ }
+ }
+ return null;
+ }
-});
+ /**
+ * Return the list of keys in map object.
+ *
+ * The registry guarantees that the keys have a consistent order, defined by
+ * the 'score' value when the item has been added.
+ * @returns {string[]}
+ */
+ keys() {
+ if (!this._sortedKeys) {
+ const keys = [];
+ for (const key in this.map) {
+ keys.push(key);
+ }
+ this._sortedKeys = sortBy(keys,
+ key => this._scoreMapping[key] || 0
+ );
+ }
+ return this._sortedKeys;
+ }
+ /**
+ * @function onAddCallback
+ * @param {string} key
+ * @param {any} value
+ */
+ /**
+ * Register a callback to execute when items are added to the registry.
+ * @param {onAddCallback} callback function with parameters (key, value).
+ */
+ onAdd(callback) {
+ this.listeners.push(callback);
+ }
+
+ /**
+ * Return the list of values in map object
+ * @returns {string[]}
+ */
+ values() {
+ return this.keys().map((key) => this.map[key]);
+ }
+ }
+
+ return Registry;
+});