events.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Licensed to the Software Freedom Conservancy (SFC) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The SFC licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. 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,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. 'use strict';
  18. /**
  19. * Describes an event listener registered on an {@linkplain EventEmitter}.
  20. */
  21. class Listener {
  22. /**
  23. * @param {!Function} fn The actual listener function.
  24. * @param {(Object|undefined)} scope The object in whose scope to invoke the
  25. * listener.
  26. * @param {boolean} oneshot Whether this listener should only be used once.
  27. */
  28. constructor(fn, scope, oneshot) {
  29. this.fn = fn;
  30. this.scope = scope;
  31. this.oneshot = oneshot;
  32. }
  33. }
  34. /** @type {!WeakMap<!EventEmitter, !Map<string, !Set<!Listener>>>} */
  35. const EVENTS = new WeakMap;
  36. /**
  37. * Object that can emit events for others to listen for.
  38. */
  39. class EventEmitter {
  40. /**
  41. * Fires an event and calls all listeners.
  42. * @param {string} type The type of event to emit.
  43. * @param {...*} var_args Any arguments to pass to each listener.
  44. */
  45. emit(type, var_args) {
  46. let events = EVENTS.get(this);
  47. if (!events) {
  48. return;
  49. }
  50. let args = Array.prototype.slice.call(arguments, 1);
  51. let listeners = events.get(type);
  52. if (listeners) {
  53. for (let listener of listeners) {
  54. listener.fn.apply(listener.scope, args);
  55. if (listener.oneshot) {
  56. listeners.delete(listener);
  57. }
  58. }
  59. }
  60. }
  61. /**
  62. * Returns a mutable list of listeners for a specific type of event.
  63. * @param {string} type The type of event to retrieve the listeners for.
  64. * @return {!Set<!Listener>} The registered listeners for the given event
  65. * type.
  66. */
  67. listeners(type) {
  68. let events = EVENTS.get(this);
  69. if (!events) {
  70. events = new Map;
  71. EVENTS.set(this, events);
  72. }
  73. let listeners = events.get(type);
  74. if (!listeners) {
  75. listeners = new Set;
  76. events.set(type, listeners);
  77. }
  78. return listeners;
  79. }
  80. /**
  81. * Registers a listener.
  82. * @param {string} type The type of event to listen for.
  83. * @param {!Function} fn The function to invoke when the event is fired.
  84. * @param {Object=} opt_self The object in whose scope to invoke the listener.
  85. * @param {boolean=} opt_oneshot Whether the listener should b (e removed after
  86. * the first event is fired.
  87. * @return {!EventEmitter} A self reference.
  88. * @private
  89. */
  90. addListener_(type, fn, opt_self, opt_oneshot) {
  91. let listeners = this.listeners(type);
  92. for (let listener of listeners) {
  93. if (listener.fn === fn) {
  94. return this;
  95. }
  96. }
  97. listeners.add(new Listener(fn, opt_self || undefined, !!opt_oneshot));
  98. return this;
  99. }
  100. /**
  101. * Registers a listener.
  102. * @param {string} type The type of event to listen for.
  103. * @param {!Function} fn The function to invoke when the event is fired.
  104. * @param {Object=} opt_self The object in whose scope to invoke the listener.
  105. * @return {!EventEmitter} A self reference.
  106. */
  107. addListener(type, fn, opt_self) {
  108. return this.addListener_(type, fn, opt_self, false);
  109. }
  110. /**
  111. * Registers a one-time listener which will be called only the first time an
  112. * event is emitted, after which it will be removed.
  113. * @param {string} type The type of event to listen for.
  114. * @param {!Function} fn The function to invoke when the event is fired.
  115. * @param {Object=} opt_self The object in whose scope to invoke the listener.
  116. * @return {!EventEmitter} A self reference.
  117. */
  118. once(type, fn, opt_self) {
  119. return this.addListener_(type, fn, opt_self, true);
  120. }
  121. /**
  122. * An alias for {@link #addListener() addListener()}.
  123. * @param {string} type The type of event to listen for.
  124. * @param {!Function} fn The function to invoke when the event is fired.
  125. * @param {Object=} opt_self The object in whose scope to invoke the listener.
  126. * @return {!EventEmitter} A self reference.
  127. */
  128. on(type, fn, opt_self) {
  129. return this.addListener(type, fn, opt_self);
  130. }
  131. /**
  132. * Removes a previously registered event listener.
  133. * @param {string} type The type of event to unregister.
  134. * @param {!Function} listenerFn The handler function to remove.
  135. * @return {!EventEmitter} A self reference.
  136. */
  137. removeListener(type, listenerFn) {
  138. if (typeof type !== 'string' || typeof listenerFn !== 'function') {
  139. throw TypeError('invalid args: expected (string, function), got ('
  140. + (typeof type) + ', ' + (typeof listenerFn) + ')');
  141. }
  142. let events = EVENTS.get(this);
  143. if (!events) {
  144. return this;
  145. }
  146. let listeners = events.get(type);
  147. if (!listeners) {
  148. return this;
  149. }
  150. let match;
  151. for (let listener of listeners) {
  152. if (listener.fn === listenerFn) {
  153. match = listener;
  154. break;
  155. }
  156. }
  157. if (match) {
  158. listeners.delete(match);
  159. if (!listeners.size) {
  160. events.delete(type);
  161. }
  162. }
  163. return this;
  164. }
  165. /**
  166. * Removes all listeners for a specific type of event. If no event is
  167. * specified, all listeners across all types will be removed.
  168. * @param {string=} opt_type The type of event to remove listeners from.
  169. * @return {!EventEmitter} A self reference.
  170. */
  171. removeAllListeners(opt_type) {
  172. let events = EVENTS.get(this);
  173. if (events) {
  174. if (typeof opt_type === 'string') {
  175. events.delete(opt_type);
  176. } else {
  177. EVENTS.delete(this);
  178. }
  179. }
  180. return this;
  181. }
  182. }
  183. // PUBLIC API
  184. module.exports = {
  185. EventEmitter: EventEmitter,
  186. Listener: Listener
  187. };