@@ -48,6 +48,13 @@ function lazyErrors() {
4848 return errors ;
4949}
5050
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+
5158Object . defineProperty ( EventEmitter , 'defaultMaxListeners' , {
5259 enumerable : true ,
5360 get : function ( ) {
@@ -195,10 +202,7 @@ function _addListener(target, type, listener, prepend) {
195202 var events ;
196203 var existing ;
197204
198- if ( typeof listener !== 'function' ) {
199- const errors = lazyErrors ( ) ;
200- throw new errors . ERR_INVALID_ARG_TYPE ( 'listener' , 'Function' , listener ) ;
201- }
205+ checkListener ( listener ) ;
202206
203207 events = target . _events ;
204208 if ( events === undefined ) {
@@ -283,20 +287,16 @@ function _onceWrap(target, type, listener) {
283287}
284288
285289EventEmitter . 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+
290292 this . on ( type , _onceWrap ( this , type , listener ) ) ;
291293 return this ;
292294} ;
293295
294296EventEmitter . prototype . prependOnceListener =
295297 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+
300300 this . prependListener ( type , _onceWrap ( this , type , listener ) ) ;
301301 return this ;
302302 } ;
@@ -306,10 +306,7 @@ EventEmitter.prototype.removeListener =
306306 function removeListener ( type , listener ) {
307307 var list , events , position , i , originalListener ;
308308
309- if ( typeof listener !== 'function' ) {
310- const errors = lazyErrors ( ) ;
311- throw new errors . ERR_INVALID_ARG_TYPE ( 'listener' , 'Function' , listener ) ;
312- }
309+ checkListener ( listener ) ;
313310
314311 events = this . _events ;
315312 if ( events === undefined )
0 commit comments