UNPKG

3.28 kBTypeScriptView Raw
1/** Queue configuration object. */
2export interface QueueOptions {
3 /** The queue will be flushed automatically after an inactivity of this delay in milliseconds. By default there is no automatic flushing (`null`). */
4 delay?: null | number;
5 /** When the queue exceeds the given maximum number of entries, the queue is flushed automatically. Default value is `Infinity`. */
6 max?: number;
7}
8/**
9 * Queue extending options.
10 *
11 * @typeParam T - The type of method names to be replaced by queued versions.
12 */
13export interface QueueExtendOptions<T> {
14 /** A list with method names of the methods on the object to be replaced with queued ones. */
15 replace: T[];
16 /** When provided, the queue will be flushed automatically after an inactivity of this delay in milliseconds. Default value is null. */
17 delay?: number;
18 /** When the queue exceeds the given maximum number of entries, the queue is flushed automatically. Default value of max is Infinity. */
19 max?: number;
20}
21/**
22 * Queue call entry.
23 * - A function to be executed.
24 * - An object with function, args, context (like function.bind(context, ...args)).
25 */
26declare type QueueCallEntry = Function | {
27 fn: Function;
28 args: unknown[];
29} | {
30 fn: Function;
31 args: unknown[];
32 context: unknown;
33};
34/**
35 * A queue.
36 *
37 * @typeParam T - The type of method names to be replaced by queued versions.
38 */
39export declare class Queue<T = never> {
40 /** Delay in milliseconds. If defined the queue will be periodically flushed. */
41 delay: null | number;
42 /** Maximum number of entries in the queue before it will be flushed. */
43 max: number;
44 private readonly _queue;
45 private _timeout;
46 private _extended;
47 /**
48 * Construct a new Queue.
49 *
50 * @param options - Queue configuration.
51 */
52 constructor(options?: QueueOptions);
53 /**
54 * Update the configuration of the queue.
55 *
56 * @param options - Queue configuration.
57 */
58 setOptions(options?: QueueOptions): void;
59 /**
60 * Extend an object with queuing functionality.
61 * The object will be extended with a function flush, and the methods provided in options.replace will be replaced with queued ones.
62 *
63 * @param object - The object to be extended.
64 * @param options - Additional options.
65 * @returns The created queue.
66 */
67 static extend<O extends {
68 flush?: () => void;
69 }, K extends string>(object: O, options: QueueExtendOptions<K>): Queue<O>;
70 /**
71 * Destroy the queue. The queue will first flush all queued actions, and in case it has extended an object, will restore the original object.
72 */
73 destroy(): void;
74 /**
75 * Replace a method on an object with a queued version.
76 *
77 * @param object - Object having the method.
78 * @param method - The method name.
79 */
80 replace<M extends string>(object: Record<M, () => void>, method: M): void;
81 /**
82 * Queue a call.
83 *
84 * @param entry - The function or entry to be queued.
85 */
86 queue(entry: QueueCallEntry): void;
87 /**
88 * Check whether the queue needs to be flushed.
89 */
90 private _flushIfNeeded;
91 /**
92 * Flush all queued calls
93 */
94 flush(): void;
95}
96export {};
97//# sourceMappingURL=queue.d.ts.map
\No newline at end of file