[REF] web, *: remove legacy registry
*: website, website_slides Part of task-id 3439226 closes odoo/odoo#133132 Related: odoo/enterprise#46272 Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
This commit is contained in:
@@ -112,7 +112,6 @@
|
||||
'web/static/src/legacy/xml/dialog.xml',
|
||||
'web/static/src/legacy/js/core/dom.js',
|
||||
'web/static/src/legacy/js/core/mixins.js',
|
||||
'web/static/src/legacy/js/core/registry.js',
|
||||
'web/static/src/legacy/js/core/rpc.js',
|
||||
'web/static/src/legacy/js/core/service_mixins.js',
|
||||
'web/static/src/legacy/js/core/session.js',
|
||||
|
||||
@@ -163,7 +163,6 @@
|
||||
'web/static/src/legacy/xml/dialog.xml',
|
||||
'web/static/src/legacy/js/core/dom.js',
|
||||
'web/static/src/legacy/js/core/mixins.js',
|
||||
'web/static/src/legacy/js/core/registry.js',
|
||||
'web/static/src/legacy/js/core/rpc.js',
|
||||
'web/static/src/legacy/js/core/service_mixins.js',
|
||||
'web/static/src/legacy/js/core/session.js',
|
||||
|
||||
@@ -112,7 +112,6 @@ This module provides the core of the Odoo Web Client.
|
||||
'web/static/src/legacy/xml/dialog.xml',
|
||||
'web/static/src/legacy/js/core/dom.js',
|
||||
'web/static/src/legacy/js/core/mixins.js',
|
||||
'web/static/src/legacy/js/core/registry.js',
|
||||
'web/static/src/legacy/js/core/rpc.js',
|
||||
'web/static/src/legacy/js/core/service_mixins.js',
|
||||
'web/static/src/legacy/js/core/session.js',
|
||||
@@ -272,7 +271,6 @@ This module provides the core of the Odoo Web Client.
|
||||
'web/static/src/legacy/xml/dialog.xml',
|
||||
'web/static/src/legacy/js/core/dom.js',
|
||||
'web/static/src/legacy/js/core/mixins.js',
|
||||
'web/static/src/legacy/js/core/registry.js',
|
||||
'web/static/src/legacy/js/core/rpc.js',
|
||||
'web/static/src/legacy/js/core/service_mixins.js',
|
||||
'web/static/src/legacy/js/core/session.js',
|
||||
|
||||
@@ -1,153 +0,0 @@
|
||||
/** @odoo-module **/
|
||||
|
||||
|
||||
import { sortBy } from "@web/legacy/js/core/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.
|
||||
*/
|
||||
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, 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;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// 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) {
|
||||
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;
|
||||
for (const callback of this.listeners) {
|
||||
callback(key, value);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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];
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value associated to the given key.
|
||||
* @param {string} key
|
||||
* @returns {any}
|
||||
*/
|
||||
get(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(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]);
|
||||
}
|
||||
}
|
||||
|
||||
export default Registry;
|
||||
@@ -1,87 +0,0 @@
|
||||
/** @odoo-module **/
|
||||
|
||||
import Registry from "@web/legacy/js/core/registry";
|
||||
|
||||
QUnit.module('core', {}, function () {
|
||||
|
||||
QUnit.module('Registry');
|
||||
|
||||
QUnit.test('key set', function (assert) {
|
||||
assert.expect(1);
|
||||
|
||||
var registry = new Registry();
|
||||
var foo = {};
|
||||
|
||||
registry
|
||||
.add('foo', foo);
|
||||
|
||||
assert.strictEqual(registry.get('foo'), foo);
|
||||
});
|
||||
|
||||
QUnit.test('get initial keys', function (assert) {
|
||||
assert.expect(1);
|
||||
|
||||
var registry = new Registry({ a: 1, });
|
||||
assert.deepEqual(
|
||||
registry.keys(),
|
||||
['a'],
|
||||
"keys on prototype should be returned"
|
||||
);
|
||||
});
|
||||
|
||||
QUnit.test('get initial entries', function (assert) {
|
||||
assert.expect(1);
|
||||
|
||||
var registry = new Registry({ a: 1, });
|
||||
assert.deepEqual(
|
||||
registry.entries(),
|
||||
{ a: 1, },
|
||||
"entries on prototype should be returned"
|
||||
);
|
||||
});
|
||||
|
||||
QUnit.test('multiget', function (assert) {
|
||||
assert.expect(1);
|
||||
|
||||
var foo = {};
|
||||
var bar = {};
|
||||
var registry = new Registry({
|
||||
foo: foo,
|
||||
bar: bar,
|
||||
});
|
||||
assert.strictEqual(
|
||||
registry.getAny(['qux', 'grault', 'bar', 'foo']),
|
||||
bar,
|
||||
"Registry getAny should find first defined key");
|
||||
});
|
||||
|
||||
QUnit.test('keys and values are properly ordered', function (assert) {
|
||||
assert.expect(2);
|
||||
|
||||
var registry = new Registry();
|
||||
|
||||
registry
|
||||
.add('fred', 'foo', 3)
|
||||
.add('george', 'bar', 2)
|
||||
.add('ronald', 'qux', 4);
|
||||
|
||||
assert.deepEqual(registry.keys(), ['george', 'fred', 'ronald']);
|
||||
assert.deepEqual(registry.values(), ['bar', 'foo', 'qux']);
|
||||
});
|
||||
|
||||
QUnit.test("predicate prevents invalid values", function (assert) {
|
||||
assert.expect(5);
|
||||
|
||||
const predicate = value => typeof value === "number";
|
||||
const registry = new Registry(null, predicate);
|
||||
registry.onAdd((key) => assert.step(key));
|
||||
|
||||
assert.ok(registry.add("age", 23));
|
||||
assert.throws(
|
||||
() => registry.add("name", "Fred"),
|
||||
new Error(`Value of key "name" does not pass the addition predicate.`)
|
||||
);
|
||||
assert.deepEqual(registry.entries(), { age: 23 });
|
||||
assert.verifySteps(["age"]);
|
||||
});
|
||||
});
|
||||
@@ -102,7 +102,6 @@ Odoo Web Editor widget.
|
||||
'web/static/src/legacy/xml/dialog.xml',
|
||||
'web/static/src/legacy/js/core/dom.js',
|
||||
'web/static/src/legacy/js/core/mixins.js',
|
||||
'web/static/src/legacy/js/core/registry.js',
|
||||
'web/static/src/legacy/js/core/rpc.js',
|
||||
'web/static/src/legacy/js/core/service_mixins.js',
|
||||
'web/static/src/legacy/js/core/session.js',
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/** @odoo-module **/
|
||||
|
||||
import Registry from "@web/legacy/js/core/registry";
|
||||
import { Registry } from "@web/core/registry";
|
||||
|
||||
export default new Registry();
|
||||
|
||||
@@ -205,7 +205,6 @@ Featuring
|
||||
'web/static/src/legacy/xml/dialog.xml',
|
||||
'web/static/src/legacy/js/core/dom.js',
|
||||
'web/static/src/legacy/js/core/mixins.js',
|
||||
'web/static/src/legacy/js/core/registry.js',
|
||||
'web/static/src/legacy/js/core/rpc.js',
|
||||
'web/static/src/legacy/js/core/service_mixins.js',
|
||||
'web/static/src/legacy/js/core/session.js',
|
||||
|
||||
Reference in New Issue
Block a user