|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +const EmitterSubscription = require('./EmitterSubscription'); |
| 11 | +const EventSubscriptionVendor = require('./EventSubscriptionVendor'); |
| 12 | +const invariant = require('invariant'); |
| 13 | + |
| 14 | +/** |
| 15 | + * @class EventEmitter |
| 16 | + * @description |
| 17 | + * An EventEmitter is responsible for managing a set of listeners and publishing |
| 18 | + * events to them when it is told that such events happened. In addition to the |
| 19 | + * data for the given event it also sends a event control object which allows |
| 20 | + * the listeners/handlers to prevent the default behavior of the given event. |
| 21 | + * |
| 22 | + * The emitter is designed to be generic enough to support all the different |
| 23 | + * contexts in which one might want to emit events. It is a simple multicast |
| 24 | + * mechanism on top of which extra functionality can be composed. For example, a |
| 25 | + * more advanced emitter may use an EventHolder and EventFactory. |
| 26 | + */ |
| 27 | +class EventEmitter { |
| 28 | + /** |
| 29 | + * @constructor |
| 30 | + * |
| 31 | + * @param {EventSubscriptionVendor} subscriber - Optional subscriber instance |
| 32 | + * to use. If omitted, a new subscriber will be created for the emitter. |
| 33 | + */ |
| 34 | + constructor(subscriber) { |
| 35 | + this._subscriber = subscriber || new EventSubscriptionVendor(); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Adds a listener to be invoked when events of the specified type are |
| 40 | + * emitted. An optional calling context may be provided. The data arguments |
| 41 | + * emitted will be passed to the listener function. |
| 42 | + * |
| 43 | + * TODO: Annotate the listener arg's type. This is tricky because listeners |
| 44 | + * can be invoked with varargs. |
| 45 | + * |
| 46 | + * @param {string} eventType - Name of the event to listen to |
| 47 | + * @param {function} listener - Function to invoke when the specified event is |
| 48 | + * emitted |
| 49 | + * @param {*} context - Optional context object to use when invoking the |
| 50 | + * listener |
| 51 | + */ |
| 52 | + addListener(eventType, listener, context) { |
| 53 | + |
| 54 | + return (this._subscriber.addSubscription( |
| 55 | + eventType, |
| 56 | + new EmitterSubscription(this, this._subscriber, listener, context) |
| 57 | + )); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Similar to addListener, except that the listener is removed after it is |
| 62 | + * invoked once. |
| 63 | + * |
| 64 | + * @param {string} eventType - Name of the event to listen to |
| 65 | + * @param {function} listener - Function to invoke only once when the |
| 66 | + * specified event is emitted |
| 67 | + * @param {*} context - Optional context object to use when invoking the |
| 68 | + * listener |
| 69 | + */ |
| 70 | + once(eventType, listener, context) { |
| 71 | + return this.addListener(eventType, (...args) => { |
| 72 | + this.removeCurrentListener(); |
| 73 | + listener.apply(context, args); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Removes all of the registered listeners, including those registered as |
| 79 | + * listener maps. |
| 80 | + * |
| 81 | + * @param {?string} eventType - Optional name of the event whose registered |
| 82 | + * listeners to remove |
| 83 | + */ |
| 84 | + removeAllListeners(eventType) { |
| 85 | + this._subscriber.removeAllSubscriptions(eventType); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Provides an API that can be called during an eventing cycle to remove the |
| 90 | + * last listener that was invoked. This allows a developer to provide an event |
| 91 | + * object that can remove the listener (or listener map) during the |
| 92 | + * invocation. |
| 93 | + * |
| 94 | + * If it is called when not inside of an emitting cycle it will throw. |
| 95 | + * |
| 96 | + * @throws {Error} When called not during an eventing cycle |
| 97 | + * |
| 98 | + * @example |
| 99 | + * var subscription = emitter.addListenerMap({ |
| 100 | + * someEvent: function(data, event) { |
| 101 | + * console.log(data); |
| 102 | + * emitter.removeCurrentListener(); |
| 103 | + * } |
| 104 | + * }); |
| 105 | + * |
| 106 | + * emitter.emit('someEvent', 'abc'); // logs 'abc' |
| 107 | + * emitter.emit('someEvent', 'def'); // does not log anything |
| 108 | + */ |
| 109 | + removeCurrentListener() { |
| 110 | + invariant( |
| 111 | + !!this._currentSubscription, |
| 112 | + 'Not in an emitting cycle; there is no current subscription' |
| 113 | + ); |
| 114 | + this.removeSubscription(this._currentSubscription); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Removes a specific subscription. Called by the `remove()` method of the |
| 119 | + * subscription itself to ensure any necessary cleanup is performed. |
| 120 | + */ |
| 121 | + removeSubscription(subscription) { |
| 122 | + invariant( |
| 123 | + subscription.emitter === this, |
| 124 | + 'Subscription does not belong to this emitter.' |
| 125 | + ); |
| 126 | + this._subscriber.removeSubscription(subscription); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Returns an array of listeners that are currently registered for the given |
| 131 | + * event. |
| 132 | + * |
| 133 | + * @param {string} eventType - Name of the event to query |
| 134 | + * @returns {array} |
| 135 | + */ |
| 136 | + listeners(eventType) { |
| 137 | + const subscriptions = (this._subscriber.getSubscriptionsForType(eventType)); |
| 138 | + return subscriptions ? subscriptions.map(s => subscription.listener) : []; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Emits an event of the given type with the given data. All handlers of that |
| 143 | + * particular type will be notified. |
| 144 | + * |
| 145 | + * @param {string} eventType - Name of the event to emit |
| 146 | + * @param {...*} Arbitrary arguments to be passed to each registered listener |
| 147 | + * |
| 148 | + * @example |
| 149 | + * emitter.addListener('someEvent', function(message) { |
| 150 | + * console.log(message); |
| 151 | + * }); |
| 152 | + * |
| 153 | + * emitter.emit('someEvent', 'abc'); // logs 'abc' |
| 154 | + */ |
| 155 | + emit(eventType) { |
| 156 | + const subscriptions = (this._subscriber.getSubscriptionsForType(eventType)); |
| 157 | + if (subscriptions) { |
| 158 | + for (let i = 0, l = subscriptions.length; i < l; i++) { |
| 159 | + const subscription = subscriptions[i]; |
| 160 | + |
| 161 | + // The subscription may have been removed during this event loop. |
| 162 | + if (subscription) { |
| 163 | + this._currentSubscription = subscription; |
| 164 | + subscription.listener.apply( |
| 165 | + subscription.context, |
| 166 | + Array.prototype.slice.call(arguments, 1) |
| 167 | + ); |
| 168 | + } |
| 169 | + } |
| 170 | + this._currentSubscription = null; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Removes the given listener for event of specific type. |
| 176 | + * |
| 177 | + * @param {string} eventType - Name of the event to emit |
| 178 | + * @param {function} listener - Function to invoke when the specified event is |
| 179 | + * emitted |
| 180 | + * |
| 181 | + * @example |
| 182 | + * emitter.removeListener('someEvent', function(message) { |
| 183 | + * console.log(message); |
| 184 | + * }); // removes the listener if already registered |
| 185 | + * |
| 186 | + */ |
| 187 | + removeListener(eventType, listener) { |
| 188 | + const subscriptions = (this._subscriber.getSubscriptionsForType(eventType)); |
| 189 | + if (subscriptions) { |
| 190 | + for (let i = 0, l = subscriptions.length; i < l; i++) { |
| 191 | + const subscription = subscriptions[i]; |
| 192 | + |
| 193 | + // The subscription may have been removed during this event loop. |
| 194 | + // its listener matches the listener in method parameters |
| 195 | + if (subscription && subscription.listener === listener) { |
| 196 | + subscription.remove(); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +module.exports = EventEmitter; |
0 commit comments