UNPKG

@nevuamarkets/poly-websockets

Version:

Plug-and-play Polymarket WebSocket price alerts

86 lines (85 loc) 3.11 kB
import { WebSocketGroup } from '../types/WebSocketSubscriptions'; import { OrderBookCache } from './OrderBookCache'; export declare class GroupRegistry { /** * Atomic mutate helper. * * @param fn - The function to run atomically. * @returns The result of the function. */ mutate<T>(fn: (groups: WebSocketGroup[]) => T | Promise<T>): Promise<T>; /** * Read-only copy of the registry. * * Only to be used in test suite. */ snapshot(): WebSocketGroup[]; /** * Find the first group with capacity to hold new assets. * * Returns the groupId if found, otherwise null. */ findGroupWithCapacity(newAssetLen: number, maxPerWS: number): string | null; /** * Get the indices of all groups that contain the asset. * * Returns an array of indices. */ getGroupIndicesForAsset(assetId: string): number[]; /** * Check if any group contains the asset. */ hasAsset(assetId: string): boolean; /** * Find the group by groupId. * * Returns the group if found, otherwise undefined. */ findGroupById(groupId: string): WebSocketGroup | undefined; /** * Atomically remove **all** groups from the registry and return them so the * caller can perform any asynchronous cleanup (closing sockets, etc.) * outside the lock. * * Returns the removed groups. */ clearAllGroups(): Promise<WebSocketGroup[]>; /** * Add new asset subscriptions. * * – Ignores assets that are already subscribed. * – Either reuses an existing group with capacity or creates new groups (size ≤ maxPerWS). * – If appending to a group: * - A new group is created with the updated assetIds. * - The old group is marked for cleanup. * - The group is added to the list of groups to connect. * * @param assetIds - The assetIds to add. * @param maxPerWS - The maximum number of assets per WebSocket group. * @returns An array of *new* groupIds that need websocket connections. */ addAssets(assetIds: string[], maxPerWS: number): Promise<string[]>; /** * Remove asset subscriptions from every group that contains the asset. * * It should be only one group that contains the asset, we search all of them * regardless. * * Returns the list of assetIds that were removed. */ removeAssets(assetIds: string[], bookCache: OrderBookCache): Promise<string[]>; /** * Disconnect a group. */ disconnectGroup(group: WebSocketGroup): void; /** * Check status of groups and reconnect or cleanup as needed. * * – Empty groups are removed from the global array and returned. * – Dead (but non-empty) groups are reset so that caller can reconnect them. * – Pending groups are returned so that caller can connect them. * * Returns an array of group IDs that need to be reconnected, after cleaning up empty and cleanup-marked groups. */ getGroupsToReconnectAndCleanup(): Promise<string[]>; }