mqrpc
Version:
💫 Easy RPC over RabbitMQ
68 lines (67 loc) • 2.38 kB
TypeScript
/// <reference types="node" />
export declare class TimeoutExpired extends Error {
timeout: Timeout;
constructor(timeout: Timeout);
}
export interface Timeout {
id: string;
length: number;
}
export interface ActiveTimeout extends Timeout {
handle: NodeJS.Timer;
}
export interface Entry {
readonly promise: Promise<any>;
readonly reject: (err: Error) => void;
readonly timeouts: Map<string, ActiveTimeout>;
}
export default class Timer {
protected entries: Map<string, Entry>;
constructor();
/**
* Adds and starts the given timeouts for the given `entryId`. If an entry
* with the same ID exists, timeouts will be appended to it. Otherwise, a new
* entry is created.
*
* The promise returned will never resolve, but it will reject when one of the
* timeouts expires.
*
* @param {string} entryId An ID under which to add the timeouts.
* @param {Timeout[]} ...timeouts Timeout objects, with an ID and length in ms.
* @return {Promise<void>} Rejects when a timeout expires. Never resolves.
*/
addTimeouts(entryId: string, ...timeouts: Timeout[]): Promise<void>;
/**
* Restarts all timeouts with the given IDs for the given entry.
*
* @param {string} entryId The ID under which the timeouts are.
* @param {string[]} ...timeoutIds The IDs for timeouts to restart.
*/
restartTimeouts(entryId: string, ...timeoutIds: string[]): void;
/**
* Stops and removes the timeouts with the given IDs from the given entry.
*
* @param {string} entryId The ID under which the timeouts are.
* @param {string[]} ...timeoutIds The IDs for timeouts to stop & remove.
*/
removeTimeouts(entryId: string, ...timeoutIds: string[]): void;
/**
* Removes an entry. This stops and removes all its timeouts as well.
*
* @param {string} entryId The ID for the entry to remove.
*/
remove(entryId: string): void;
/**
* Removes every entry and every timeout.
*/
clear(): void;
/**
* @protected
*
* Returns an existing Entry or creates a new one.
*
* @param {string} entryId The Entry's ID to create or fetch.
* @return {Entry} A new or existing Entry
*/
protected ensureEntry(entryId: string): Entry;
}