Skip to content
Open
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
26 changes: 15 additions & 11 deletions src/js/mixins/evented.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
},

Expand Down
10 changes: 6 additions & 4 deletions test/unit/mixins/evented.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({});
Expand All @@ -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) {
Expand Down