-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
Websocket#addEventListener()
ignore non standard events
Make `Websocket.prototype.addEventListener()` a noop if the `type` argument is not one of `'close'`, `'error'`, `'message'`, or `'open'`.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
'use strict'; | ||
|
||
const { kListener } = require('./constants'); | ||
|
||
/** | ||
* Class representing an event. | ||
* | ||
|
@@ -126,8 +128,6 @@ const EventTarget = { | |
* @public | ||
*/ | ||
addEventListener(type, listener, options) { | ||
if (typeof listener !== 'function') return; | ||
|
||
function onMessage(data, isBinary) { | ||
listener.call( | ||
this, | ||
|
@@ -150,35 +150,31 @@ const EventTarget = { | |
const method = options && options.once ? 'once' : 'on'; | ||
|
||
if (type === 'message') { | ||
onMessage._listener = listener; | ||
onMessage[kListener] = listener; | ||
this[method](type, onMessage); | ||
} else if (type === 'close') { | ||
onClose._listener = listener; | ||
onClose[kListener] = listener; | ||
this[method](type, onClose); | ||
} else if (type === 'error') { | ||
onError._listener = listener; | ||
onError[kListener] = listener; | ||
this[method](type, onError); | ||
} else if (type === 'open') { | ||
onOpen._listener = listener; | ||
onOpen[kListener] = listener; | ||
this[method](type, onOpen); | ||
} else { | ||
this[method](type, listener); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
lpinca
Author
Member
|
||
} | ||
}, | ||
|
||
/** | ||
* Remove an event listener. | ||
* | ||
* @param {String} type A string representing the event type to remove | ||
* @param {Function} listener The listener to remove | ||
* @param {Function} handler The listener to remove | ||
* @public | ||
*/ | ||
removeEventListener(type, listener) { | ||
const listeners = this.listeners(type); | ||
|
||
for (let i = 0; i < listeners.length; i++) { | ||
if (listeners[i] === listener || listeners[i]._listener === listener) { | ||
this.removeListener(type, listeners[i]); | ||
removeEventListener(type, handler) { | ||
for (const listener of this.listeners(type)) { | ||
if (listener === handler || listener[kListener] === handler) { | ||
this.removeListener(type, listener); | ||
} | ||
} | ||
} | ||
|
1 comment
on commit a421eb5
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.
There are typos in commit message:
Websocket#addEventListener()
->WebSocket#addEventListener()
.Websocket.prototype.addEventListener()
->WebSocket.prototype.addEventListener()
.
@lpinca a couple of comments about this change:
else
case throw instead of silently ignoring the event? (that would save time for people using the API incorrectly :-)ping
event be supported here?on(type, listener)
behave likeaddEventListener(type, listener)
?Let me know if that makes sense, I'm happy to submit a PR!