type StatusCacheEntry = { expiresAt: number value: boolean } const STATUS_CACHE_TTL = 3 * 60 * 1000 const existsStatusCache = new Map() const existsStatusRequests = new Map>() const subscribeStatusCache = new Map() const subscribeStatusRequests = new Map>() function getCachedValue(cache: Map, key: string): boolean | undefined { const entry = cache.get(key) if (!entry) { return undefined } if (entry.expiresAt <= Date.now()) { cache.delete(key) return undefined } return entry.value } function setCachedValue(cache: Map, key: string, value: boolean) { cache.set(key, { expiresAt: Date.now() + STATUS_CACHE_TTL, value, }) } async function resolveCachedStatus( cache: Map, requests: Map>, key: string, loader: () => Promise, ): Promise { const cachedValue = getCachedValue(cache, key) if (cachedValue !== undefined) { return cachedValue } const currentRequest = requests.get(key) if (currentRequest) { return currentRequest } const request = loader() .then(value => { setCachedValue(cache, key, value) return value }) .finally(() => { requests.delete(key) }) requests.set(key, request) return request } export function getCachedMediaExistsStatus(key: string, loader: () => Promise) { return resolveCachedStatus(existsStatusCache, existsStatusRequests, key, loader) } export function setCachedMediaExistsStatus(key: string, value: boolean) { setCachedValue(existsStatusCache, key, value) } export function getCachedMediaSubscribeStatus(key: string, loader: () => Promise) { return resolveCachedStatus(subscribeStatusCache, subscribeStatusRequests, key, loader) } export function setCachedMediaSubscribeStatus(key: string, value: boolean) { setCachedValue(subscribeStatusCache, key, value) }