Dom: Symbols should be allowed as Event types.

Created on 22 Sep 2016  路  6Comments  路  Source: whatwg/dom

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.

additioproposal needs implementer interest events

Most helpful comment

Something like

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

All 6 comments

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.

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.

Something like

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

^ Yes, that's exactly it.

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

It's hard to say:

  • EventTarget is very new and people mostly use it for AbortController, the only feature requests I got is to "add AbortSignal to more APIs" and provide tooling to make it more approachable and debuggable
  • Node's EventEmitter already allows dispatching symbols for at least a few years. I can find some examples (e.g. https://github.com/webvalera96/currency-rates/blob/99a5e6caf36e17482faa064656b2d546707a9e7d/lib/HTTPClient.js#L31 ) that do this but it seems to be pretty rare and uncommon.
Was this page helpful?
0 / 5 - 0 ratings

Related issues

a-tarasyuk picture a-tarasyuk  路  3Comments

AMorgaut picture AMorgaut  路  13Comments

domenic picture domenic  路  12Comments

saschanaz picture saschanaz  路  5Comments

annevk picture annevk  路  5Comments