UNPKG

thorish

Version:

This is a library of useful JS concepts and data structures for Node and the browser. It it, unashamedly, a dumping ground for code needed by [@samthor](https://twitter.com/samthor)'s projects.

37 lines (36 loc) 1.72 kB
export type WebSocketSupportedPayload = string | ArrayBufferLike | Blob | ArrayBufferView; export type PrepMessage<T> = T | Promise<T> | (() => T | Promise<T>); /** * Helper which returns a method that sends data over the given {@link WebSocket}, including queuing until it is connected. * This method returns `true` if the data was sent, or ever has a chance of being sent (i.e., not closed/closing). * * The passed message can be a `Promise`, or a function which returns a `Promise`. * * The data is sent without being transformed (i.e., no {@link JSON.stringify}), but you can pass this as the second arg if it's important to you. */ export declare function socketQueueSend<T = WebSocketSupportedPayload>(socket: WebSocket | Promise<WebSocket>, transformMessage?: (m: T) => WebSocketSupportedPayload | undefined): (message: PrepMessage<T>) => boolean; export type SocketConnectArg = { /** * Signal that, if specified, closes the {@link WebSocket} when aborted. */ signal?: AbortSignal; /** * The delay to wait for before opening the socket. */ delay?: number; /** * Any protocols (second arg of {@link WebSocket} constructor). */ protocols?: string | string[]; /** * If specified, automatically added as the "message" handler. */ message?: (e: MessageEvent) => void; }; /** * Helper which prepares a {@link WebSocket} in a {@link Promise} that only resolves when it is ready. * * Additionally, accepts a {@link AbortSignal} which can be used to shut down the socket early. * This will cause the method to throw if already aborted. */ export declare function socketConnect(url: string, opts?: SocketConnectArg): Promise<WebSocket>;