/** * @fileoverview Registry of in-flight media downloads. * * Two loaders that request the same URL at the same time would normally open * two connections and decode the same picture twice. This registry lets the * second loader attach to the download that is already running, so the bytes * travel through the network exactly once and every consumer receives the very * same {@link Blob} instance. */ export type MediaMirrorEntry = { /** * - The normalized registry key. */ key: string; /** * - The original source URL. */ src: string; /** * - The epoch timestamp, in milliseconds, when the download started. */ startedAt: number; /** * - The number of loaders attached to this download. */ mirrors: number; /** * - Whether the download already produced a payload. */ settled: boolean; /** * - The sequence number assigned to the next published event. */ sequence: number; /** * - Every event published so far, in order. */ events: MediaMirrorEvent[]; /** * - The loaders attached to this download. */ subscribers: Set>; /** * - Resolves with the shared blob. */ promise: Promise; /** * - Resolves the shared promise. */ resolve: (blob: Blob) => void; /** * - Rejects the shared promise. */ reject: (error: Error) => void; }; export type MediaMirrorStats = { /** * - The number of downloads currently in flight. */ pending: number; /** * - The total number of attached loaders. */ mirrors: number; }; export type MediaMirrorSnapshot = { /** * - The normalized registry key. */ key: string; /** * - The original source URL. */ src: string; /** * - The epoch timestamp, in milliseconds, when the download started. */ startedAt: number; /** * - The number of loaders attached to this download. */ mirrors: number; /** * - Whether the download already produced a payload. */ settled: boolean; }; export type MediaMirrorEvent = { /** * - The zero-based position of the event in the stream. */ seq: number; /** * - The event name. */ name: Name; /** * - The arguments the event was emitted with. */ args: Args; /** * - The high resolution timestamp of the capture. */ timestamp: number; }; export type MediaMirrorSubscriber = (event: MediaMirrorEvent) => void; /** * Describes a single in-flight download. * @template {string} Name * @template {unknown[]} Args * @typedef {Object} MediaMirrorEntry * @property {string} key - The normalized registry key. * @property {string} src - The original source URL. * @property {number} startedAt - The epoch timestamp, in milliseconds, when the download started. * @property {number} mirrors - The number of loaders attached to this download. * @property {boolean} settled - Whether the download already produced a payload. * @property {number} sequence - The sequence number assigned to the next published event. * @property {MediaMirrorEvent[]} events - Every event published so far, in order. * @property {Set>} subscribers - The loaders attached to this download. * @property {Promise} promise - Resolves with the shared blob. * @property {(blob: Blob) => void} resolve - Resolves the shared promise. * @property {(error: Error) => void} reject - Rejects the shared promise. */ /** * A read-only snapshot of the registry counters. * @typedef {Object} MediaMirrorStats * @property {number} pending - The number of downloads currently in flight. * @property {number} mirrors - The total number of attached loaders. */ /** * A read-only snapshot of a single in-flight download. * @typedef {Object} MediaMirrorSnapshot * @property {string} key - The normalized registry key. * @property {string} src - The original source URL. * @property {number} startedAt - The epoch timestamp, in milliseconds, when the download started. * @property {number} mirrors - The number of loaders attached to this download. * @property {boolean} settled - Whether the download already produced a payload. */ /** * A single event captured from the leading loader. * @template {string} Name * @template {unknown[]} Args * @typedef {Object} MediaMirrorEvent * @property {number} seq - The zero-based position of the event in the stream. * @property {Name} name - The event name. * @property {Args} args - The arguments the event was emitted with. * @property {number} timestamp - The high resolution timestamp of the capture. */ /** * Handles a broadcast event. * @template {string} Name * @template {unknown[]} Args * @callback MediaMirrorSubscriber * @param {MediaMirrorEvent} event - The published event. * @returns {void} */ /** * Deduplicates concurrent downloads that target the same URL. */ declare class TinyMediaMirror { #private; /** * The number of downloads currently in flight. * @returns {number} The number of registered downloads. */ static get size(): number; /** * The key of every in-flight download. * @returns {string[]} A new array with the registered keys. */ static get keys(): string[]; /** * A read-only snapshot of the registry counters. * @returns {MediaMirrorStats} The current counters. */ static get stats(): MediaMirrorStats; /** * Checks whether a download is already in flight for the given key. * @param {string} key - The normalized registry key. * @returns {boolean} True when a download is in flight. */ static has(key: string): boolean; /** * Reads an in-flight download without attaching to it. * @param {string} key - The normalized registry key. * @returns {MediaMirrorEntry|null} The shared entry, or null when nothing is in flight. */ static get(key: string): MediaMirrorEntry | null; /** * A read-only snapshot of every in-flight download. * @returns {MediaMirrorSnapshot[]} One record per in-flight download. */ static entries(): MediaMirrorSnapshot[]; /** * Registers a new in-flight download. When the key is already taken the * existing entry is returned, so a live download is never overwritten. * @param {string} key - The normalized registry key. * @param {string} src - The original source URL. * @returns {MediaMirrorEntry} The registered entry. * @throws {TypeError} If `key` is not a string. * @throws {TypeError} If `src` is not a string. */ static open(key: string, src: string): MediaMirrorEntry; /** * Attaches a new consumer to an in-flight download. * @param {string} key - The normalized registry key. * @returns {MediaMirrorEntry|null} The shared entry, or null when nothing is in flight. */ static attach(key: string): MediaMirrorEntry | null; /** * Removes a finished download from the registry. A download that never * produced a payload rejects its waiters instead of hanging them forever. * @param {string} key - The normalized registry key. * @returns {boolean} True when an entry was removed. */ static close(key: string): boolean; /** * Rejects every in-flight download and empties the registry. * @returns {void} */ static clear(): void; /** * Broadcasts an event to every loader attached to an in-flight download. The * event is appended to the history so a loader that attaches later still * receives the complete stream, in its original order. * @param {string} key - The normalized registry key. * @param {string} name - The event name. * @param {unknown[]} args - The arguments the event was emitted with. * @returns {number} The number of subscribers that received the event. * @throws {TypeError} If `key` is not a string. * @throws {TypeError} If `name` is not a string. * @throws {TypeError} If `args` is not an array. */ static publish(key: string, name: string, args: unknown[]): number; /** * Attaches a subscriber to an in-flight download. The entire history is * replayed synchronously, in its original order, before the handler starts * receiving live events. Because JavaScript never preempts a running * function, no live event can slip between the replay and the subscription, * so the handler observes exactly the same sequence the leader produced. * @param {string} key - The normalized registry key. * @param {MediaMirrorSubscriber} handler - The callback invoked for every event. * @returns {(() => void)|null} A function that detaches the subscriber, or null when nothing is in flight. * @throws {TypeError} If `handler` is not a function. */ static subscribe(key: string, handler: MediaMirrorSubscriber): (() => void) | null; /** * The ordered event history of an in-flight download. The array is live and * must be treated as read-only. * @param {string} key - The normalized registry key. * @returns {MediaMirrorEvent[]} The captured events, oldest first. */ static history(key: string): MediaMirrorEvent[]; } export default TinyMediaMirror; //# sourceMappingURL=TinyMediaMirror.d.mts.map