import { Websocket } from "./websocket"; /** * Events that can be fired by the websocket. */ export const WebsocketEvent = { /** Fired when the connection is opened. */ open: "open", /** Fired when the connection is closed. */ close: "close", /** Fired when the connection has been closed because of an error, such as when some data couldn't be sent. */ error: "error", /** Fired when a message is received. */ message: "message", /** Fired when the websocket tries to reconnect after a connection loss. */ retry: "retry", /** Fired when the websocket successfully reconnects after a connection loss. */ reconnect: "reconnect", } as const; /** Union of all event type strings, allowing plain strings like "open" as event types. */ export type WebsocketEvent = (typeof WebsocketEvent)[keyof typeof WebsocketEvent]; /** Provides member types (e.g. WebsocketEvent.open) for use as generic type arguments. */ // eslint-disable-next-line @typescript-eslint/no-namespace export namespace WebsocketEvent { export type open = typeof WebsocketEvent.open; export type close = typeof WebsocketEvent.close; export type error = typeof WebsocketEvent.error; export type message = typeof WebsocketEvent.message; export type retry = typeof WebsocketEvent.retry; export type reconnect = typeof WebsocketEvent.reconnect; } /*** * Details/properties of a retry-event. */ export type RetryEventDetail = { /** Number of retries that have been made since the connection was lost. */ readonly retries: number; /** Time (ms) waited since the last connection-retry. */ readonly backoff: number; /** Timestamp of when the connection was lost or undefined if the connection has never been established. */ readonly lastConnection: Date | undefined; }; /** * Properties of a reconnect-event. */ export type ReconnectEventDetail = Omit; /** * Maps websocket events to their corresponding event. */ export type WebsocketEventMap = { [WebsocketEvent.open]: Event; [WebsocketEvent.close]: CloseEvent; [WebsocketEvent.error]: Event; [WebsocketEvent.message]: MessageEvent; [WebsocketEvent.retry]: CustomEvent; [WebsocketEvent.reconnect]: CustomEvent; }; /** * Listener for websocket events. * */ export type WebsocketEventListener = ( instance: Websocket, ev: WebsocketEventMap[K], ) => unknown; export type WebsocketEventListenerParams = Parameters< WebsocketEventListener >; /** * Options for websocket events. */ export type WebsocketEventListenerOptions = EventListenerOptions & AddEventListenerOptions; /** * Listener for websocket events with options. */ export type WebsocketEventListenerWithOptions = { readonly listener: WebsocketEventListener; readonly options?: WebsocketEventListenerOptions; }; /** * Maps websocket events to their corresponding event-listeners. */ export type WebsocketEventListeners = { [K in WebsocketEvent]: WebsocketEventListenerWithOptions[]; };