Skip to content

EventSource events are always provided a MessageEvent #736

Closed
@Zirak

Description

@Zirak

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions