-
Notifications
You must be signed in to change notification settings - Fork 30.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
events, doc: add type checking in defaultMaxListeners getter #11938
events, doc: add type checking in defaultMaxListeners getter #11938
Conversation
lib/events.js
Outdated
// force global console to be compiled. | ||
// see https://github.com/nodejs/node/issues/4467 | ||
console; | ||
defaultMaxListeners = arg; | ||
if (typeof n !== 'number' || n < 0 || isNaN(n)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we replace isNaN(n)
with n !== n
, possibly with a comment with a short explanation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mscdex Is this similar place also worth replacing then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably, but as a separate commit I'd say.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mscdex @vsemozhetbyt OK, updated.
aa568e6
to
58f7610
Compare
@vsemozhetbyt Thanks for reminding, changed it to a shorter one :) |
ce92c12
to
35db7e6
Compare
lib/events.js
Outdated
// force global console to be compiled. | ||
// see https://github.com/nodejs/node/issues/4467 | ||
console; | ||
defaultMaxListeners = arg; | ||
// check whether the input is a positive number (which value is | ||
// larger than zero and not a NaN). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'which value is larger than zero' should probably instead be something like 'whose value is zero or greater'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with a couple nits.
doc/api/events.md
Outdated
@@ -262,7 +262,8 @@ By default, a maximum of `10` listeners can be registered for any single | |||
event. This limit can be changed for individual `EventEmitter` instances | |||
using the [`emitter.setMaxListeners(n)`][] method. To change the default | |||
for *all* `EventEmitter` instances, the `EventEmitter.defaultMaxListeners` | |||
property can be used. | |||
property can be used. If trying to set it to a value which is not a positive |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can I suggest rewording this as:
If this value is not a positive number, a TypeError will be thrown.
lib/events.js
Outdated
@@ -52,11 +52,15 @@ Object.defineProperty(EventEmitter, 'defaultMaxListeners', { | |||
get: function() { | |||
return defaultMaxListeners; | |||
}, | |||
set: function(arg) { | |||
set: function(n) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing this variable name seems unnecessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with nits addressed
35db7e6
to
b3c06f2
Compare
@mscdex ... does this LGTY? |
// check whether the input is a positive number (whose value is zero or | ||
// greater and not a NaN). | ||
if (typeof arg !== 'number' || arg < 0 || arg !== arg) | ||
throw new TypeError('defaultMaxListeners must be a positive number'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'defaultMaxListeners' should be enclosed in double quotes to match the style when referencing property names in error messages.
One minor nit, but otherwise LGTM. |
I can take care of that nit while landing |
PR-URL: #11938 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com> Reviewed-By: Brian White <mscdex@mscdex.net>
Landed in 221b03a |
Since
EventEmitter.defaultMaxListeners
andemitter.setMaxListeners(n)
are both public APIs listed in the document, so it is reasonable to checkEventEmitter.defaultMaxListeners
getter's input like whatemitter.setMaxListeners(n)
does to avoid potential confusing results or error messages caused by some userland mistaken input, like:Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
events, doc