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

Symbols should be allowed as Event types. #331

Open
bicknellr opened this issue Sep 21, 2016 · 18 comments
Open

Symbols should be allowed as Event types. #331

bicknellr opened this issue Sep 21, 2016 · 18 comments
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest topic: events

Comments

@bicknellr
Copy link

This prevents the need to use a namespacing scheme or filter out similarly named events in the listeners themselves. Components generating particular events could instead expose symbols on their constructor (or other relevant object) to allow a user to listen to an event.

@annevk annevk added the needs implementer interest Moving the issue forward requires implementers to express interest label Sep 23, 2016
@annevk
Copy link
Member

annevk commented Sep 23, 2016

Sounds like a reasonable feature. We'd need interest from implementers and support in IDL. I'd also like to hear from framework developers and major websites if they'd use this.

@jcrben
Copy link

jcrben commented Apr 21, 2017

I'm not an implementor or a developer at a major website (just a small one!). Could you provide a brief example of how this would look in practice to understand how user-friendly it would be? I'd like to avoid using jQuery, but namespacing events is one of those things that the native event listening API doesn't seem to provide.

@annevk
Copy link
Member

annevk commented Mar 13, 2018

Something like

const eventType = Symbol(),
      event = new Event(eventType);
eventTarget.addEventListener(eventType, ...);

@bicknellr
Copy link
Author

^ Yes, that's exactly it.

@annevk annevk added the addition/proposal New features or enhancements label Apr 11, 2019
@annevk
Copy link
Member

annevk commented Dec 8, 2020

@benjamingr have there been requests for this in Node.js?

@benjamingr
Copy link
Member

It's hard to say:

@matthewp
Copy link

matthewp commented Jul 8, 2021

This issue has come up with the web component community which wants to establish common event types but want to avoid name collisions, see: webcomponents-cg/community-protocols#19 (comment)

Allowing symbols as event types would resolve this issue for us. Is there still implementer interest in this idea?

@Westbrook
Copy link

Thanks for connecting these two conversations @matthewp 🙏

Very interested in what might be possible here!!

@benjamingr
Copy link
Member

@domenic is this something that’s interesting for Chrome in terms of web components? If it is and there is implement interest I am happy to work on the spec once back from paternity leave.

(I am stuck on the AbortSignal stuff)

@domenic
Copy link
Member

domenic commented Jul 9, 2021

@mfreed7 is the best person to ask for web components stuff these days.

@mfreed7
Copy link
Contributor

mfreed7 commented Nov 11, 2021

I think this sounds like a reasonable idea, and doesn't seem too hard to implement (modulo the details). I'm at least supportive enough to continue the discussion and get a more concrete proposal!

@benjamingr
Copy link
Member

@mfreed7 I am happy to write the bikeshed - I think the proposal in this case is pretty straightforward:

  • addEventListener accepts DOMString or Symbol for type.
  • The Event constructor (as well as all subclasses implciitly) take a DOMString or Symbol for type.
  • Places in the spec that reference the fact event names are strings get those references fixed.

The primary use case is enabling events without having possible naming collisions something like:

const myLibraryEvent = new Symbol('myLibrary');
const component = obtainElement(); // get any target
component.addEventListener(myLibraryEvent, myLibraryLogicForEvent);

// later, in other code, communicate without collisions
component.dispatchEvent(new Event(myLibraryEvent));

I can list use cases (it's mostly important in browsers where events propagate but also in Node.js and other EventTarget consumers)


Let me know what details you'd need to make a decision on this and if you think this is interesting enough to prototype I'll try to open a PR to this repo next week'ish.

@annevk
Copy link
Member

annevk commented Nov 15, 2021

Unless it got fixed in the intervening years, whatwg/webidl#377 (comment) will also need to be addressed in Web IDL I think. That can happen in parallel and by someone else, but it will need to be solved as this would be the first API to adopt symbols.

@benjamingr
Copy link
Member

@annevk I actually have never contributed to webidl before and it's interesting learning experience for me to do so. I'm happy to lock time to take a stab next week if I will get guidance when I will (undoubtedly) get stuck like last time :)?

@annevk
Copy link
Member

annevk commented Nov 15, 2021

@benjamingr sounds great! The editors should be able to help you, yeah.

@mfreed7
Copy link
Contributor

mfreed7 commented Nov 17, 2021

Let me know what details you'd need to make a decision on this and if you think this is interesting enough to prototype I'll try to open a PR to this repo next week'ish.

Just to be totally clear - I support this proposal, but I'll likely not be the first one to try to prototype it. Perhaps the Gecko or WebKit folks want to take a look first?

@jasnell
Copy link
Contributor

jasnell commented Dec 3, 2021

Just weighing in, I'm +1 on supporting this both in Node.js and Cloudflare Workers. I'll wait to implement in either until the spec text is drafted up tho. I definitely think it's worthwhile.

@jasnell
Copy link
Contributor

jasnell commented Jan 27, 2022

Just updating here. I did put together a quick prototype in Workers (not public, just local dev) to see if there would be any issues here and it all seems to just work rather nicely. Not a major (or even moderate) priority for us, of course, so no rush on getting it in, but I can't see anything that would/should prevent supporting this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest topic: events
Development

No branches or pull requests

9 participants