Closed
Description
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.
Metadata
Metadata
Assignees
Labels
No labels