UNPKG

nav-connect

Version:

Nav Online Invoice connection module

32 lines (31 loc) 1.45 kB
export declare class RequestQueue { private _minIntervalMs; private _lastRequestTime; private _queue; private _processing; constructor(minIntervalMs: number); markRequestSent(): void; /** * Queues `fn` so it runs at most once per `minIntervalMs` (measured from * the last actually sent request). The returned promise settles with * `fn`'s value or rejection: each queued task wires `resolve`/`reject` * onto `fn()` via `run().then(resolve, reject)`, so the caller always * receives the outcome even though the task itself never rejects. * When the queue is idle, `processQueue()` is started to drain it. */ enqueue<T>(fn: () => Promise<T>): Promise<T>; /** * Drains the queue as long as it is non-empty, then resets `_processing` * so a future `enqueue` restarts the loop. State flow: * - idle (`_processing === false`): the next `enqueue` flips the flag and * starts `processQueue` without awaiting it. * - processing: further `enqueue` calls only append tasks; the running * loop picks them up. * - each task settles its own promise (see `enqueue`), so the try/catch * below is defensive only: a task must never stop the loop or strand * `_processing` as `true`. * The `finally` guarantees `_processing` is cleared even if a task throws. */ private processQueue; static sleep(ms: number): Promise<void>; }