Skip to content

EventSource events are always provided a MessageEvent #736

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

Closed
Zirak opened this issue Jul 6, 2019 · 0 comments · Fixed by #1182
Closed

EventSource events are always provided a MessageEvent #736

Zirak opened this issue Jul 6, 2019 · 0 comments · Fixed by #1182

Comments

@Zirak
Copy link

Zirak commented Jul 6, 2019

Right now, EventSource.addEventListener has the following eventName => eventType mapping:

interface EventSourceEventMap {
    "error": Event;
    "message": MessageEvent;
    "open": Event;
}

And all other events are given the Event type. However, any event can be given a MessageEvent, e.g. given the following EventSource response:

event: foobar
data: I am foo

event: open
data: Why hello there

event: error
data: Now isn't this fun

And the following code:

var source = new EventSource('https://wherever');
source.addEventListener('foobar', console.log);
source.addEventListener('open', console.log);
source.addEventListener('error', console.log);

We'll get the following console output:

open { target: EventSource, isTrusted: true, srcElement: EventSource, … }

foobar { target: EventSource, isTrusted: true, data: "I am foo", … }

open { target: EventSource, isTrusted: true, data: "Why hello there", … }

error { target: EventSource, isTrusted: true, data: "Now isn't this fun", … }

The trivial solution would be to have the signature addEventListener(type: string, listener: (this: EventSource, ev: MessageEvent) => any, ...)

(omitted for brevity that the listener can also be an EventListenerObject receiving a MessageEvent).

I can send a PR if this seems like the right solution to you, I'm however a ts novice so maybe I'm missing a better solution?

Thanks.

Alexendoo added a commit to Alexendoo/TypeScript-DOM-lib-generator that referenced this issue Oct 20, 2021
Fixes microsoft#736

The event listeners are removed and then recreated to avoid having the
existing add/removeEventListeners in addition to the new ones
Alexendoo added a commit to Alexendoo/TypeScript-DOM-lib-generator that referenced this issue Oct 21, 2021
github-actions bot pushed a commit that referenced this issue Oct 23, 2021
* Use MessageEvent in EventSource#addEventListener

Fixes #736

* Retain EventSource's original listener

* Add test for MessageEvent being the default overload
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 a pull request may close this issue.

1 participant