[WIP] Added ADD/PREPEND Tags for Events
Showing
2 changed files
with
60 additions
and
2 deletions
... | @@ -129,6 +129,51 @@ EventEmitter.prototype.addListener = function addListener(type, listener) { | ... | @@ -129,6 +129,51 @@ EventEmitter.prototype.addListener = function addListener(type, listener) { |
129 | } else if (isArray(this._events[type])) { | 129 | } else if (isArray(this._events[type])) { |
130 | 130 | ||
131 | // If we've already got an array, just append. | 131 | // If we've already got an array, just append. |
132 | this._events[type].push(listener); | ||
133 | |||
134 | // Check for listener leak | ||
135 | if (!this._events[type].warned) { | ||
136 | var m; | ||
137 | if (this._maxListeners !== undefined) { | ||
138 | m = this._maxListeners; | ||
139 | } else { | ||
140 | m = defaultMaxListeners; | ||
141 | } | ||
142 | |||
143 | if (m && m > 0 && this._events[type].length > m) { | ||
144 | this._events[type].warned = true; | ||
145 | console.error('(node) warning: possible EventEmitter memory ' + | ||
146 | 'leak detected. %d listeners added. ' + | ||
147 | 'Use emitter.setMaxListeners() to increase limit.', | ||
148 | this._events[type].length); | ||
149 | console.trace(); | ||
150 | } | ||
151 | } | ||
152 | } else { | ||
153 | // Adding the second element, need to change to array. | ||
154 | this._events[type] = [this._events[type], listener]; | ||
155 | } | ||
156 | |||
157 | return this; | ||
158 | }; | ||
159 | |||
160 | EventEmitter.prototype.prependListener = function prependListener(type, listener) { | ||
161 | if ('function' !== typeof listener) { | ||
162 | throw new CasperError('addListener only takes instances of Function'); | ||
163 | } | ||
164 | |||
165 | if (!this._events) this._events = {}; | ||
166 | |||
167 | // To avoid recursion in the case that type == "newListeners"! Before | ||
168 | // adding it to the listeners, first emit "newListeners". | ||
169 | this.emit('newListener', type, listener); | ||
170 | |||
171 | if (!this._events[type]) { | ||
172 | // Optimize the case of one listener. Don't need the extra array object. | ||
173 | this._events[type] = listener; | ||
174 | } else if (isArray(this._events[type])) { | ||
175 | |||
176 | // If we've already got an array, just append. | ||
132 | this._events[type].unshift(listener); | 177 | this._events[type].unshift(listener); |
133 | 178 | ||
134 | // Check for listener leak | 179 | // Check for listener leak |
... | @@ -157,7 +202,20 @@ EventEmitter.prototype.addListener = function addListener(type, listener) { | ... | @@ -157,7 +202,20 @@ EventEmitter.prototype.addListener = function addListener(type, listener) { |
157 | return this; | 202 | return this; |
158 | }; | 203 | }; |
159 | 204 | ||
160 | EventEmitter.prototype.on = EventEmitter.prototype.addListener; | 205 | EventEmitter.prototype.on = function on(type, listener, tag) { |
206 | switch(tag) { | ||
207 | case "ADD": | ||
208 | return this.addListener(type, listener); | ||
209 | break; | ||
210 | |||
211 | case "PREPEND": | ||
212 | return this.prependListener; | ||
213 | break; | ||
214 | |||
215 | default: | ||
216 | return this.addListener(type, listener); | ||
217 | } | ||
218 | }; | ||
161 | 219 | ||
162 | EventEmitter.prototype.once = function once(type, listener) { | 220 | EventEmitter.prototype.once = function once(type, listener) { |
163 | if ('function' !== typeof listener) { | 221 | if ('function' !== typeof listener) { | ... | ... |
... | @@ -1532,7 +1532,7 @@ Tester.prototype.renderResults = function renderResults(exit, status, save) { | ... | @@ -1532,7 +1532,7 @@ Tester.prototype.renderResults = function renderResults(exit, status, save) { |
1532 | }; | 1532 | }; |
1533 | 1533 | ||
1534 | /** | 1534 | /** |
1535 | * Runs al suites contained in the paths passed as arguments. | 1535 | * Runs all suites contained in the paths passed as arguments. |
1536 | * | 1536 | * |
1537 | */ | 1537 | */ |
1538 | Tester.prototype.runSuites = function runSuites() { | 1538 | Tester.prototype.runSuites = function runSuites() { | ... | ... |
-
Please register or sign in to post a comment