From ee96f741a5485c796c209e5e3d685ecd3d2d97e9 Mon Sep 17 00:00:00 2001 From: Deepak kudi Date: Sat, 23 May 2026 11:01:01 +0530 Subject: [PATCH] Fix one() for array events on target objects Signed-off-by: Deepak kudi --- src/js/mixins/evented.js | 26 +++++++++++++++----------- test/unit/mixins/evented.test.js | 10 ++++++---- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/js/mixins/evented.js b/src/js/mixins/evented.js index 0d6bea5cca..228739f421 100644 --- a/src/js/mixins/evented.js +++ b/src/js/mixins/evented.js @@ -318,19 +318,23 @@ const EventedMixin = { // Targeting another evented object. } else { - // TODO: This wrapper is incorrect! It should only - // remove the wrapper for the event type that called it. - // Instead all listeners are removed on the first trigger! - // see https://github.com/videojs/video.js/issues/5962 - const wrapper = (...largs) => { - this.off(target, type, wrapper); - listener.apply(null, largs); + const listenOnce = (typeName) => { + const wrapper = (...largs) => { + this.off(target, typeName, wrapper); + listener.apply(null, largs); + }; + + // Use the same function ID as the listener so we can remove it later + // it using the ID of the original listener. + wrapper.guid = listener.guid; + listen(target, 'one', typeName, wrapper); }; - // Use the same function ID as the listener so we can remove it later - // it using the ID of the original listener. - wrapper.guid = listener.guid; - listen(target, 'one', type, wrapper); + if (Array.isArray(type)) { + type.forEach(listenOnce); + } else { + listenOnce(type); + } } }, diff --git a/test/unit/mixins/evented.test.js b/test/unit/mixins/evented.test.js index d66d40bbdc..dccba7c842 100644 --- a/test/unit/mixins/evented.test.js +++ b/test/unit/mixins/evented.test.js @@ -332,9 +332,6 @@ QUnit.test('one() can add a listener to one event type on a different target obj }); }); -// TODO: This test is incorrect! this listener should be called twice, -// but instead all listeners are removed on the first trigger! -// see https://github.com/videojs/video.js/issues/5962 QUnit.test('one() can add a listener to an array of event types on a different target object', function(assert) { const a = this.targets.a = evented({}); const b = this.targets.b = evented({}); @@ -350,12 +347,17 @@ QUnit.test('one() can add a listener to an array of event types on a different t a.trigger('x'); a.trigger('y'); - assert.strictEqual(spy.callCount, 1, 'the listener was called the expected number of times'); + assert.strictEqual(spy.callCount, 2, 'the listener was called the expected number of times'); validateListenerCall(spy.getCall(0), a, { type: 'x', target: b.eventBusEl_ }); + + validateListenerCall(spy.getCall(1), a, { + type: 'y', + target: b.eventBusEl_ + }); }); QUnit.test('any() can add a listener to one event type on a different target object', function(assert) {