-
Notifications
You must be signed in to change notification settings - Fork 30.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
events: handle inherited properties properly
As of now, the events module considers inherited properties as one of the valid types, by default. > process.version 'v3.0.0' > events.EventEmitter.listenerCount(new events.EventEmitter(), 'toString') 1 This patch makes sure that the inherited properties are considered as normal types and they will not be counted unless explicitly added. > process.version 'v4.0.0-pre' > events.EventEmitter.listenerCount(new events.EventEmitter(), 'toString') 0 > const emitter = new events.EventEmitter(); undefined > emitter.on('toString', function() {}); EventEmitter { domain: Domain { domain: null, _events: { error: [Function] }, _eventsCount: 1, _maxListeners: undefined, members: [] }, _events: { toString: [Function] }, _eventsCount: 1, _maxListeners: undefined } > events.EventEmitter.listenerCount(emitter, 'toString') 1
- Loading branch information
1 parent
84f0964
commit 85b2939
Showing
3 changed files
with
30 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,34 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
require('../common'); | ||
const assert = require('assert'); | ||
const EventEmitter = require('events'); | ||
|
||
const emitter = new EventEmitter(); | ||
emitter.on('foo', function() {}); | ||
emitter.on('foo', function() {}); | ||
emitter.on('baz', function() {}); | ||
|
||
function noop() {} | ||
|
||
emitter.on('foo', noop); | ||
emitter.on('foo', noop); | ||
emitter.on('baz', noop); | ||
// Allow any type | ||
emitter.on(123, function() {}); | ||
emitter.on(123, noop); | ||
|
||
assert.strictEqual(EventEmitter.listenerCount(emitter, 'foo'), 2); | ||
assert.strictEqual(emitter.listenerCount('foo'), 2); | ||
assert.strictEqual(emitter.listenerCount('bar'), 0); | ||
assert.strictEqual(emitter.listenerCount('baz'), 1); | ||
assert.strictEqual(emitter.listenerCount(123), 1); | ||
|
||
// The inherited properties should not be counted towards the actual | ||
// listeners count | ||
assert.strictEqual(EventEmitter.listenerCount(emitter, 'toString'), 0); | ||
|
||
// when we add a new listener with the name of an inherited property, it should | ||
// accept it | ||
emitter.on('toString', noop); | ||
assert.strictEqual(EventEmitter.listenerCount(emitter, 'toString'), 1); | ||
|
||
// after removing a listener with the name of an inherited property, the count | ||
// should reduce by one | ||
emitter.removeListener('toString', noop); | ||
assert.strictEqual(EventEmitter.listenerCount(emitter, 'toString'), 0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters