2.1.0 • Published 3 years ago
@async-cancellables/async-cooldown-queue v2.1.0
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-queueExample
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. Ifctis 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 aswaitbut waits fortimemilliseconds instead of the default cooldown time.
Getting waiters information
waitersPresentreturnstrueif there are waiters in the queuewaitersCountreturns the number of waiters in the queuetimeUntilAvailablereturns the time in milliseconds until the queue can immediately allow a waiter to proceed
Authors
- vuwuv - Initial work - vuwuv
License
MIT License © vuwuv