DecayingDeque
A decaying deque is a special kind of doubly linked list that serves as a queue for a special kind of nodes, called drifts.
A decaying deque has a worker function that spawns a task for each element that is added to the queue. This task then gets wrapped into a drift. The drifts are then the actual elements (aka. links) in the queue.
In addition, the decaying deque runs a timer that purges old elements from the queue. This period of time is determined by the task
.
When a task completes or exceeds its timeout, the corresponding drift is removed from the queue. As a result, only drifts with pending tasks are contained in the queue at all times.
When a tasks completes with failure (reject
s or exceeds the timeout), the respective handler (catch
or catch
) is called.
The decaying deque has its name from the observation that new elements are appended to the tail, and the old elements are removed at arbitrary positions in the queue whenever a task completes, hence, the queue seems to decay.
Type Parameters
Y
Y
R
R = unknown
Constructors
DecayingDeque(
taskTimeout: number,
worker: (t: Y) => Promise<void>,
concurrency: boolean | number,
catchError: (err: R, elem: Y) => void | Promise<void>,
catchTimeout: (t: Y, task: Promise<void>) => void,
);
Creates a new decaying queue with the given parameters.
Properties
concurrency
readonly concurrency: number;
Number of currently pending tasks that we strive for (add
calls will resolve only after the number of pending tasks falls below this value.
In the context of grammy
, it is possible to await
calls to add
to determine when to fetch more updates.
Methods
add
Adds the provided elements to the queue and starts tasks for all of them immediately. Returns a Promise
that resolves with concurrency
once this value becomes positive.
empty
empty(): Promise<void>;
capacity
capacity(): Promise<number>;
Returns a Promise
that resolves with concurrency
once this value becomes positive. Use await queue
to wait until the queue has free space again.
length (getter)
get length();
Number of pending tasks in the queue. Equivalent to this
(but much more efficient).
pendingTasks
pendingTasks(): Y[];
Creates a snapshot of the queue by computing a list of those elements that are currently being processed.