whatwg / dom

DOM Standard
https://dom.spec.whatwg.org/
Other
1.58k stars 295 forks source link

Symbols should be allowed as Event types. #331

Open bicknellr opened 8 years ago

bicknellr commented 8 years ago

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 commented 8 years ago

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 commented 7 years ago

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 commented 6 years ago

Something like

const eventType = Symbol(),
      event = new Event(eventType);
eventTarget.addEventListener(eventType, ...);
bicknellr commented 6 years ago

^ Yes, that's exactly it.

annevk commented 3 years ago

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

benjamingr commented 3 years ago

It's hard to say:

matthewp commented 3 years ago

This issue has come up with the web component community which wants to establish common event types but want to avoid name collisions, see: https://github.com/webcomponents/community-protocols/issues/19#issuecomment-876482731

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

Westbrook commented 3 years ago

Thanks for connecting these two conversations @matthewp 🙏

Very interested in what might be possible here!!

benjamingr commented 3 years ago

@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 commented 3 years ago

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

mfreed7 commented 2 years ago

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 commented 2 years ago

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

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 commented 2 years ago

Unless it got fixed in the intervening years, https://github.com/whatwg/webidl/pull/377#issuecomment-370049394 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 commented 2 years ago

@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 commented 2 years ago

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

mfreed7 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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.