Skip to content

Commit 8c0022b

Browse files
committed
refactor: simplify event listener methods in intercept functions
Signed-off-by: Sebastian Beltran <[email protected]>
1 parent 93f7cb7 commit 8c0022b

File tree

2 files changed

+20
-57
lines changed

2 files changed

+20
-57
lines changed

index.js

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -299,34 +299,22 @@ function interceptAddListener (ee, fn) {
299299
var _on = ee.on
300300

301301
if (_addListener) {
302-
Object.defineProperty(ee, 'addListener', {
303-
configurable: true,
304-
value: addListener,
305-
writable: true
306-
})
302+
ee.addListener = function addListener (type, listener) {
303+
return fn.call(this, type, listener) === false
304+
? _addListener.call(this, type, listener)
305+
: this
306+
}
307307
}
308308

309309
if (_on) {
310-
Object.defineProperty(ee, 'on', {
311-
configurable: true,
312-
value: on,
313-
writable: true
314-
})
310+
ee.on = function on (type, listener) {
311+
return fn.call(this, type, listener) === false
312+
? _on.call(this, type, listener)
313+
: this
314+
}
315315
}
316316

317317
return _addListener || _on || noop
318-
319-
function addListener (type, listener) {
320-
return fn.call(this, type, listener) === false
321-
? _addListener.call(this, type, listener)
322-
: this
323-
}
324-
325-
function on (type, listener) {
326-
return fn.call(this, type, listener) === false
327-
? _on.call(this, type, listener)
328-
: this
329-
}
330318
}
331319

332320
/**
@@ -339,34 +327,22 @@ function interceptRemoveListener (ee, fn) {
339327
var _off = ee.off
340328

341329
if (_removeListener) {
342-
Object.defineProperty(ee, 'removeListener', {
343-
configurable: true,
344-
value: removeListener,
345-
writable: true
346-
})
330+
ee.removeListener = function removeListener(type, listener) {
331+
return fn.call(this, type, listener) === false
332+
? _removeListener.call(this, type, listener)
333+
: this;
334+
};
347335
}
348336

349337
if (_off) {
350-
Object.defineProperty(ee, 'off', {
351-
configurable: true,
352-
value: off,
353-
writable: true
354-
})
338+
ee.off = function off(type, listener) {
339+
return fn.call(this, type, listener) === false
340+
? _off.call(this, type, listener)
341+
: this;
342+
};
355343
}
356344

357345
return _removeListener || _off || noop
358-
359-
function removeListener (type, listener) {
360-
return fn.call(this, type, listener) === false
361-
? _removeListener.call(this, type, listener)
362-
: this
363-
}
364-
365-
function off (type, listener) {
366-
return fn.call(this, type, listener) === false
367-
? _off.call(this, type, listener)
368-
: this
369-
}
370346
}
371347

372348
/**

test/compression.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,6 @@ describe('compression()', function () {
368368
})
369369

370370
it('should support off("drain") after addListener("drain")', function (done) {
371-
if (!require('events').EventEmitter.prototype.off) { // off was added in Node.js v10
372-
this.skip()
373-
}
374371
var hasWarned = false
375372
var onWarning = function () {
376373
hasWarned = true
@@ -460,9 +457,6 @@ describe('compression()', function () {
460457
it('should not leak event listeners when res.unpipe() is used (#135)', function (done) {
461458
// unpipe and stream.Readable were added in v0.9.4
462459
var stream = require('stream')
463-
if (!(stream.Readable && stream.Readable.prototype.unpipe)) {
464-
this.skip()
465-
}
466460

467461
var hasWarned = false
468462
var onWarning = function () {
@@ -1457,13 +1451,6 @@ function createServer (opts, fn) {
14571451
return
14581452
}
14591453

1460-
if (typeof res.getMaxListeners !== 'function') {
1461-
// Added in v0.11.2
1462-
res.getMaxListeners = function getMaxListeners () {
1463-
return 10
1464-
}
1465-
}
1466-
14671454
fn(req, res)
14681455
})
14691456
})

0 commit comments

Comments
 (0)