Skip to content

Commit

Permalink
feat: Add a way to listen to all events on a specific target
Browse files Browse the repository at this point in the history
Change-Id: Ia7874430692edd289129dd38a1f59feee9fdc44b
  • Loading branch information
ismena committed Apr 13, 2021
1 parent 66d28b9 commit dc5d033
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion lib/util/fake_event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ shaka.util.FakeEventTarget = class {
this.listeners_.push(type, listener);
}

/**
* Add an event listener to this object that is invoked for all events types
* the object fires.
*
* @param {shaka.util.FakeEventTarget.ListenerType} listener The callback or
* listener object to invoke.
* @exportInterface
*/
listenToAllEvents(listener) {
this.addEventListener(shaka.util.FakeEventTarget.ALL_EVENTS_, listener);
}

/**
* Remove an event listener from this object.
*
Expand Down Expand Up @@ -78,7 +90,12 @@ shaka.util.FakeEventTarget = class {
goog.asserts.assert(event instanceof shaka.util.FakeEvent,
'FakeEventTarget can only dispatch FakeEvents!');

const listeners = this.listeners_.get(event.type) || [];
let listeners = this.listeners_.get(event.type) || [];
const universalListeners =
this.listeners_.get(shaka.util.FakeEventTarget.ALL_EVENTS_);
if (universalListeners) {
listeners = listeners.concat(universalListeners);
}

// Execute this event on listeners until the event has been stopped or we
// run out of listeners.
Expand Down Expand Up @@ -120,3 +137,10 @@ shaka.util.FakeEventTarget = class {
* @exportInterface
*/
shaka.util.FakeEventTarget.ListenerType;


/**
* @const {string}
* @private
*/
shaka.util.FakeEventTarget.ALL_EVENTS_ = 'All';

0 comments on commit dc5d033

Please sign in to comment.