Source: lib/util/event_manager.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.util.EventManager');
  18. goog.require('shaka.util.IDestroyable');
  19. goog.require('shaka.util.MultiMap');
  20. /**
  21. * Creates a new EventManager. An EventManager maintains a collection of "event
  22. * bindings" between event targets and event listeners.
  23. *
  24. * @struct
  25. * @constructor
  26. * @implements {shaka.util.IDestroyable}
  27. */
  28. shaka.util.EventManager = function() {
  29. /**
  30. * Maps an event type to an array of event bindings.
  31. * @private {shaka.util.MultiMap.<!shaka.util.EventManager.Binding_>}
  32. */
  33. this.bindingMap_ = new shaka.util.MultiMap();
  34. };
  35. /**
  36. * @typedef {function(!Event)}
  37. */
  38. shaka.util.EventManager.ListenerType;
  39. /**
  40. * Detaches all event listeners.
  41. * @override
  42. */
  43. shaka.util.EventManager.prototype.destroy = function() {
  44. this.removeAll();
  45. this.bindingMap_ = null;
  46. return Promise.resolve();
  47. };
  48. /**
  49. * Attaches an event listener to an event target.
  50. * @param {EventTarget} target The event target.
  51. * @param {string} type The event type.
  52. * @param {shaka.util.EventManager.ListenerType} listener The event listener.
  53. */
  54. shaka.util.EventManager.prototype.listen = function(target, type, listener) {
  55. var binding = new shaka.util.EventManager.Binding_(target, type, listener);
  56. this.bindingMap_.push(type, binding);
  57. };
  58. /**
  59. * Detaches an event listener from an event target.
  60. * @param {EventTarget} target The event target.
  61. * @param {string} type The event type.
  62. */
  63. shaka.util.EventManager.prototype.unlisten = function(target, type) {
  64. var list = this.bindingMap_.get(type) || [];
  65. for (var i = 0; i < list.length; ++i) {
  66. var binding = list[i];
  67. if (binding.target == target) {
  68. binding.unlisten();
  69. this.bindingMap_.remove(type, binding);
  70. }
  71. }
  72. };
  73. /**
  74. * Detaches all event listeners from all targets.
  75. */
  76. shaka.util.EventManager.prototype.removeAll = function() {
  77. var list = this.bindingMap_.getAll();
  78. for (var i = 0; i < list.length; ++i) {
  79. list[i].unlisten();
  80. }
  81. this.bindingMap_.clear();
  82. };
  83. /**
  84. * Creates a new Binding_ and attaches the event listener to the event target.
  85. * @param {EventTarget} target The event target.
  86. * @param {string} type The event type.
  87. * @param {shaka.util.EventManager.ListenerType} listener The event listener.
  88. * @constructor
  89. * @private
  90. */
  91. shaka.util.EventManager.Binding_ = function(target, type, listener) {
  92. /** @type {EventTarget} */
  93. this.target = target;
  94. /** @type {string} */
  95. this.type = type;
  96. /** @type {?shaka.util.EventManager.ListenerType} */
  97. this.listener = listener;
  98. this.target.addEventListener(type, listener, false);
  99. };
  100. /**
  101. * Detaches the event listener from the event target. This does nothing if the
  102. * event listener is already detached.
  103. */
  104. shaka.util.EventManager.Binding_.prototype.unlisten = function() {
  105. if (!this.target)
  106. return;
  107. this.target.removeEventListener(this.type, this.listener, false);
  108. this.target = null;
  109. this.listener = null;
  110. };