/** * Abstract base class for all connection types */ import { type IncomingMessage, type OutgoingMessage } from "../../messages/types"; import { ConnectionStatus, type DisconnectionDetails, type OnDisconnectCallback, type OnMessageCallback, type OnStatusChangeCallback, type OnOpenCallback, type OnErrorCallback } from "./types"; export declare abstract class BaseConnection { /** * Unique identifier for the current conversation */ abstract readonly conversationId: string; /** * Current connection status */ protected status: ConnectionStatus; /** * Queue to store messages received before callback is registered */ protected queue: IncomingMessage[]; /** * Event emitter to handle events */ private eventEmitter; /** * Disconnection details (null if still connected) */ protected disconnectionDetails: DisconnectionDetails | null; protected onErrorCallback: OnErrorCallback | null; protected onDisconnectCallback: OnDisconnectCallback | null; protected onMessageCallback: OnMessageCallback | null; protected onOpenCallback: OnOpenCallback | null; protected onStatusChangeCallback: OnStatusChangeCallback | null; /** * Close the connection */ abstract close(): void; /** * Send a message through the connection */ abstract sendMessage(message: OutgoingMessage): void; /** * Get current connection status */ getStatus(): ConnectionStatus; /** * Register callback for connection open events */ onOpen(callback: OnOpenCallback): void; /** * Register callback for incoming messages */ onMessage(callback: OnMessageCallback): void; /** * Register callback for disconnection events */ onDisconnect(callback: OnDisconnectCallback): void; /** * Register callback for error events */ onError(callback: OnErrorCallback): void; /** * Register a listener for a specific event */ on(event: string, listener: (...args: unknown[]) => void): void; /** * Remove a listener for a specific event */ off(event: string, listener: (...args: unknown[]) => void): void; /** * Emit an event */ emit(event: string, ...args: unknown[]): void; /** * Register callback for status changes */ onStatusChange(callback: OnStatusChangeCallback): void; /** * Protected method to handle disconnection * Ensures disconnect callback is only invoked once */ protected disconnect(details: DisconnectionDetails): void; /** * Protected method to handle incoming messages * Queues messages if no callback is registered yet * message type can be extended to include more types of messages */ protected handleMessage(message: IncomingMessage): void; /** * Protected method to update connection status */ protected updateStatus(status: ConnectionStatus): void; /** * Check if the connection is connected */ abstract isConnected(): boolean; /** * handle connection established event */ abstract handleConnected(): void; }