Index: lib/events.js |
=================================================================== |
--- a/lib/events.js |
+++ b/lib/events.js |
@@ -80,6 +80,17 @@ |
}, |
/** |
+ * Returns a copy of the array of listeners for the specified event. |
+ * |
+ * @return {function[]} |
+ */ |
+ listeners: function(name) |
Sebastian Noack
2016/03/24 11:07:13
node.js has that method as well. We need it to reu
|
+ { |
+ let listeners = this._listeners[name]; |
+ return listeners ? listeners.slice() : []; |
+ }, |
+ |
+ /** |
* Calls all previously added listeners for the given event name. |
* |
* @param {string} name |
@@ -87,16 +98,12 @@ |
*/ |
emit: function(name) |
{ |
- let listeners = this._listeners[name]; |
- if (listeners) |
- { |
- let args = []; |
- for (let i = 1; i < arguments.length; i++) |
- args.push(arguments[i]); |
+ let args = []; |
+ for (let i = 1; i < arguments.length; i++) |
+ args.push(arguments[i]); |
- let currentListeners = listeners.slice(); |
- for (let listener of currentListeners) |
- listener.apply(null, args); |
- } |
+ let listeners = this.listeners(name); |
+ for (let listener of listeners) |
+ listener.apply(null, args); |
} |
}; |