UNPKG

@akala/core

Version:
73 lines (72 loc) 4.05 kB
import { EventEmitter, type AllEventKeys, type EventListener, type SpecialEvents, type EventOptions, type EventArgs, type EventReturnType } from "../index.browser.js"; import { Subscription } from "../teardown-manager.js"; import { SocketAdapterAkalaEventMap, SocketAdapter } from "./shared.js"; import { SocketProtocolTransformer } from "./shared.transformer.js"; /** * Adapts a socket connection to handle protocol-level message transformations. * Provides an event-based interface for managing socket communication with custom * serialization/deserialization logic. * @template T The type of messages after transformation */ export declare class SocketProtocolAdapter<T> extends EventEmitter<SocketAdapterAkalaEventMap<T>> implements SocketAdapter<T> { readonly transform: SocketProtocolTransformer<T>; private readonly socket; /** * Creates a new socket protocol adapter. * @param transform Configuration object containing receive, send, and optional close handlers * @param transform.receive Function to deserialize incoming data into application messages * @param transform.send Function to serialize application messages for transmission * @param transform.close Optional function to perform protocol-specific cleanup * @param socket The underlying socket adapter to wrap */ constructor(transform: SocketProtocolTransformer<T>, socket: SocketAdapter); /** * Pipes incoming messages to another socket adapter. * @param socket The destination socket to send messages to */ pipe(socket: SocketAdapter<T>): void; /** * Gets the open status of the underlying socket. */ get open(): boolean; /** * Closes the socket and performs any necessary protocol cleanup. */ close(): Promise<void>; /** * Sends a message through the socket after applying the send transformation. * @param data The message to send */ send(data: T): Promise<void>; private readonly messageListeners; private messageSubscription; /** * Removes an event listener from the adapter. * @param event The event type to remove the listener from * @param handler The event handler to remove, or undefined to remove all handlers * @returns true if the listener was successfully removed */ off<const TEvent extends AllEventKeys<SocketAdapterAkalaEventMap<T>>>(event: TEvent, handler: EventListener<(SocketAdapterAkalaEventMap<T> & Partial<SpecialEvents>)[TEvent]>): boolean; /** * Registers an event listener on the adapter. * @param event The event type to listen for * @param handler The callback to invoke when the event occurs * @param options Optional event listener configuration * @returns A subscription function to unsubscribe from the event */ on<const TEvent extends AllEventKeys<SocketAdapterAkalaEventMap<T>>>(event: TEvent, handler: EventListener<(SocketAdapterAkalaEventMap<T> & Partial<SpecialEvents>)[TEvent]>, options?: EventOptions<(SocketAdapterAkalaEventMap<T> & Partial<SpecialEvents>)[TEvent]>): Subscription; /** * Registers an event listener that fires only once. * @param event The event type to listen for * @param handler The callback to invoke when the event occurs * @returns A subscription function to unsubscribe from the event */ once<const TEvent extends AllEventKeys<SocketAdapterAkalaEventMap<T>>>(event: TEvent, handler: EventListener<(SocketAdapterAkalaEventMap<T> & Partial<SpecialEvents>)[TEvent]>): Subscription; /** * Emits an event to all registered listeners. * @param event The event type to emit * @param args Arguments to pass to event listeners * @returns The result of the emit operation */ emit<const TEvent extends AllEventKeys<SocketAdapterAkalaEventMap<T>>>(event: TEvent, ...args: EventArgs<(SocketAdapterAkalaEventMap<T> & Partial<SpecialEvents>)[TEvent]>): false | EventReturnType<(SocketAdapterAkalaEventMap<T> & Partial<SpecialEvents>)[TEvent]>; }