@nktkas/hyperliquid
Version:
Hyperliquid API SDK for all major JS runtimes, written in TypeScript.
56 lines (55 loc) • 2.9 kB
TypeScript
/**
* Subscription lifecycle manager: tracks listeners per subscription payload,
* resubscribes on reconnect, and enforces per-connection subscription limits.
*
* @module
*/
/// <amd-module name="file:///home/runner/work/hyperliquid/hyperliquid/src/transport/websocket/_subscriptionManager.ts" />
import { ReconnectingWebSocket } from "@nktkas/rews";
import type { ISubscription } from "../_base.js";
import type { HyperliquidEventTarget } from "./_events.js";
import { WebSocketDispatcher, WebSocketRequestError } from "./_dispatcher.js";
/** Tracks listeners per subscription payload, resubscribes on reconnect, enforces server-side limits. */
export declare class WebSocketSubscriptionManager {
/** Enable automatic re-subscription to Hyperliquid subscription after reconnection. */
resubscribe: boolean;
private readonly _socket;
private readonly _dispatcher;
private readonly _hlEvents;
private _subscriptions;
constructor(socket: ReconnectingWebSocket, dispatcher: WebSocketDispatcher, hlEvents: HyperliquidEventTarget, resubscribe: boolean);
/**
* Subscribes to a Hyperliquid event channel.
*
* @param options.signal Stops waiting for the confirmation and detaches the listener.
* @param options.onError Callback invoked at most once, when an already confirmed subscription fails:
* - the server rejects a re-subscription after a reconnect;
* - the connection is permanently terminated;
* - the connection goes down while re-subscription is disabled.
*
* Failures before the confirmation reject the `subscribe()` promise instead.
* After the callback fires, the subscription is removed and no further events or errors follow.
*
* @throws {WebSocketRequestError} When the subscription request fails or limits are exceeded.
*/
subscribe<T>(channel: string, payload: unknown, listener: (data: CustomEvent<T>) => void, options?: {
signal?: AbortSignal;
onError?: (error: WebSocketRequestError) => void;
}): Promise<ISubscription>;
/** Resubscribes to every completed subscription when the socket re-opens. */
private _handleOpen;
/**
* Fails every subscription when the connection stops serving them: the socket is
* terminated, or it closes while re-subscription is disabled.
*/
private _handleClose;
/**
* Removes the subscription with all its listeners, then notifies each
* confirmed listener's `onError` once. Sends nothing to the server: every
* caller deals with a subscription the server no longer serves — refused,
* or cut off by a close.
*/
private _failSubscription;
/** True when subscribing `payload` would track one user above the limit. */
private _exceedsUserLimit;
}