import { TheLink, Tunnel, type Deserialize, type Serialize } from "@the-link/core"; /** * Private Client-side Link for one HttpServer subscription. * * Wraps one browser WebSocket with TheLink-compatible inbound and outbound * tunnels. Server messages are routed into the inbound tunnel, while outbound * tunnel events are serialized and sent back through the WebSocket. * * Event messages use a simple `[event, ...values]` envelope. * * @example * ```typescript * const socket = await client.subscribeAsync() * * socket.$inbound.subscribe("server:event", handleServerEvent) * await socket.$outbound.publish("client:event", payload) * ``` */ export default class ClientSocket extends TheLink { /** * Internal tunnel for this socket's lifecycle events. * * Publishes `unsubscribe` when the underlying WebSocket closes. */ readonly $internal: Tunnel; /** * Browser WebSocket used for bidirectional transport. */ private readonly socket; /** * Server response payload supplied during subscription establishment. */ readonly payload: Payload; private serialize; private deserialize; /** * Initialize the Client Link for one browser WebSocket subscription. * * @param socket Browser WebSocket connected to the server subscription route * @param payload Server-provided subscription response payload */ constructor(socket: WebSocket, payload: Payload); /** * Configure the serialization function used for WebSocket payloads. * * @param serialize Custom function that converts values to bytes */ setSerialize(serialize: Serialize): void; /** * Configure the deserialization function used for WebSocket payloads. * * @param deserialize Custom function that parses values from bytes */ setDeserialize(deserialize: Deserialize): void; /** * Connect another TheLink instance to this socket until it unsubscribes. * * @param theLink Source link whose events should be forwarded * @param fromPrefix Source event prefix to match * @param toPrefix Destination event prefix to apply * @returns Function that manually disconnects forwarding and cleanup */ autoJoin(theLink: TheLink, fromPrefix?: string, toPrefix?: string): () => void; /** * Close the WebSocket subscription from the client side. * * @param code WebSocket close code * @param reason Close reason sent to the server */ unsubscribe(code?: number, reason?: string): void; /** * Process a server message envelope and route it into the inbound tunnel. * * @param event Browser WebSocket message event */ private subscribeHandler; /** * Send an outbound tunnel event to the server. * * @param event Event identifier sent to the server * @param values Event payload values sent to the server */ private publishHandler; /** * Publish the socket-level unsubscribe event after WebSocket closure. * * @param event Browser WebSocket close event */ private closeHandler; }