/** * Small shared utilities the client's layers build on: {@link promise} — the * constructor for the {@link PromiseLike} thenables the public API resolves * with — and {@link defer}, the async-scheduling primitive it sits on; * {@link emitter}, the one-to-many fan-out for recurring events; * {@link retryWait}, the retry curve every retry in the client waits on; and * {@link has} / {@link isEmpty}, the prototype-safe object helpers every * `for...in` in the package filters through. * * We resolve with the built-in `PromiseLike` (declared in `lib.es5`, so no * `Promise` runtime is implied): it is the minimal thenable the language's * await / promise-resolution procedure needs. On a modern client `await x` * works because the native `Promise` duck-types `x.then` and adopts it; an ES5 * client with no `Promise` at all calls `.then(onFulfilled, onRejected)` * directly — the same method serves both, with none of the combinators. */ /** * Build a {@link PromiseLike} from an `executor` that settles it exactly once — * the bridge from a callback/event source (a socket message, a parked request) * to the await-able world. Mirrors the `Promise` executor: `resolve` with a * value (or another thenable, which is adopted) or `reject` with an error; a * throw from the executor rejects. `then` returns a fresh thenable, so calls * chain and a thrown error propagates without manual wiring. Settlement and * every reaction are delivered via {@link defer}, so no caller is ever called * back synchronously. */ export declare function promise(executor: (resolve: (value: T | PromiseLike) => void, reject: (error: unknown) => void) => void): PromiseLike; /** * Invoke `fn` on a later tick, never synchronously within the caller — so a * call's outcome is delivered with uniform (asynchronous) timing whether the * work was a no-op, an immediate failure, or a real round-trip (the "don't * release Zalgo" rule). `setTimeout(0)` is the portable choice on the ES5 / * HbbTV baseline, which has no microtask queue and no `Promise`. */ export declare function defer(fn: () => void): void; /** * `Object.prototype.hasOwnProperty`, callable on any object. Every `for...in` * in this package filters through it: on a publisher page a third-party script * may have added enumerable properties to `Object.prototype`, and an unguarded * loop would iterate those too. */ export declare function has(object: object, key: string): boolean; /** True when `object` has no own enumerable keys. */ export declare function isEmpty(object: object): boolean; /** * A one-to-many fan-out for a recurring event: `add` a listener (getting back an * unsubscribe), or `emit` a value to every current listener. `emit` iterates a * snapshot, so a listener may unsubscribe — or the set otherwise change — * mid-emit without disturbing the in-flight pass. (A *one-shot* terminal signal * is a {@link PromiseLike} instead — see {@link promise}.) */ export type Emitter = { /** Register `listener`; returns a function that removes it. */ add(listener: (value: T) => void): () => void; /** Call every current listener with `value`, over a snapshot of the set. */ emit(value: T): void; /** How many listeners are currently registered. */ size(): number; }; /** Build an empty {@link Emitter}. */ export declare function emitter(): Emitter; /** * The wait, in milliseconds, before the `retry`th retry in a row of one thing — * a socket to reconnect, a request to resend, a session to reopen: 500 ms for * the first, doubling to a 10 s ceiling. Each wait is spread at random over * half to one-and-a-half times its step: every client that lost the same * server at the same instant would otherwise retry in lockstep, and arrive * together again at each step of the curve. */ export declare function retryWait(retry: number): number;