import { PERFORMANCE_CONFIG } from '../constants/perpsConfig.js'; // Rapid perps market switching (candle bridge + activity tab burst) drives // duplicate REST calls against api.hyperliquid.xyz and occasionally trips 429. // This helper collapses concurrent identical calls into one in-flight promise // and serves a short TTL cache to absorb the burst. Explicit refresh bypasses // the cache so user-initiated refetch still re-runs. // // Lives at the service layer (MarketDataService) rather than per-hook so every // current and future caller — hooks, controller, aggregated provider — is // deduped automatically. Centralized at MarketDataService because it is // already a single choke point. type CacheEntry = { value: TValue; expiresAt: number; }; const inflight = new Map>(); const cache = new Map>(); export type CoalesceOptions = { /** * Cache TTL in milliseconds. Defaults to * {@link PERFORMANCE_CONFIG.PerpsRestCoalesceTtlMs}. */ ttlMs?: number; /** * Bypass the cache and any in-flight promise. The fresh result will be * written back to the cache under the same key. */ forceRefresh?: boolean; }; /** * Coalesce an idempotent perps REST call. * * - If a fresh cached value exists for `key`, it is returned immediately. * - If an in-flight promise exists for `key`, it is shared with the caller. * - Otherwise, `fetcher` runs once; the result populates the cache for `ttlMs`. * * `forceRefresh` skips both cache and in-flight dedup. * * @param key - Stable cache key identifying this logical REST call. * @param fetcher - Thunk that performs the REST call when no cache/inflight hit. * @param options - Optional overrides for TTL and forceRefresh behavior. * @returns The fetched (or cached) value. */ export function coalescePerpsRestRequest( key: string, fetcher: () => Promise, options: CoalesceOptions = {}, ): Promise { const ttlMs = options.ttlMs ?? PERFORMANCE_CONFIG.PerpsRestCoalesceTtlMs; const forceRefresh = options.forceRefresh ?? false; if (forceRefresh) { cache.delete(key); } else { const now = Date.now(); const cached = cache.get(key) as CacheEntry | undefined; if (cached) { if (cached.expiresAt > now) { return Promise.resolve(cached.value); } // Evict expired entry so callers with per-call-unique keys (e.g. // CandleStreamChannel historical paging with per-page endTime) do // not accumulate dead blobs for the life of the process. cache.delete(key); } const existing = inflight.get(key) as Promise | undefined; if (existing !== undefined) { return existing; } } const run = fetcher().then( (value) => { // Only the currently-tracked in-flight promise writes to cache. A stale // in-flight (e.g. one that was racing a later forceRefresh=true caller) // must not clobber the fresh value once it finally resolves. if (inflight.get(key) === run) { cache.set(key, { value, expiresAt: Date.now() + ttlMs }); inflight.delete(key); } return value; }, (error) => { if (inflight.get(key) === run) { inflight.delete(key); } throw error; }, ); inflight.set(key, run); return run; } /** * Test-only: wipe all cached entries and in-flight promises. */ export function resetPerpsRestCacheForTests(): void { cache.clear(); inflight.clear(); }