summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/core
diff options
context:
space:
mode:
authorJulien Mougenot <jum@odoo.com>2020-07-13 13:03:23 +0000
committerMathieu Duckerts-Antoine <dam@odoo.com>2020-07-23 08:21:09 +0000
commit5698d1fab9cf36714f677ae6942201d5c9354798 (patch)
treef6f9c7e70f34adb4a3c375f42700804702aa664c /addons/web/static/src/js/core
parentea1741bc743b2066e871b526fc0d7cbf30b08fef (diff)
[IMP] web: add Registry predicate
Before this commit, any value could be added to registries and the class needed to be overridden to assert that added values would answer to a certain predicate. Now, the registry can be instantiated with a given predicate function that all added value must pass to be registered (default: all values are accepted). Existing implentations have been adapted: - FieldRegistry - FieldRegistryOwl.
Diffstat (limited to 'addons/web/static/src/js/core')
-rw-r--r--addons/web/static/src/js/core/registry.js13
1 files changed, 12 insertions, 1 deletions
diff --git a/addons/web/static/src/js/core/registry.js b/addons/web/static/src/js/core/registry.js
index 7b2c69db180..1ab16236e59 100644
--- a/addons/web/static/src/js/core/registry.js
+++ b/addons/web/static/src/js/core/registry.js
@@ -20,13 +20,21 @@ odoo.define("web.Registry", function (require) {
*/
class Registry {
/**
+ * @function predicate
+ * @param {any} value
+ * @returns {boolean}
+ */
+ /**
* @param {Object} [mapping] the initial data in the registry
+ * @param {predicate} [predicate=(() => true)] predicate that each
+ * added value must pass to be registered.
*/
- constructor(mapping) {
+ constructor(mapping, predicate = () => true) {
this.map = Object.create(mapping || null);
this._scoreMapping = Object.create(null);
this._sortedKeys = null;
this.listeners = []; // listening callbacks on newly added items.
+ this.predicate = predicate;
}
//--------------------------------------------------------------------------
@@ -42,6 +50,9 @@ odoo.define("web.Registry", function (require) {
* @returns {Registry} can be used to chain add calls.
*/
add(key, value, score) {
+ if (!this.predicate(value)) {
+ throw new Error(`Value of key "${key}" does not pass the addition predicate.`);
+ }
this._scoreMapping[key] = score === undefined ? key : score;
this._sortedKeys = null;
this.map[key] = value;