annotate lib/EventEmitter.js @ 106:c06ec02217ee

many changes
author Sonny Piers <sonny@fastmail.net>
date Tue, 26 Jun 2012 12:02:14 +0200
parents fb50311997b5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
105
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
1 /**
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
2 * EventEmitter v3.1.5
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
3 * https://github.com/Wolfy87/EventEmitter
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
4 *
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
5 * Oliver Caldwell (http://oli.me.uk)
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
6 * Creative Commons Attribution 3.0 Unported License (http://creativecommons.org/licenses/by/3.0/)
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
7 */
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
8
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
9 ;(function(exports) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
10 // JSHint config
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
11 /*jshint smarttabs:true,devel:true*/
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
12 /*global define:true*/
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
13
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
14 // Place the script into strict mode
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
15 'use strict';
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
16
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
17 /**
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
18 * EventEmitter class
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
19 * Creates an object with event registering and firing methods
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
20 */
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
21 function EventEmitter() {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
22 // Initialise required storage variables
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
23 this._events = {};
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
24 this._maxListeners = 10;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
25 }
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
26
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
27 /**
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
28 * Event class
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
29 * Contains Event methods and property storage
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
30 *
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
31 * @param {String} type Event type name
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
32 * @param {Function} listener Function to be called when the event is fired
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
33 * @param {Object} scope Object that this should be set to when the listener is called
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
34 * @param {Boolean} once If true then the listener will be removed after the first call
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
35 * @param {Object} instance The parent EventEmitter instance
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
36 */
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
37 function Event(type, listener, scope, once, instance) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
38 // Store arguments
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
39 this.type = type;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
40 this.listener = listener;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
41 this.scope = scope;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
42 this.once = once;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
43 this.instance = instance;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
44 }
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
45
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
46 /**
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
47 * Executes the listener
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
48 *
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
49 * @param {Array} args List of arguments to pass to the listener
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
50 * @return {Boolean} If false then it was a once event
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
51 */
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
52 Event.prototype.fire = function(args) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
53 this.listener.apply(this.scope || this.instance, args);
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
54
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
55 // Remove the listener if this is a once only listener
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
56 if(this.once) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
57 this.instance.removeListener(this.type, this.listener, this.scope);
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
58 return false;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
59 }
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
60 };
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
61
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
62 /**
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
63 * Passes every listener for a specified event to a function one at a time
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
64 *
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
65 * @param {String} type Event type name
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
66 * @param {Function} callback Function to pass each listener to
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
67 * @return {Object} The current EventEmitter instance to allow chaining
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
68 */
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
69 EventEmitter.prototype.eachListener = function(type, callback) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
70 // Initialise variables
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
71 var i = null,
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
72 possibleListeners = null,
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
73 result = null;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
74
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
75 // Only loop if the type exists
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
76 if(this._events.hasOwnProperty(type)) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
77 possibleListeners = this._events[type];
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
78
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
79 for(i = 0; i < possibleListeners.length; i += 1) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
80 result = callback.call(this, possibleListeners[i], i);
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
81
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
82 if(result === false) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
83 i -= 1;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
84 }
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
85 else if(result === true) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
86 break;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
87 }
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
88 }
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
89 }
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
90
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
91 // Return the instance to allow chaining
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
92 return this;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
93 };
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
94
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
95 /**
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
96 * Adds an event listener for the specified event
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
97 *
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
98 * @param {String} type Event type name
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
99 * @param {Function} listener Function to be called when the event is fired
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
100 * @param {Object} scope Object that this should be set to when the listener is called
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
101 * @param {Boolean} once If true then the listener will be removed after the first call
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
102 * @return {Object} The current EventEmitter instance to allow chaining
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
103 */
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
104 EventEmitter.prototype.addListener = function(type, listener, scope, once) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
105 // Create the listener array if it does not exist yet
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
106 if(!this._events.hasOwnProperty(type)) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
107 this._events[type] = [];
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
108 }
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
109
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
110 // Push the new event to the array
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
111 this._events[type].push(new Event(type, listener, scope, once, this));
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
112
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
113 // Emit the new listener event
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
114 this.emit('newListener', type, listener, scope, once);
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
115
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
116 // Check if we have exceeded the maxListener count
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
117 // Ignore this check if the count is 0
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
118 // Also don't check if we have already fired a warning
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
119 if(this._maxListeners && !this._events[type].warned && this._events[type].length > this._maxListeners) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
120 // The max listener count has been exceeded!
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
121 // Warn via the console if it exists
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
122 if(typeof console !== 'undefined') {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
123 console.warn('Possible EventEmitter memory leak detected. ' + this._events[type].length + ' listeners added. Use emitter.setMaxListeners() to increase limit.');
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
124 }
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
125
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
126 // Set the flag so it doesn't fire again
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
127 this._events[type].warned = true;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
128 }
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
129
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
130 // Return the instance to allow chaining
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
131 return this;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
132 };
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
133
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
134 /**
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
135 * Alias of the addListener method
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
136 *
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
137 * @param {String} type Event type name
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
138 * @param {Function} listener Function to be called when the event is fired
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
139 * @param {Object} scope Object that this should be set to when the listener is called
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
140 * @param {Boolean} once If true then the listener will be removed after the first call
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
141 */
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
142 EventEmitter.prototype.on = EventEmitter.prototype.addListener;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
143
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
144 /**
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
145 * Alias of the addListener method but will remove the event after the first use
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
146 *
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
147 * @param {String} type Event type name
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
148 * @param {Function} listener Function to be called when the event is fired
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
149 * @param {Object} scope Object that this should be set to when the listener is called
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
150 * @return {Object} The current EventEmitter instance to allow chaining
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
151 */
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
152 EventEmitter.prototype.once = function(type, listener, scope) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
153 return this.addListener(type, listener, scope, true);
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
154 };
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
155
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
156 /**
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
157 * Removes the a listener for the specified event
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
158 *
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
159 * @param {String} type Event type name the listener must have for the event to be removed
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
160 * @param {Function} listener Listener the event must have to be removed
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
161 * @param {Object} scope The scope the event must have to be removed
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
162 * @return {Object} The current EventEmitter instance to allow chaining
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
163 */
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
164 EventEmitter.prototype.removeListener = function(type, listener, scope) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
165 this.eachListener(type, function(currentListener, index) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
166 // If this is the listener remove it from the array
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
167 // We also compare the scope if it was passed
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
168 if(currentListener.listener === listener && (!scope || currentListener.scope === scope)) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
169 this._events[type].splice(index, 1);
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
170 }
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
171 });
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
172
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
173 // Remove the property if there are no more listeners
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
174 if(this._events[type] && this._events[type].length === 0) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
175 delete this._events[type];
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
176 }
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
177
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
178 // Return the instance to allow chaining
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
179 return this;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
180 };
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
181
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
182 /**
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
183 * Alias of the removeListener method
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
184 *
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
185 * @param {String} type Event type name the listener must have for the event to be removed
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
186 * @param {Function} listener Listener the event must have to be removed
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
187 * @param {Object} scope The scope the event must have to be removed
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
188 * @return {Object} The current EventEmitter instance to allow chaining
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
189 */
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
190 EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
191
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
192 /**
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
193 * Removes all listeners for a specified event
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
194 * If no event type is passed it will remove every listener
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
195 *
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
196 * @param {String} type Event type name to remove all listeners from
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
197 * @return {Object} The current EventEmitter instance to allow chaining
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
198 */
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
199 EventEmitter.prototype.removeAllListeners = function(type) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
200 // Check for a type, if there is none remove all listeners
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
201 // If there is a type however, just remove the listeners for that type
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
202 if(type && this._events.hasOwnProperty(type)) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
203 delete this._events[type];
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
204 }
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
205 else if(!type) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
206 this._events = {};
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
207 }
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
208
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
209 // Return the instance to allow chaining
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
210 return this;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
211 };
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
212
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
213 /**
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
214 * Retrieves the array of listeners for a specified event
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
215 *
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
216 * @param {String} type Event type name to return all listeners from
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
217 * @return {Array} Will return either an array of listeners or an empty array if there are none
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
218 */
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
219 EventEmitter.prototype.listeners = function(type) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
220 // Return the array of listeners or an empty array if it does not exist
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
221 if(this._events.hasOwnProperty(type)) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
222 // It does exist, loop over building the array
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
223 var listeners = [];
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
224
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
225 this.eachListener(type, function(evt) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
226 listeners.push(evt.listener);
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
227 });
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
228
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
229 return listeners;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
230 }
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
231
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
232 return [];
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
233 };
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
234
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
235 /**
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
236 * Emits an event executing all appropriate listeners
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
237 * All values passed after the type will be passed as arguments to the listeners
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
238 *
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
239 * @param {String} type Event type name to run all listeners from
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
240 * @return {Object} The current EventEmitter instance to allow chaining
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
241 */
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
242 EventEmitter.prototype.emit = function(type) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
243 // Calculate the arguments
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
244 var args = [],
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
245 i = null;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
246
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
247 for(i = 1; i < arguments.length; i += 1) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
248 args.push(arguments[i]);
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
249 }
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
250
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
251 this.eachListener(type, function(currentListener) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
252 return currentListener.fire(args);
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
253 });
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
254
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
255 // Return the instance to allow chaining
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
256 return this;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
257 };
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
258
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
259 /**
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
260 * Sets the max listener count for the EventEmitter
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
261 * When the count of listeners for an event exceeds this limit a warning will be printed
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
262 * Set to 0 for no limit
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
263 *
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
264 * @param {Number} maxListeners The new max listener limit
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
265 * @return {Object} The current EventEmitter instance to allow chaining
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
266 */
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
267 EventEmitter.prototype.setMaxListeners = function(maxListeners) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
268 this._maxListeners = maxListeners;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
269
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
270 // Return the instance to allow chaining
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
271 return this;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
272 };
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
273
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
274 /**
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
275 * Builds a clone of the prototype object for you to extend with
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
276 *
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
277 * @return {Object} A clone of the EventEmitter prototype object
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
278 */
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
279 EventEmitter.extend = function() {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
280 // First thing we need to do is create our new prototype
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
281 // Then we loop over the current one copying each method out
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
282 // When done, simply return the clone
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
283 var clone = {},
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
284 current = this.prototype,
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
285 key = null;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
286
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
287 for(key in current) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
288 // Make sure this is actually a property of the object before copying it
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
289 // We don't want any default object methods leaking though
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
290 if(current.hasOwnProperty(key)) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
291 clone[key] = current[key];
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
292 }
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
293 }
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
294
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
295 // All done, return the clone
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
296 return clone;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
297 };
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
298
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
299 // Export the class
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
300 // If AMD is available then use it
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
301 if(typeof define === 'function' && define.amd) {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
302 define(function() {
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
303 return EventEmitter;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
304 });
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
305 }
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
306
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
307 // No matter what it will be added to the global object
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
308 exports.EventEmitter = EventEmitter;
fb50311997b5 Add EventEmitter.js lib
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
309 }(this));