Async Cancellables / AsyncCooldownQueue
Asynchronous cooldown queue class provides cooldown control and supports cancellation tokens.
Table of contents
Prerequisites
This project requires NodeJS (version 18 or later) and NPM.
Installation
To install and set up the library, run:
$ npm install @async-cancellables/async-cooldown-queue
Example
Function that can send a message no more than once in 2 seconds. The function will wait for the cooldown to end before sending the message. If ct
is provided, the function will throw if the cancellation token is cancelled before the cooldown ends and quit the cooldown queue.
import AsyncCooldownQueue from '@async-cancellables/async-cooldown-queue';
const cooldownQueue = new AsyncCooldownQueue(2000);
async sendMessage(text, ct = null) {
await cooldownQueue.wait(ct);
socket.emit('message', text);
}
API
Creating queue
new AsyncCooldownQueue()
creates new queue.
Waiting for cooldown
There are two wait methods:
-
wait(ct = null)
waits for the cooldown to end. Ifct
is provided, the function will throw if the cancellation token is cancelled before the cooldown ends and quit the cooldown queue. -
waitTime(time, ct = null)
same aswait
but waits fortime
milliseconds instead of the default cooldown time.
Getting waiters information
-
waitersPresent
returnstrue
if there are waiters in the queue -
waitersCount
returns the number of waiters in the queue -
timeUntilAvailable
returns the time in milliseconds until the queue can immediately allow a waiter to proceed
Authors
- vuwuv - Initial work - vuwuv
License
[MIT License] © vuwuv