36 lines
977 B
Markdown
36 lines
977 B
Markdown
|
|
# Attq!
|
||
|
|
|
||
|
|
An async tiny task queue (and related utilities), with zero dependencies. Attq
|
||
|
|
provides a data structure that executes an asynchronous callback sequentially on
|
||
|
|
a flexible list. It is designed to facilitate in-order client-server message
|
||
|
|
passing with bells and whistles including batching, throttling, and configurable
|
||
|
|
retries. **Work in progress.**
|
||
|
|
|
||
|
|
## Example
|
||
|
|
|
||
|
|
```typescript
|
||
|
|
import { AsyncTaskQueue } from "attq";
|
||
|
|
|
||
|
|
const q = new AsyncTaskQueue<number>(
|
||
|
|
(nums: number[]) => fetch(`/refine?macrodata=${nums.join(",")}`),
|
||
|
|
)
|
||
|
|
// Handler will receive up to 4 items per batch.
|
||
|
|
.batchSize(4)
|
||
|
|
// Handler will be called at most once per 100 milliseconds.
|
||
|
|
.throttleMs(100);
|
||
|
|
|
||
|
|
// Add items to the queue.
|
||
|
|
for (let n = 0; n < 1000; n++) {
|
||
|
|
q.push(n);
|
||
|
|
}
|
||
|
|
|
||
|
|
await new Promise((resolve) => setTimeout(resolve, 5000));
|
||
|
|
|
||
|
|
console.log(q.size()); // Should display >=800 items remaining.
|
||
|
|
|
||
|
|
// Stop processing items if any causes an exception.
|
||
|
|
q.onError(() => {
|
||
|
|
q.drain();
|
||
|
|
});
|
||
|
|
```
|