Open ronag opened 3 years ago
@benjamingr @jasnell
we are not allowed to do this, WebIDL is very specific about that fact and we're not going to be able to push such a huge change the EventTarget spec (almost everything in the web basically is an EventTarget from a div
to an XMLHttpRequest
).
Use the static helpers - they already support both EventTarget
and EventEmitter
explicitly for this purpuse:
events.once(emitterOrTarget, 'eventName'); // does the right thing, even with regards to error handling
events.on(emitterOrTarget, 'eventName'); // async iterator, ditto
They both even take a signal parameter.
I like to implement api's to support both EventEmitter and AbortSignal.
Also - we have in the past supported both in some cancellation APIs (made it intentionally possible to pass an EventEmitter and avoided branding checks) - but I think AbortSignal is probably the future in terms of what we're going with.
(There is also NodeEventTarget by the way :D )
I will ask in whatwg/dom just in case
I'm happy with the answer if you wish to close.
Do we want to add a section to the docs discussing this (code supporting both EventTarget and EventEmitter) and add a recommendation to use on/once
for that case?
Do we want to add a section to the docs discussing this (code supporting both EventTarget and EventEmitter) and add a recommendation to use
on/once
for that case?
I'd be happy to try and help writing some docs, although i'll only have time in a few days, in the weekend.
@Linkgoron ... are you still interested in working on the doc updates for this?
@jasnell whoops, totally forgot about this. I'll take care of this.
Would if make sense to add on/off as aliases to addEventListener/removeEventListener?
I'm ending up writing quite a bit of code that checks for either since usually I like to implement api's to support both EventEmitter and AbortSignal. Having the aliases would make code much easier and less error prone.