UNPKG

@trycourier/courier-js

Version:

A browser-safe API wrapper

100 lines (99 loc) 3.55 kB
import { CourierClientOptions } from '../client/courier-client'; import { InboxMessageEventEnvelope, ServerMessage } from '../types/socket/protocol/messages'; import { CourierSocket } from './courier-socket'; /** Application-layer implementation of the Courier WebSocket API for Inbox messages. */ export declare class CourierInboxSocket extends CourierSocket { /** * The default interval in milliseconds at which to send a ping message to the server * if no other message has been received from the server. * * Fallback when the server does not provide a config. */ private static readonly DEFAULT_PING_INTERVAL_MILLIS; /** * The default maximum number of outstanding pings before the client should * close the connection and retry connecting. * * Fallback when the server does not provide a config. */ private static readonly DEFAULT_MAX_OUTSTANDING_PINGS; /** * The interval ID for the ping interval. * * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/setInterval */ private pingIntervalId; /** * The list of message event listeners, called when a message event is received * from the Courier WebSocket server. */ private messageEventListeners; /** Server-provided configuration for the client. */ private config; /** * The transaction manager, used to track outstanding requests and responses. */ private readonly pingTransactionManager; constructor(options: CourierClientOptions); onOpen(_: Event): Promise<void>; onMessageReceived(data: ServerMessage): Promise<void>; onClose(_: CloseEvent): Promise<void>; onError(_: Event): Promise<void>; /** * Sends a subscribe message to the server. * * Subscribes to all events for the user. */ sendSubscribe(): void; /** * Sends an unsubscribe message to the server. * * Unsubscribes from all events for the user. */ sendUnsubscribe(): void; /** * Adds a message event listener, called when a message event is received * from the Courier WebSocket server. * * @param listener The listener function * @returns A function that can be called to remove this specific listener */ addMessageEventListener(listener: (message: InboxMessageEventEnvelope) => void): () => void; /** * Send a ping message to the server. * * ping/pong is implemented at the application layer since the browser's * WebSocket implementation does not support control-level ping/pong. */ private sendPing; /** * Send a pong response to the server. * * ping/pong is implemented at the application layer since the browser's * WebSocket implementation does not support control-level ping/pong. */ private sendPong; /** * Send a request for the client's configuration. */ private sendGetConfig; /** * Restart the ping interval, clearing the previous interval if it exists. */ private restartPingInterval; private clearPingInterval; private get pingInterval(); private get maxOutstandingPings(); private setConfig; /** * Removes all message event listeners. */ private clearMessageEventListeners; /** * Remove the message event listener specified. * * This is the same listener function passed to {@link addMessageEventListener}. */ private removeMessageEventListener; private static isInboxMessageEvent; }