UNPKG

@tapsioss/client-socket-manager

Version:
95 lines (94 loc) 3.13 kB
import type { ClientSocketManagerOptions, DefaultEventsMap, EventNames, EventParams, EventsMap } from "./types.ts"; declare class ClientSocketManager<ListenEvents extends EventsMap = DefaultEventsMap, EmitEvents extends EventsMap = ListenEvents> { private _disposed; private _socket; private _inputListeners; constructor(uri: string, options?: Partial<ClientSocketManagerOptions>); private _attachPageEvents; private _attachSocketEvents; private _attachManagerEvents; private _detachPageEvents; private _detachSocketEvents; private _detachManagerEvents; private _handleVisibilityChange; /** * Whether the client is disposed. */ get disposed(): boolean; /** * Emits an event to the socket identified by the channel name. */ emit<Ev extends EventNames<EmitEvents>>(channel: Ev, ...args: EventParams<EmitEvents, Ev>): void; /** * A unique identifier for the session. * * `null` when the socket is not connected. */ get id(): string | null; /** * Whether the socket is currently connected to the server. */ get connected(): boolean; /** * Whether the connection state was recovered after a temporary disconnection. * In that case, any missed packets will be transmitted by the server. */ get recovered(): boolean; /** * Whether the Socket will try to reconnect when its Manager connects * or reconnects. */ get autoReconnectable(): boolean; /** * Subscribes to a specified channel with a callback function. */ subscribe<Ev extends EventNames<ListenEvents>>( /** * The name of the channel to subscribe to. */ channel: Ev, /** * The callback function to invoke when a message is received on the channel. */ cb: ListenEvents[Ev], options?: { /** * The callback function to invoke when the subscription is complete. */ onSubscriptionComplete?: (this: ClientSocketManager, channel: string) => void; /** * The `AbortSignal` to unsubscribe the listener upon abortion. */ signal?: AbortSignal; }): void; /** * Removes the listener for the specified channel. * If no callback is provided, it removes all listeners for that channel. */ unsubscribe<Ev extends EventNames<ListenEvents>>( /** * The name of the channel whose listener should be deleted. */ channel: Ev, /** * The subscriber callback function to remove. */ cb?: ListenEvents[Ev]): void; /** * Manually connects/reconnects the socket. */ connect(): void; /** * Manually disconnects the socket. * In that case, the socket will not try to reconnect. * * If this is the last active Socket instance of the Manager, * the low-level connection will be closed. */ disconnect(): void; /** * Disposes of the socket, manager, and engine, ensuring all connections are * closed and cleaned up. */ dispose(): void; } export default ClientSocketManager;