auto shifting queue based on denque
npm install tickque- You might not want to create timer for each incoming request.
- The queue is designed to align those requests into time slots.
- When each time slot is shifted, process requests in the slot in batch.
``
const Queue = require('tickque')
let queue = new Queue(1, 10, true)
queue.onShift(itemsIterator => { ... })
queue.add('shifted after 10 sec'))
queue.add('shifted after 2 sec'), 2)
`
> 0Note: It's possible to set
interval to float point but the queue uses setInterval which cannot guarantee pricise timers. There will be ms delays (depending on CPU usage) before each onShift callback due to single thread JS model. It's roughly 1 second error every 8 minutes$3
- cb will be invoked with the items iterator in the shifted time slot$3
- item: the object to be inserted into the ttl specified time slot
- ttl: tick to live -- time slot offset (positive integer)The queue grows automatically until
limit, if ttl is larger than current queue length.
This is one time O(n) operation, n equals the length of queue.
When queue is contructred with preAlloc, complexity is always O(1).
If ttl is omitted, item is added to the end of queue.$3
Returns the items in the given time slot in O(1). $3
Remove the given item in O(1).$3
Check if the item is in the queue in O(1).$3
Returns the queue length in O(1).$3
Returns iterator of all items in O(1).$3
Returns number of all items in O(1).$3
- immediate: tick at once if true, otherwise tick after interval`Start queue shifting. Note the queue starts automatically when item is added.