Files
shaka-player/lib/util/event_manager.js
T
Joey Parrish 0e65a4543e Fix EventManager listenOnce with multiple listeners
When listening to the same event on the same object from two places,
it's important that both listeners get called back.

This fixes EventManager's listenOnce() so that the unlisten() call
within listenOnce() doesn't remove both listeners at once.  Now each
listener will be called before it is removed.

This bug is over two years old!

Change-Id: Id99f3a8e5ab80819921b30e28aa66d8a08b29e86
2019-04-30 18:33:26 +00:00

166 lines
4.4 KiB
JavaScript

/**
* @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
goog.provide('shaka.util.EventManager');
goog.require('goog.asserts');
goog.require('shaka.util.IReleasable');
goog.require('shaka.util.MultiMap');
/**
* Creates a new EventManager. An EventManager maintains a collection of "event
* bindings" between event targets and event listeners.
*
* @struct
* @constructor
* @implements {shaka.util.IReleasable}
*/
shaka.util.EventManager = function() {
/**
* Maps an event type to an array of event bindings.
* @private {shaka.util.MultiMap.<!shaka.util.EventManager.Binding_>}
*/
this.bindingMap_ = new shaka.util.MultiMap();
};
/**
* @typedef {function(!Event)}
*/
shaka.util.EventManager.ListenerType;
/**
* Detaches all event listeners.
* @override
*/
shaka.util.EventManager.prototype.release = function() {
this.removeAll();
this.bindingMap_ = null;
};
/**
* Attaches an event listener to an event target.
* @param {EventTarget} target The event target.
* @param {string} type The event type.
* @param {shaka.util.EventManager.ListenerType} listener The event listener.
*/
shaka.util.EventManager.prototype.listen = function(target, type, listener) {
if (!this.bindingMap_) return;
let binding = new shaka.util.EventManager.Binding_(target, type, listener);
this.bindingMap_.push(type, binding);
};
/**
* Attaches an event listener to an event target. The listener will be removed
* when the first instance of the event is fired.
* @param {EventTarget} target The event target.
* @param {string} type The event type.
* @param {shaka.util.EventManager.ListenerType} listener The event listener.
*/
shaka.util.EventManager.prototype.listenOnce =
function(target, type, listener) {
// Install a shim listener that will stop listening after the first event.
const shim = (event) => {
// Stop listening to this event.
this.unlisten(target, type, shim);
// Call the original listener.
listener(event);
};
this.listen(target, type, shim);
};
/**
* Detaches an event listener from an event target.
* @param {EventTarget} target The event target.
* @param {string} type The event type.
* @param {shaka.util.EventManager.ListenerType=} listener The event listener.
*/
shaka.util.EventManager.prototype.unlisten = function(target, type, listener) {
if (!this.bindingMap_) return;
let list = this.bindingMap_.get(type) || [];
for (let i = 0; i < list.length; ++i) {
let binding = list[i];
if (binding.target == target) {
if (listener == binding.listener || !listener) {
binding.unlisten();
this.bindingMap_.remove(type, binding);
}
}
}
};
/**
* Detaches all event listeners from all targets.
*/
shaka.util.EventManager.prototype.removeAll = function() {
if (!this.bindingMap_) return;
let list = this.bindingMap_.getAll();
for (let i = 0; i < list.length; ++i) {
list[i].unlisten();
}
this.bindingMap_.clear();
};
/**
* Creates a new Binding_ and attaches the event listener to the event target.
* @param {EventTarget} target The event target.
* @param {string} type The event type.
* @param {shaka.util.EventManager.ListenerType} listener The event listener.
* @constructor
* @private
*/
shaka.util.EventManager.Binding_ = function(target, type, listener) {
/** @type {EventTarget} */
this.target = target;
/** @type {string} */
this.type = type;
/** @type {?shaka.util.EventManager.ListenerType} */
this.listener = listener;
this.target.addEventListener(type, listener, false);
};
/**
* Detaches the event listener from the event target. This does nothing if the
* event listener is already detached.
*/
shaka.util.EventManager.Binding_.prototype.unlisten = function() {
goog.asserts.assert(this.target, 'Missing target');
this.target.removeEventListener(this.type, this.listener, false);
this.target = null;
this.listener = null;
};