Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 36 additions & 46 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
export type EventType = 'open' | 'message' | 'error' | 'close';

export interface BaseEvent {
type: string;
}

export interface MessageEvent {
type: 'message';
data: string | null;
lastEventId: string | null;
url: string;
}

export interface OpenEvent {
type: 'open';
}

export interface CloseEvent {
type: 'close';
}

export interface TimeoutEvent {
type: 'timeout';
}

export interface ErrorEvent {
type: 'error';
message: string;
xhrState: number;
xhrStatus: number;
}
export type Events = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errors of type 'timeout' are also emitted via the 'error' event. There is no dedicated 'timeout' event. So this type should be changed to:

export type Events = {
  message: {
    type: 'message';
    data: string | null;
    lastEventId: string | null;
    url: string;
  };
  open: {
    type: 'open';
  };
  close: {
    type: 'close';
  };
  error: {
    type: 'error';
    message: string;
    xhrState: number;
    xhrStatus: number;
  } | {
    type: 'exception';
    message: string;
    error: Error;
  } | {
    type: 'timeout';
  };
}

message: {
type: 'message';
data: string | null;
lastEventId: string | null;
url: string;
};
open: {
type: 'open';
};
close: {
type: 'close';
};
timeout: {
type: 'timeout';
};
error: {
type: 'error';
message: string;
xhrState: number;
xhrStatus: number;
} | {
type: 'exception';
message: string;
error: Error;
};
}

export type EventType = keyof Events;

export interface CustomEvent<E extends string> {
type: E;
Expand All @@ -37,12 +35,6 @@ export interface CustomEvent<E extends string> {
url: string;
}

export interface ExceptionEvent {
type: 'exception';
message: string;
error: Error;
}

export interface EventSourceOptions {
method?: string;
timeout?: number;
Expand All @@ -53,20 +45,18 @@ export interface EventSourceOptions {
timeoutBeforeConnection?: number;
}

export type EventSourceEvent = MessageEvent | OpenEvent | CloseEvent | TimeoutEvent | ErrorEvent | ExceptionEvent;

export type EventSourceListener<E extends string = never> = (
event: CustomEvent<E> | EventSourceEvent
export type EventSourceListener<E extends EventType | string> = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be useful if the EventSourceListener type would also work without a type argument. This could easily be achieved by making E default to EventType like this:

export type EventSourceListener<E extends EventType | string = EventType> = (

event: E extends EventType ? Events[E] : CustomEvent<E>
) => void;

declare class EventSource<E extends string = never> {
declare class EventSource<E extends EventType | string = EventType> {
constructor(url: URL | string, options?: EventSourceOptions);
open(): void;
close(): void;
addEventListener(type: E | EventType, listener: EventSourceListener<E>): void;
removeEventListener(type: E | EventType, listener: EventSourceListener<E>): void;
removeAllEventListeners(type?: E | EventType): void;
dispatch(type: E | EventType, data: E | EventSourceEvent): void;
addEventListener<T extends (E | EventType)>(type: T, listener: EventSourceListener<T>): void;
removeEventListener<T extends (E | EventType)>(type: T, listener: EventSourceListener<T>): void;
removeAllEventListeners<T extends (E | EventType)>(type?: T): void;
dispatch<T extends (E | EventType)>(type: T, data: T extends EventType ? Events[T] : CustomEvent<T>): void;
}

export default EventSource;