@nktkas/hyperliquid
Version:
Hyperliquid API SDK for all major JS runtimes, written in TypeScript.
57 lines • 3 kB
TypeScript
import type { ReconnectingWebSocket } from "@nktkas/rews";
import type { HyperliquidEventTarget } from "./_hyperliquidEventTarget.js";
import { WebSocketAsyncRequest } from "./_postRequest.js";
/** WebSocket subscription with failure signal. */
export interface WebSocketSubscription {
/** Removes the event listener and unsubscribes from the event channel. */
unsubscribe(): Promise<void>;
/** [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) that is aborted if the subscription fails to restore after reconnection. */
failureSignal: AbortSignal;
}
/** Internal state for managing a subscription. */
interface SubscriptionState {
/** Map of event listeners to their unsubscribe functions. */
listeners: Map<(data: CustomEvent) => void, () => Promise<void>>;
/** Promise tracking the subscription request. */
promise: Promise<unknown>;
/** Whether the subscription request has completed. */
promiseFinished: boolean;
/** Controller to signal subscription failure. */
failureController: AbortController;
}
/**
* Manages WebSocket subscriptions to Hyperliquid event channels.
* Handles subscription lifecycle, resubscription on reconnect, and cleanup.
*/
export declare class WebSocketSubscriptionManager {
/** Enable automatic re-subscription to Hyperliquid subscription after reconnection. */
resubscribe: boolean;
protected _socket: ReconnectingWebSocket;
protected _wsRequester: WebSocketAsyncRequest;
protected _hlEvents: HyperliquidEventTarget;
protected _subscriptions: Map<string, SubscriptionState>;
constructor(socket: ReconnectingWebSocket, wsRequester: WebSocketAsyncRequest, hlEvents: HyperliquidEventTarget, resubscribe: boolean);
/**
* Subscribes to a Hyperliquid event channel.
* Sends a subscription request to the server and listens for events.
*
* @param channel - The event channel to listen to.
* @param payload - A payload to send with the subscription request.
* @param listener - A function to call when the event is dispatched.
*
* @returns A promise that resolves with a {@link WebSocketSubscription} object to manage the subscription lifecycle.
*
* @throws {WebSocketRequestError} - An error that occurs when a WebSocket request fails.
*/
subscribe<T>(channel: string, payload: unknown, listener: (data: CustomEvent<T>) => void): Promise<WebSocketSubscription>;
/** Resubscribe to all existing subscriptions if auto-resubscribe is enabled. */
protected _handleOpen(): void;
/** Cleanup subscriptions if resubscribe is disabled or socket is terminated. */
protected _handleClose(): void;
/** Checks if a payload contains a user parameter. */
protected _hasUserParam(payload: unknown): boolean;
/** Counts the number of active subscriptions with user parameter. */
protected _countUserSubscriptions(): number;
}
export {};
//# sourceMappingURL=_subscriptionManager.d.ts.map