import RequestQueue from "./RequestQueue.js"; /** * An extension of `RequestQueue` that adds a concept of "subscribers," which may share references to a single request * or cancel their subscription without disrupting the request for other subscribers. */ export default class SubscribableRequestQueue { private queue; /** The next unused subscriber ID. Increments whenever a subscriber is added. */ private nextSubscriberId; /** * Map of subscribers keyed by ID. Subscribers store a map to all their subscriptions by request key. * Subscribers are only useful as handles to cancel subscriptions early, so we only need to store rejecters here. */ private subscribers; /** Map from "inner" request (managed by `queue`) to "outer" promises generated per-subscriber. */ private requests; /** * Since `SubscribableRequestQueue` wraps `RequestQueue`, its constructor may either take the same arguments as the * `RequestQueue` constructor and create a new `RequestQueue`, or it may take an existing `RequestQueue` to wrap. */ constructor(maxActiveRequests?: number, maxLowPriorityRequests?: number); constructor(inner: RequestQueue); /** Resolves all subscriptions to request `key` with `value` */ private resolveAll; /** Rejects all subscriptions to request `key` with `reason` */ private rejectAll; /** Adds a new request subscriber. Returns a unique ID to identify this subscriber. */ addSubscriber(): number; /** * Queues a new request, or adds a subscription if the request is already queued/running. * * If `subscriberId` is already subscribed to the request, this rejects the existing promise and returns a new one. */ addRequest(key: string, subscriberId: number, requestAction: () => Promise, lowPriority?: boolean, delayMs?: number): Promise; /** * Rejects a subscription and removes it from the list of subscriptions for a request, then cancels the underlying * request if it is no longer subscribed and is not running already. */ private rejectSubscription; /** Cancels a request subscription, and cancels the underlying request if it is no longer subscribed or running. */ cancelRequest(key: string, subscriberId: number, cancelReason?: unknown): boolean; /** Removes a subscriber and cancels its remaining subscriptions. */ removeSubscriber(subscriberId: number, cancelReason?: unknown): void; /** Returns whether a request with the given `key` is running or waiting in the queue */ hasRequest(key: string): boolean; /** Returns whether a request with the given `key` is running */ requestRunning(key: string): boolean; /** Returns whether a subscriber with the given `subscriberId` exists */ hasSubscriber(subscriberId: number): boolean; /** Returns whether a subscriber with the given `subscriberId` is subscribed to the request with the given `key` */ isSubscribed(subscriberId: number, key: string): boolean; }