mirror of
https://github.com/shaka-project/shaka-player.git
synced 2026-06-16 16:16:40 +03:00
f130dffcef
This is a fully automated change. The linter will fail because the extra indentation caused line-length errors. These won't be fixed automatically. They are fixed in a follow-up to make this one fully automated. Change-Id: I4d8cf9c998985add2bcd24a81c8d65495668c4f3
172 lines
4.5 KiB
JavaScript
172 lines
4.5 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;
|
|
}
|
|
|
|
const 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;
|
|
}
|
|
|
|
const list = this.bindingMap_.get(type) || [];
|
|
|
|
for (let i = 0; i < list.length; ++i) {
|
|
const 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;
|
|
}
|
|
|
|
const 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;
|
|
};
|
|
|