@@ -48,6 +48,13 @@ function lazyErrors() { |
48 | 48 | return errors; |
49 | 49 | } |
50 | 50 |
|
| 51 | +function checkListener(listener) { |
| 52 | + if (typeof listener !== 'function') { |
| 53 | + const errors = lazyErrors(); |
| 54 | + throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener); |
| 55 | + } |
| 56 | +} |
| 57 | + |
51 | 58 | Object.defineProperty(EventEmitter, 'defaultMaxListeners', { |
52 | 59 | enumerable: true, |
53 | 60 | get: function() { |
@@ -195,10 +202,7 @@ function _addListener(target, type, listener, prepend) { |
195 | 202 | var events; |
196 | 203 | var existing; |
197 | 204 |
|
198 | | - if (typeof listener !== 'function') { |
199 | | - const errors = lazyErrors(); |
200 | | - throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener); |
201 | | - } |
| 205 | + checkListener(listener); |
202 | 206 |
|
203 | 207 | events = target._events; |
204 | 208 | if (events === undefined) { |
@@ -283,20 +287,16 @@ function _onceWrap(target, type, listener) { |
283 | 287 | } |
284 | 288 |
|
285 | 289 | EventEmitter.prototype.once = function once(type, listener) { |
286 | | - if (typeof listener !== 'function') { |
287 | | - const errors = lazyErrors(); |
288 | | - throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener); |
289 | | - } |
| 290 | + checkListener(listener); |
| 291 | + |
290 | 292 | this.on(type, _onceWrap(this, type, listener)); |
291 | 293 | return this; |
292 | 294 | }; |
293 | 295 |
|
294 | 296 | EventEmitter.prototype.prependOnceListener = |
295 | 297 | function prependOnceListener(type, listener) { |
296 | | - if (typeof listener !== 'function') { |
297 | | - const errors = lazyErrors(); |
298 | | - throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener); |
299 | | - } |
| 298 | + checkListener(listener); |
| 299 | + |
300 | 300 | this.prependListener(type, _onceWrap(this, type, listener)); |
301 | 301 | return this; |
302 | 302 | }; |
@@ -306,10 +306,7 @@ EventEmitter.prototype.removeListener = |
306 | 306 | function removeListener(type, listener) { |
307 | 307 | var list, events, position, i, originalListener; |
308 | 308 |
|
309 | | - if (typeof listener !== 'function') { |
310 | | - const errors = lazyErrors(); |
311 | | - throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener); |
312 | | - } |
| 309 | + checkListener(listener); |
313 | 310 |
|
314 | 311 | events = this._events; |
315 | 312 | if (events === undefined) |
|