Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions addons/web/static/src/core/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,12 @@ export class Registry extends EventBus {
* @param {string} key
* @returns {any}
*/
get(key) {
if (!(key in this.content)) {
get(key, defaultValue) {
if (arguments.length < 2 && !(key in this.content)) {
throw new KeyNotFoundError(`Cannot find ${key} in this registry!`);
}
return this.content[key][1];
const info = this.content[key];
return info ? info[1] : defaultValue;
}

/**
Expand Down
11 changes: 2 additions & 9 deletions addons/web/static/src/webclient/actions/action_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -946,15 +946,8 @@ function makeActionManager(env) {
case "ir.actions.server":
return _executeServerAction(action, options);
default: {
let handler;
try {
handler = actionHandlersRegistry.get(action.type);
} catch (e) {
if (!(e instanceof KeyNotFoundError)) {
throw e;
}
}
if (handler) {
let handler = actionHandlersRegistry.get(action.type, null);
if (handler !== null) {
return handler({ env, action, options });
}
throw new Error(
Expand Down
40 changes: 40 additions & 0 deletions addons/web/static/tests/core/registry_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,46 @@ QUnit.test("key set and get", function (assert) {
assert.strictEqual(registry.get("foo"), foo);
});

QUnit.test("can set and get falsy values", function (assert) {
const registry = new Registry();
registry.add("foo1", false);
registry.add("foo2", 0);
registry.add("foo3", "");
registry.add("foo4", undefined);
registry.add("foo5", null);
assert.strictEqual(registry.get("foo1"), false);
assert.strictEqual(registry.get("foo2"), 0);
assert.strictEqual(registry.get("foo3"), "");
assert.strictEqual(registry.get("foo4"), undefined);
assert.strictEqual(registry.get("foo5"), null);
});

QUnit.test("can set and get falsy values with default value", function (assert) {
const registry = new Registry();
registry.add("foo1", false);
registry.add("foo2", 0);
registry.add("foo3", "");
registry.add("foo4", undefined);
registry.add("foo5", null);
assert.strictEqual(registry.get("foo1", 1), false);
assert.strictEqual(registry.get("foo2", 1), 0);
assert.strictEqual(registry.get("foo3", 1), "");
assert.strictEqual(registry.get("foo4", 1), undefined);
assert.strictEqual(registry.get("foo5", 1), null);
});

QUnit.test("can get a default value when missing key", function (assert) {
const registry = new Registry();
assert.strictEqual(registry.get("missing", "default"), "default");
assert.strictEqual(registry.get("missing", null), null);
assert.strictEqual(registry.get("missing", false), false);
});

QUnit.test("throws if key is missing", function (assert) {
const registry = new Registry();
assert.throws(() => registry.get("missing"));
});

QUnit.test("contains method", function (assert) {
const registry = new Registry();

Expand Down