Skip to content
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

Add support for the EventTarget once option #1754

Merged
merged 8 commits into from May 7, 2020
Merged

Conversation

Sceat
Copy link
Contributor

@Sceat Sceat commented May 5, 2020

As per the documentation this is something not browser related and should be supported to allow transparent polyfill usage

https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

import WebSocket from 'ws'

new WebSocket().addEventListener('message', () => {}, { once: true })

@lpinca
Copy link
Member

lpinca commented May 5, 2020

Well, the whole EventTarget is browser related. It is emulated for convenience and it's still there only for historical reasons. Also it doesn't make much sense to use once for the 'open', 'error', and 'close' events as they are emitted only once.

Do you really need this feature? In Node.js you can use ws.once().

Sceat added a commit to Sceat/ws that referenced this pull request May 5, 2020
@Sceat
Copy link
Contributor Author

Sceat commented May 5, 2020

well to build an hybrid node/web client you wouldn't know if you can use .once as it doesn't exist in the browser :/ this option allow to be transparent and not care about which WebSocket object you use

lib/event-target.js Outdated Show resolved Hide resolved
lib/event-target.js Outdated Show resolved Hide resolved
@lpinca
Copy link
Member

lpinca commented May 5, 2020

Also this need tests (also for removeEventListener to make it sure that is works for once events added with addEventListener).

@lpinca
Copy link
Member

lpinca commented May 5, 2020

The documentation should also be updated https://github.com/websockets/ws/blob/master/doc/ws.md#websocketaddeventlistenertype-listener.

I think it does not worth the effort as it is only useful for the 'message' event and the same can be achieved by removing the listener from the inside of the listener itself but I have no objections.

Anyway if you want to add it we have to do it properly.

@Sceat
Copy link
Contributor Author

Sceat commented May 5, 2020

Also this need tests (also for removeEventListener to make it sure that is works for once events added with addEventListener).

Resolved the previous issues, but i'm kinda not very good at testing stuff 😨

@Sceat
Copy link
Contributor Author

Sceat commented May 5, 2020

rip coverage

Sceat and others added 6 commits May 7, 2020 12:10
As per the documentation this is something not browser related and should be supported to allow transparent polyfill usage

https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

```js
import WebSocket from 'ws'

new WebSocket().addEventListener('message', () => {}, { once: true })
```
This reverts commit 697ed9b.
lib/event-target.js Outdated Show resolved Hide resolved
@lpinca lpinca merged commit 2e5c01f into websockets:master May 7, 2020
@lpinca
Copy link
Member

lpinca commented May 7, 2020

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants