|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Creates a new instance of Emitter. |
| 5 | + * @class |
| 6 | + * @returns {Object} Returns a new instance of Emitter. |
| 7 | + * @example |
| 8 | + * // Creates a new instance of Emitter. |
| 9 | + * var Emitter = require('emitter'); |
| 10 | + * |
| 11 | + * var emitter = new Emitter(); |
| 12 | + */ |
| 13 | +class Emitter { |
| 14 | + |
| 15 | + /** |
| 16 | + * Adds a listener to the collection for the specified event. |
| 17 | + * @memberof! Emitter.prototype |
| 18 | + * @function |
| 19 | + * @param {String} event - The event name. |
| 20 | + * @param {Function} listener - A listener function to add. |
| 21 | + * @returns {Object} Returns an instance of Emitter. |
| 22 | + * @example |
| 23 | + * // Add an event listener to "foo" event. |
| 24 | + * emitter.on('foo', listener); |
| 25 | + */ |
| 26 | + on(event, listener) { |
| 27 | + // Use the current collection or create it. |
| 28 | + this._eventCollection = this._eventCollection || {}; |
| 29 | + |
| 30 | + // Use the current collection of an event or create it. |
| 31 | + this._eventCollection[event] = this._eventCollection[event] || []; |
| 32 | + |
| 33 | + // Appends the listener into the collection of the given event |
| 34 | + this._eventCollection[event].push(listener); |
| 35 | + |
| 36 | + return this; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Adds a listener to the collection for the specified event that will be called only once. |
| 41 | + * @memberof! Emitter.prototype |
| 42 | + * @function |
| 43 | + * @param {String} event - The event name. |
| 44 | + * @param {Function} listener - A listener function to add. |
| 45 | + * @returns {Object} Returns an instance of Emitter. |
| 46 | + * @example |
| 47 | + * // Will add an event handler to "foo" event once. |
| 48 | + * emitter.once('foo', listener); |
| 49 | + */ |
| 50 | + once(event, listener) { |
| 51 | + const self = this; |
| 52 | + |
| 53 | + function fn() { |
| 54 | + self.off(event, fn); |
| 55 | + listener.apply(this, arguments); |
| 56 | + } |
| 57 | + |
| 58 | + fn.listener = listener; |
| 59 | + |
| 60 | + this.on(event, fn); |
| 61 | + |
| 62 | + return this; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Removes a listener from the collection for the specified event. |
| 67 | + * @memberof! Emitter.prototype |
| 68 | + * @function |
| 69 | + * @param {String} event - The event name. |
| 70 | + * @param {Function} listener - A listener function to remove. |
| 71 | + * @returns {Object} Returns an instance of Emitter. |
| 72 | + * @example |
| 73 | + * // Remove a given listener. |
| 74 | + * emitter.off('foo', listener); |
| 75 | + */ |
| 76 | + off(event, listener) { |
| 77 | + |
| 78 | + let listeners; |
| 79 | + |
| 80 | + // Defines listeners value. |
| 81 | + if (!this._eventCollection || !(listeners = this._eventCollection[event])) { |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + listeners.forEach(function(fn, i) { |
| 86 | + if (fn === listener || fn.listener === listener) { |
| 87 | + // Removes the given listener. |
| 88 | + listeners.splice(i, 1); |
| 89 | + return; |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + // Removes an empty event collection. |
| 94 | + if (listeners.length === 0) { |
| 95 | + delete this._eventCollection[event]; |
| 96 | + } |
| 97 | + |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Execute each item in the listener collection in order with the specified data. |
| 103 | + * @memberof! Emitter.prototype |
| 104 | + * @function |
| 105 | + * @param {String} event - The name of the event you want to emit. |
| 106 | + * @param {...Object} data - Data to pass to the listeners. |
| 107 | + * @returns {Object} Returns an instance of Emitter. |
| 108 | + * @example |
| 109 | + * // Emits the "foo" event with 'param1' and 'param2' as arguments. |
| 110 | + * emitter.emit('foo', 'param1', 'param2'); |
| 111 | + */ |
| 112 | + emit(event, ...args) { |
| 113 | + let listeners; |
| 114 | + |
| 115 | + // Defines listeners value. |
| 116 | + if (!this._eventCollection || !(listeners = this._eventCollection[event])) { |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + // Clone listeners |
| 121 | + listeners = listeners.slice(0); |
| 122 | + |
| 123 | + listeners.forEach(fn => fn.apply(this, args)); |
| 124 | + |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * Exports Emitter |
| 132 | + */ |
| 133 | +export default Emitter; |
0 commit comments