-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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: EventTarget use validators and small fix #33663
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,23 +2,24 @@ | |
|
||
const { | ||
ArrayFrom, | ||
Boolean, | ||
Error, | ||
Map, | ||
NumberIsInteger, | ||
Object, | ||
Set, | ||
Symbol, | ||
NumberIsNaN, | ||
SymbolToStringTag, | ||
} = primordials; | ||
|
||
const { | ||
codes: { | ||
ERR_INVALID_ARG_TYPE, | ||
ERR_EVENT_RECURSION, | ||
ERR_OUT_OF_RANGE, | ||
ERR_MISSING_ARGS | ||
} | ||
} = require('internal/errors'); | ||
const { validateInteger, validateObject } = require('internal/validators'); | ||
|
||
const { customInspectSymbol } = require('internal/util'); | ||
const { inspect } = require('util'); | ||
|
@@ -54,11 +55,10 @@ class Event { | |
|
||
|
||
constructor(type, options) { | ||
if (arguments.length === 0) { | ||
if (arguments.length === 0) | ||
throw new ERR_MISSING_ARGS('type'); | ||
} | ||
if (options != null && typeof options !== 'object') | ||
throw new ERR_INVALID_ARG_TYPE('options', 'object', options); | ||
if (options != null) | ||
validateObject(options, 'options'); | ||
const { cancelable, bubbles, composed } = { ...options }; | ||
this.#cancelable = !!cancelable; | ||
this.#bubbles = !!bubbles; | ||
|
@@ -80,7 +80,7 @@ class Event { | |
return name; | ||
|
||
const opts = Object.assign({}, options, { | ||
dept: options.depth === null ? null : options.depth - 1 | ||
depth: NumberIsInteger(options.depth) ? options.depth - 1 : options.depth | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd assume this was a bug ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is both correct. The user should only ever pass through |
||
}); | ||
|
||
return `${name} ${inspect({ | ||
|
@@ -296,7 +296,7 @@ class EventTarget { | |
this.#emitting.delete(event.type); | ||
event[kTarget] = undefined; | ||
|
||
return event.defaultPrevented === true ? false : true; | ||
return event.defaultPrevented !== true; | ||
} | ||
|
||
[customInspectSymbol](depth, options) { | ||
|
@@ -305,7 +305,7 @@ class EventTarget { | |
return name; | ||
|
||
const opts = Object.assign({}, options, { | ||
dept: options.depth === null ? null : options.depth - 1 | ||
depth: NumberIsInteger(options.depth) ? options.depth - 1 : options.depth | ||
}); | ||
|
||
return `${name} ${inspect({}, opts)}`; | ||
|
@@ -350,9 +350,7 @@ class NodeEventTarget extends EventTarget { | |
} | ||
|
||
setMaxListeners(n) { | ||
if (typeof n !== 'number' || n < 0 || NumberIsNaN(n)) { | ||
throw new ERR_OUT_OF_RANGE('n', 'a non-negative number', n); | ||
} | ||
validateInteger(n, 'n', 0); | ||
this.#maxListeners = n; | ||
return this; | ||
} | ||
|
@@ -430,20 +428,13 @@ function validateListener(listener) { | |
} | ||
|
||
function validateEventListenerOptions(options) { | ||
if (typeof options === 'boolean') { | ||
options = { capture: options }; | ||
} | ||
if (options == null || typeof options !== 'object') | ||
throw new ERR_INVALID_ARG_TYPE('options', 'object', options); | ||
const { | ||
once = false, | ||
capture = false, | ||
passive = false, | ||
} = options; | ||
if (typeof options === 'boolean') | ||
return { capture: options }; | ||
validateObject(options, 'options'); | ||
return { | ||
once: !!once, | ||
capture: !!capture, | ||
passive: !!passive, | ||
once: Boolean(options.once), | ||
capture: Boolean(options.capture), | ||
passive: Boolean(options.passive), | ||
}; | ||
} | ||
|
||
|
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.
Also, @BridgeAR shouldn't this return
this
? (as well as the other one)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.
Yes, that would be ideal.
this
will just letinspect()
to properly know what to do.