[FIX] web: allow to unregister multiple listener from the same target

Steps to reproduce
==================

- Register two events listener on the same target
- Unregister them

Only the first one is unregistered

Cause of the issue
==================

`cbs` is used instead of `listeners`. This means that after any `.off`
call, the target is removed from the Map and it is not possible anymore
to unregister any events for it

closes odoo/odoo#118739

X-original-commit: 345dd30fdc627ada212a8eaa6f9817bbdaf5d3a3
Signed-off-by: Samuel Degueldre <sad@odoo.com>
Signed-off-by: Hubert Van De Walle <huvw@odoo.com>
This commit is contained in:
Hubert Van de Walle (huvw)
2023-04-17 13:09:42 +02:00
parent 10086517ba
commit 27c90b95ff
2 changed files with 29 additions and 2 deletions
@@ -33,8 +33,8 @@
for (const callback of cbs) {
this.removeEventListener(type, callback);
}
delete cbs[type];
if (Object.keys(cbs).length === 0) {
delete listeners[type];
if (Object.keys(listeners).length === 0) {
this.targetsCallbacks.delete(target);
}
}
@@ -21,6 +21,7 @@
const {
Component,
EventBus,
onError,
onMounted,
onWillDestroy,
@@ -1677,4 +1678,30 @@
await mount(MyComponent, getFixture(), { env: legacyEnv });
});
QUnit.module("EventBus");
QUnit.test("unregister multiple listener from the same target", async function (assert) {
const target = Symbol("test");
const bus = new EventBus();
let i = 0;
bus.on("a", target, () => assert.step(`a:${i++}`));
bus.on("b", target, () => assert.step(`b:${i++}`));
bus.trigger("a");
bus.trigger("b");
bus.off("a", target);
bus.off("b", target);
bus.trigger("a");
bus.trigger("b");
assert.verifySteps([
"a:0",
"b:1",
], "callback should not be called after unregistering them");
});
});