@cldmv/holdmytask 2.0.3 → 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +366 -176
- package/dist/hold-my-task.mjs +150 -31
- package/index.cjs +12 -3
- package/package.json +7 -6
package/README.md
CHANGED
|
@@ -1,16 +1,40 @@
|
|
|
1
1
|
# @cldmv/holdmytask
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
[](https://www.npmjs.com/package/@cldmv/holdmytask)
|
|
5
|
-
[](https://github.com/CLDMV/holdmytask/releases)
|
|
6
|
-
[](https://github.com/CLDMV/holdmytask/commits)
|
|
7
|
-
[](https://www.npmjs.com/package/@cldmv/holdmytask)
|
|
3
|
+
**@cldmv/holdmytask** is a tiny, dependency-free task queue for Node.js that executes tasks with priority ordering, concurrency control, and completion delays. It is built for asynchronous workflows with real timing requirements: rate-limited APIs, device control, UI refreshes, and anything else where _when_ a task runs matters as much as _whether_ it runs.
|
|
8
4
|
|
|
9
|
-
|
|
5
|
+
Every task can carry a priority, a scheduled start time, a timeout, an `AbortSignal`, and metadata. Per-priority concurrency limits and pre/post delays shape throughput, urgent work can bypass an active delay, and tasks that share a coalescing key collapse into a single execution whose result resolves every caller. Smart scheduling sets precise timers for the next ready task instead of polling.
|
|
10
6
|
|
|
11
|
-
|
|
7
|
+
Use it with callbacks or promises, from ESM or CommonJS, with full TypeScript definitions included.
|
|
12
8
|
|
|
13
|
-
|
|
9
|
+
> _A tiny task queue that waits until your task is ready._
|
|
10
|
+
|
|
11
|
+
[![npm version]][npm_version_url] [![npm downloads]][npm_downloads_url] [![GitHub downloads]][github_downloads_url] [![Last commit]][last_commit_url] [![npm last update]][npm_last_update_url] [![coverage]][coverage_url]
|
|
12
|
+
|
|
13
|
+
[![Contributors]][contributors_url] [![Sponsor shinrai]][sponsor_url]
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## ✨ What's New
|
|
18
|
+
|
|
19
|
+
### Latest: v2.0.5 (October 2026)
|
|
20
|
+
|
|
21
|
+
- **Task-level `postDelay` / `startDelay` work as documented** — `enqueue()` now honors the documented `postDelay` and `startDelay` task options, and a task-level completion delay is enforced even when the task's priority has no `postDelay` of its own. The old `delay` / `start` names still work as deprecated aliases and emit one `deprecation` warning per queue instance.
|
|
22
|
+
- **Safer failure reporting** — A failing callback-style task no longer crashes the process when no `error` listener is attached; the failure always reaches the callback, and the `error` event is emitted only when someone listens. Non-integer `priorities` keys now produce an `invalid-priority` warning instead of being dropped silently. The README was rewritten to the CLDMV layout with every example on the current API.
|
|
23
|
+
- **Header tooling on fix-headers 2.2.0** — the `@cldmv/fix-headers` and `@cldmv/configs` dev dependencies move to 2.2.0 and 1.2.4, so `@Last modified by` now follows content edits only; no file was restamped, and the published package is unaffected (#61, #62).
|
|
24
|
+
- [View full v2.0.5 Changelog](https://github.com/CLDMV/holdmytask/blob/master/docs/changelog/v2/v2.0.5.md)
|
|
25
|
+
|
|
26
|
+
### Recent Releases
|
|
27
|
+
|
|
28
|
+
- **v2.0.4** (October 2026) — Clearer `require()` error on Node.js without `require(esm)`; `brace-expansion` security patch in development tooling ([Changelog](https://github.com/CLDMV/holdmytask/blob/master/docs/changelog/v2/v2.0.4.md))
|
|
29
|
+
- **v2.0.3** (October 2026) — Development toolchain moves to vitest 5; bundle-size and v4 workflow updates; verbatim Apache-2.0 license text ([Changelog](https://github.com/CLDMV/holdmytask/blob/master/docs/changelog/v2/v2.0.3.md))
|
|
30
|
+
- **v2.0.2** (September 2026) — Development dependency update (`@humanfs/node`); no other changes ([Changelog](https://github.com/CLDMV/holdmytask/blob/master/docs/changelog/v2/v2.0.2.md))
|
|
31
|
+
- **v2.0.1** (September 2026) — Fix: a task's timeout timer is now cleared when the task fails or is cancelled ([Changelog](https://github.com/CLDMV/holdmytask/blob/master/docs/changelog/v2/v2.0.1.md))
|
|
32
|
+
|
|
33
|
+
📚 **For complete version history and detailed release notes, see the [docs/changelog/](https://github.com/CLDMV/holdmytask/tree/master/docs/changelog/) folder.**
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## 🚀 Key Features
|
|
14
38
|
|
|
15
39
|
- **Smart scheduling** - Dynamic timeout-based scheduling for optimal performance
|
|
16
40
|
- **Task coalescing** - Intelligent merging of similar tasks for efficiency
|
|
@@ -25,13 +49,72 @@ A tiny, dependency-free task queue for Node.js that executes tasks with priority
|
|
|
25
49
|
- **TypeScript ready** - Full type definitions included
|
|
26
50
|
- **Zero dependencies** - Lightweight and fast
|
|
27
51
|
|
|
52
|
+
---
|
|
53
|
+
|
|
28
54
|
## 📦 Installation
|
|
29
55
|
|
|
56
|
+
### Requirements
|
|
57
|
+
|
|
58
|
+
- **Node.js v18.12.0 or higher** for ESM (`import`), as declared in the package's `engines` field
|
|
59
|
+
- **CommonJS `require()`** needs Node.js `^20.19.0` or `>=22.12.0`. The CommonJS entry is a thin wrapper that loads the ESM build through Node's synchronous `require(esm)`. On older Node.js versions `require("@cldmv/holdmytask")` throws an `ERR_REQUIRE_ESM` error that names the supported versions; load the package with `import()` instead.
|
|
60
|
+
|
|
61
|
+
### Install
|
|
62
|
+
|
|
30
63
|
```bash
|
|
31
64
|
npm install @cldmv/holdmytask
|
|
32
65
|
```
|
|
33
66
|
|
|
34
|
-
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## 🚀 Quick Start
|
|
70
|
+
|
|
71
|
+
```javascript
|
|
72
|
+
import { HoldMyTask } from "@cldmv/holdmytask";
|
|
73
|
+
|
|
74
|
+
const queue = new HoldMyTask({
|
|
75
|
+
concurrency: 2,
|
|
76
|
+
priorities: {
|
|
77
|
+
1: { postDelay: 100 }, // 100ms delay after priority 1 tasks
|
|
78
|
+
2: { postDelay: 200 } // 200ms delay after priority 2 tasks
|
|
79
|
+
},
|
|
80
|
+
coalescing: {
|
|
81
|
+
defaults: {
|
|
82
|
+
windowDuration: 200, // Group similar tasks within 200ms
|
|
83
|
+
maxDelay: 1000 // Force execution after 1000ms max
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// Enqueue a task
|
|
89
|
+
queue.enqueue(
|
|
90
|
+
async (signal) => {
|
|
91
|
+
// Your task logic here
|
|
92
|
+
return "task result";
|
|
93
|
+
},
|
|
94
|
+
(error, result) => {
|
|
95
|
+
if (error) {
|
|
96
|
+
console.error("Task failed:", error);
|
|
97
|
+
} else {
|
|
98
|
+
console.log("Task completed:", result);
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
{ priority: 1, timeout: 5000 }
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
// Coalescing example - multiple similar tasks become one
|
|
105
|
+
const results = await Promise.all([
|
|
106
|
+
queue.enqueue(async () => updateUI(), { coalescingKey: "ui.update" }),
|
|
107
|
+
queue.enqueue(async () => updateUI(), { coalescingKey: "ui.update" }),
|
|
108
|
+
queue.enqueue(async () => updateUI(), { coalescingKey: "ui.update" })
|
|
109
|
+
]);
|
|
110
|
+
// Only one updateUI() call executes, all three promises resolve with the same result
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
CommonJS works the same way: `const { HoldMyTask } = require("@cldmv/holdmytask")` (see [Requirements](#requirements) for the Node.js versions that support `require()`).
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## 📥 Import Options
|
|
35
118
|
|
|
36
119
|
The library provides flexible import options for different use cases:
|
|
37
120
|
|
|
@@ -47,13 +130,10 @@ import HoldMyTask from "@cldmv/holdmytask";
|
|
|
47
130
|
// Import everything
|
|
48
131
|
import * as HoldMyTaskLib from "@cldmv/holdmytask";
|
|
49
132
|
// Uses optimized distribution files
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
### Development Source Access
|
|
53
133
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
134
|
+
// CommonJS (Node.js ^20.19.0 or >=22.12.0)
|
|
135
|
+
const HoldMyTask = require("@cldmv/holdmytask"); // the class itself
|
|
136
|
+
const { HoldMyTask: Named } = require("@cldmv/holdmytask"); // or by name
|
|
57
137
|
```
|
|
58
138
|
|
|
59
139
|
### Common Queue System Aliases
|
|
@@ -74,54 +154,27 @@ import {
|
|
|
74
154
|
// All aliases are functionally identical
|
|
75
155
|
const myQueue = new queue({ concurrency: 5 });
|
|
76
156
|
const stdQueue = new Queue({ concurrency: 5 });
|
|
77
|
-
const manager = new TaskManager({ priorities: {
|
|
157
|
+
const manager = new TaskManager({ priorities: { 1: { postDelay: 100 } } });
|
|
78
158
|
```
|
|
79
159
|
|
|
80
|
-
|
|
160
|
+
The ESM entry also exports async factory helpers that resolve to a new instance: `createHoldMyTask(options)`, `createQueue(options)`, `createTaskManager(options)`, and `createTaskProcessor(options)`.
|
|
161
|
+
|
|
162
|
+
### Development Source Access
|
|
81
163
|
|
|
82
164
|
```javascript
|
|
83
|
-
import { HoldMyTask } from "@cldmv/holdmytask/
|
|
84
|
-
//
|
|
165
|
+
import { HoldMyTask } from "@cldmv/holdmytask/main";
|
|
166
|
+
// Resolves to dist files by default; resolves to src/ when Node.js runs with the holdmytask-dev condition
|
|
85
167
|
```
|
|
86
168
|
|
|
87
|
-
|
|
169
|
+
To run against the unbuilt source (from a checkout of this repository), start Node.js with the `holdmytask-dev` export condition:
|
|
88
170
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
```
|
|
92
|
-
import { HoldMyTask } from "@cldmv/holdmytask";
|
|
171
|
+
```bash
|
|
172
|
+
node --conditions=holdmytask-dev your-script.mjs
|
|
173
|
+
```
|
|
93
174
|
|
|
94
|
-
|
|
95
|
-
concurrency: 2,
|
|
96
|
-
delays: { 1: 100, 2: 200 }, // 100ms delay after priority 1 tasks, 200ms after priority 2
|
|
97
|
-
coalescingWindowDuration: 200, // Group similar tasks within 200ms
|
|
98
|
-
coalescingMaxDelay: 1000 // Force execution after 1000ms max
|
|
99
|
-
});
|
|
175
|
+
**Note:** In a checkout of this repository, the root import runs a best-effort development environment check (`devcheck.mjs`) that exits with setup instructions when `src/` is present but the `holdmytask-dev` condition is not set. The check is skipped when the package is installed as a dependency and in CI.
|
|
100
176
|
|
|
101
|
-
|
|
102
|
-
queue.enqueue(
|
|
103
|
-
async (signal) => {
|
|
104
|
-
// Your task logic here
|
|
105
|
-
return "task result";
|
|
106
|
-
},
|
|
107
|
-
(error, result) => {
|
|
108
|
-
if (error) {
|
|
109
|
-
console.error("Task failed:", error);
|
|
110
|
-
} else {
|
|
111
|
-
console.log("Task completed:", result);
|
|
112
|
-
}
|
|
113
|
-
},
|
|
114
|
-
{ priority: 1, timeout: 5000 }
|
|
115
|
-
);
|
|
116
|
-
|
|
117
|
-
// Coalescing example - multiple similar tasks become one
|
|
118
|
-
const results = await Promise.all([
|
|
119
|
-
queue.enqueue(async () => updateUI(), { coalescingKey: "ui.update" }),
|
|
120
|
-
queue.enqueue(async () => updateUI(), { coalescingKey: "ui.update" }),
|
|
121
|
-
queue.enqueue(async () => updateUI(), { coalescingKey: "ui.update" })
|
|
122
|
-
]);
|
|
123
|
-
// Only one updateUI() call executes, all three promises resolve with the same result
|
|
124
|
-
```
|
|
177
|
+
---
|
|
125
178
|
|
|
126
179
|
## 🔄 Promise API
|
|
127
180
|
|
|
@@ -165,6 +218,8 @@ queue.enqueue(task1, callback, options);
|
|
|
165
218
|
const result = await queue.enqueue(task2, options);
|
|
166
219
|
```
|
|
167
220
|
|
|
221
|
+
---
|
|
222
|
+
|
|
168
223
|
## 🏗️ Async Constructor Pattern
|
|
169
224
|
|
|
170
225
|
HoldMyTask works excellently with async constructor patterns for classes that need initialization:
|
|
@@ -226,6 +281,8 @@ This pattern is particularly useful for:
|
|
|
226
281
|
- **Resource acquisition**: Set up required resources in a controlled manner
|
|
227
282
|
- **Dependency injection**: Initialize dependencies in the correct order
|
|
228
283
|
|
|
284
|
+
---
|
|
285
|
+
|
|
229
286
|
## 📚 API Reference
|
|
230
287
|
|
|
231
288
|
### Constructor
|
|
@@ -236,25 +293,22 @@ const queue = new HoldMyTask(options?)
|
|
|
236
293
|
|
|
237
294
|
### Async Initialization Pattern
|
|
238
295
|
|
|
239
|
-
|
|
296
|
+
With `sync: false`, the constructor returns a Promise that resolves to the instance once initialization has run on the next turn of the event loop. The returned value is a plain Promise, so attach listeners to the resolved instance:
|
|
240
297
|
|
|
241
298
|
```javascript
|
|
242
299
|
// Create instance with async initialization
|
|
243
|
-
const
|
|
300
|
+
const queue = await new HoldMyTask({
|
|
244
301
|
concurrency: 5,
|
|
245
302
|
maxQueue: 100,
|
|
246
303
|
sync: false // Enable async initialization
|
|
247
304
|
});
|
|
248
305
|
|
|
249
|
-
//
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
// Wait for initialization to complete
|
|
254
|
-
const queue = await queuePromise;
|
|
306
|
+
// Initialization warnings are emitted asynchronously, so listeners attached here still receive them
|
|
307
|
+
queue.on("error", (err) => console.error("Queue error:", err));
|
|
308
|
+
queue.on("warning", (warning) => console.warn("Warning:", warning.message));
|
|
255
309
|
```
|
|
256
310
|
|
|
257
|
-
|
|
311
|
+
Initialization warnings (such as [deprecation warnings](#warning-events)) are emitted on `setImmediate` in both modes, so a listener attached right after a synchronous `new HoldMyTask(...)` receives them as well.
|
|
258
312
|
|
|
259
313
|
**Options:**
|
|
260
314
|
|
|
@@ -264,26 +318,25 @@ This pattern is particularly useful when you need to handle initialization error
|
|
|
264
318
|
- `autoStart` (boolean, default: true) - Whether to start processing immediately
|
|
265
319
|
- `defaultPriority` (number, default: 0) - Default task priority
|
|
266
320
|
- `maxQueue` (number, default: Infinity) - Maximum queued tasks. Use `-1` for unlimited queue capacity (equivalent to `Infinity`)
|
|
267
|
-
- `
|
|
268
|
-
- `priorities` (object, default: {}) - Priority-specific configuration: `{ [priority]: { concurrency, postDelay, startDelay } }`
|
|
321
|
+
- `priorities` (object, default: {}) - Priority-specific configuration: `{ [priority]: { concurrency, postDelay, startDelay } }`. Keys must be integers; any other key emits a `warning` event with `type: "invalid-priority"` (and is ignored when it isn't numeric at all)
|
|
269
322
|
- `concurrency` (number) - Maximum concurrent tasks for this priority (defaults to global concurrency limit)
|
|
270
|
-
- `postDelay` (number) - Delay after task
|
|
323
|
+
- `postDelay` (number) - Delay after a task of this priority completes before the next task can start (the delay applies queue-wide; see [Priority Delays](#priority-delays---advanced-timing-control))
|
|
271
324
|
- `startDelay` (number) - Delay before task execution (pre-execution delay)
|
|
272
325
|
- `coalescing` (object) - Enhanced coalescing configuration
|
|
273
326
|
- `defaults` (object) - Default settings for all coalescing keys
|
|
274
327
|
- `windowDuration` (number, default: 200) - Window duration in milliseconds
|
|
275
328
|
- `maxDelay` (number, default: 1000) - Maximum delay before forcing execution
|
|
276
|
-
- `
|
|
277
|
-
- `
|
|
278
|
-
- `resolveAllPromises` (boolean, default: true) - Whether
|
|
329
|
+
- `postDelay` (number) - Default completion delay for coalescing tasks
|
|
330
|
+
- `startDelay` (number) - Default start delay for coalescing tasks
|
|
331
|
+
- `resolveAllPromises` (boolean, default: true) - Whether every task in a coalescing group resolves with the result; when `false`, only the newest task resolves and the others are rejected with `Task was coalesced with a newer task`
|
|
279
332
|
- `multipleCallbacks` (boolean, default: false) - Whether to call multiple callbacks
|
|
280
|
-
- `keys` (object) - Per-key configuration overrides: `{ [key]: { windowDuration, maxDelay,
|
|
281
|
-
- `
|
|
282
|
-
- `
|
|
283
|
-
- `coalescingResolveAllPromises` (boolean, default: true) - **DEPRECATED:** Use `coalescing.defaults.resolveAllPromises`
|
|
284
|
-
- `onError` (function) - Global error handler
|
|
333
|
+
- `keys` (object) - Per-key configuration overrides: `{ [key]: { windowDuration, maxDelay, postDelay, startDelay, ... } }`
|
|
334
|
+
- `healingInterval` (number, default: 5000) - Self-healing check interval in milliseconds (smart scheduling only)
|
|
335
|
+
- `sync` (boolean, default: true) - Set to `false` for [async initialization](#async-initialization-pattern); the constructor then returns a Promise that resolves to the instance
|
|
285
336
|
- `now` (function) - Injectable clock for testing
|
|
286
337
|
|
|
338
|
+
Deprecated option names are still accepted and converted for you, but each one emits a `warning` event; see [Deprecated Options](#deprecated-options) for the mapping.
|
|
339
|
+
|
|
287
340
|
### Methods
|
|
288
341
|
|
|
289
342
|
#### `enqueue(task, callback?, options?)`
|
|
@@ -299,12 +352,12 @@ Adds a task to the queue.
|
|
|
299
352
|
**Task Options:**
|
|
300
353
|
|
|
301
354
|
- `priority` (number) - Task priority (higher = more important)
|
|
302
|
-
- `
|
|
355
|
+
- `postDelay` (number) - Override completion delay for this task (use -1 to bypass delays). `delay` is a deprecated alias that emits a `warning` event once per queue
|
|
303
356
|
- `bypassDelay` (boolean) - If true, skip any active delay period and start immediately
|
|
304
357
|
- `timeout` (number) - Timeout in milliseconds
|
|
305
358
|
- `signal` (AbortSignal) - External abort signal
|
|
306
359
|
- `timestamp` (number) - Absolute execution timestamp
|
|
307
|
-
- `
|
|
360
|
+
- `startDelay` (number) - Milliseconds from now when the task should be ready to run (convenience for timestamp calculation). `start` is a deprecated alias that emits a `warning` event once per queue
|
|
308
361
|
- `coalescingKey` (string) - Tasks with the same coalescing key can be merged for efficiency
|
|
309
362
|
- `mustRunBy` (number) - Absolute timestamp by which the task must execute (overrides coalescing delays)
|
|
310
363
|
- `metadata` (any) - Custom metadata attached to the task. Individual metadata is always directly accessible via the returned task handle
|
|
@@ -424,10 +477,10 @@ queue.enqueue(task2, { id: "duplicate" }); // throws: Task ID "duplicate" alread
|
|
|
424
477
|
|
|
425
478
|
- `configurePriority(priority, config)` - Configure or update priority-specific settings
|
|
426
479
|
- `priority` (string|number) - Priority level to configure
|
|
427
|
-
- `config` (object) - Configuration: `{
|
|
480
|
+
- `config` (object) - Configuration: `{ postDelay?, startDelay? }`
|
|
428
481
|
- `configureCoalescingKey(key, config)` - Configure or update coalescing key settings
|
|
429
482
|
- `key` (string) - Coalescing key to configure
|
|
430
|
-
- `config` (object) - Configuration: `{ windowDuration?, maxDelay?,
|
|
483
|
+
- `config` (object) - Configuration: `{ windowDuration?, maxDelay?, postDelay?, startDelay?, multipleCallbacks?, resolveAllPromises? }`
|
|
431
484
|
- `getPriorityConfig(priority, taskOptions?)` - Get effective configuration for a specific priority
|
|
432
485
|
- `getPriorityConfigurations()` - Get all configured priorities and their settings
|
|
433
486
|
- `getCoalescingConfig(coalescingKey, taskOptions?)` - Get effective configuration for a specific coalescing key
|
|
@@ -560,7 +613,7 @@ const queue = new HoldMyTask({ concurrency: 2 });
|
|
|
560
613
|
|
|
561
614
|
// Add some tasks
|
|
562
615
|
queue.enqueue(async () => "task1");
|
|
563
|
-
queue.enqueue(async () => "task2", { priority: 2,
|
|
616
|
+
queue.enqueue(async () => "task2", { priority: 2, startDelay: 5000 });
|
|
564
617
|
|
|
565
618
|
// Check initial state
|
|
566
619
|
console.log("Initial state:");
|
|
@@ -602,28 +655,66 @@ queue.on("warning", (warning) => {
|
|
|
602
655
|
});
|
|
603
656
|
```
|
|
604
657
|
|
|
605
|
-
|
|
658
|
+
When a callback-style task fails, times out, or is aborted, the task's callback receives the failure. The queue also emits an `error` event with the task (its `error` and `status` properties describe the failure), but only when an `error` listener is attached, so a listener is optional. Promise-style tasks report task failures only through the rejected promise.
|
|
659
|
+
|
|
660
|
+
The callback's first argument is an error payload rather than the raw error: `{ type: "timeout", message }`, `{ type: "canceled", message: "Task was aborted" }`, or `{ type: "error", error }`.
|
|
606
661
|
|
|
607
|
-
|
|
662
|
+
### Warning Events
|
|
663
|
+
|
|
664
|
+
HoldMyTask reports configuration problems through `warning` events instead of throwing. The events are emitted asynchronously (on `setImmediate`), so a listener attached right after the constructor returns still receives them. There are two types.
|
|
665
|
+
|
|
666
|
+
**Deprecations** (`type: "deprecation"`): when deprecated options are used, HoldMyTask converts them to the current names and emits one warning per deprecated option:
|
|
667
|
+
|
|
668
|
+
- `type` - `"deprecation"`
|
|
669
|
+
- `message` - a human-readable description
|
|
670
|
+
- `deprecated` - the deprecated option or property name
|
|
671
|
+
- `replacement` - the name to use instead
|
|
672
|
+
|
|
673
|
+
**Invalid priority keys** (`type: "invalid-priority"`): `priorities` (and the deprecated `delays`) must be keyed by integers. A key that isn't one is reported instead of being dropped silently: a key that isn't numeric at all (`"high"`) is ignored, and a key with a fractional part (`"2.5"`) is applied to the truncated priority (`2`), as before:
|
|
674
|
+
|
|
675
|
+
- `type` - `"invalid-priority"`
|
|
676
|
+
- `message` - a human-readable description
|
|
677
|
+
- `option` - the option the key came from (`"priorities"` or `"delays"`)
|
|
678
|
+
- `key` - the key as written
|
|
679
|
+
- `priority` - the priority it was applied to, or `null` when it was ignored
|
|
608
680
|
|
|
609
681
|
```javascript
|
|
610
682
|
const queue = new HoldMyTask({
|
|
611
|
-
delays: { 1: 100 }, // Deprecated
|
|
612
|
-
coalescingWindowDuration: 200 // Deprecated
|
|
683
|
+
delays: { 1: 100 }, // Deprecated: use priorities
|
|
684
|
+
coalescingWindowDuration: 200 // Deprecated: use coalescing.defaults.windowDuration
|
|
613
685
|
});
|
|
614
686
|
|
|
615
687
|
queue.on("warning", (warning) => {
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
688
|
+
if (warning.type === "deprecation") {
|
|
689
|
+
console.warn(`${warning.message} (${warning.deprecated} -> ${warning.replacement})`);
|
|
690
|
+
}
|
|
619
691
|
});
|
|
620
692
|
|
|
621
693
|
// Output:
|
|
622
|
-
//
|
|
623
|
-
// Use
|
|
624
|
-
// In: constructor options
|
|
694
|
+
// Option 'coalescingWindowDuration' is deprecated. Use 'coalescing.defaults.windowDuration' instead. (coalescingWindowDuration -> coalescing.defaults.windowDuration)
|
|
695
|
+
// Option 'delays' is deprecated. Use 'priorities' instead with { [priority]: { postDelay: value, startDelay: 0 } } format. (delays -> priorities)
|
|
625
696
|
```
|
|
626
697
|
|
|
698
|
+
### Deprecated Options
|
|
699
|
+
|
|
700
|
+
These names still work, but each emits a deprecation warning. Use the current names in new code:
|
|
701
|
+
|
|
702
|
+
| Deprecated | Use instead |
|
|
703
|
+
| ------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------- |
|
|
704
|
+
| `delays: { [priority]: ms }` | `priorities: { [priority]: { postDelay: ms } }` |
|
|
705
|
+
| `delay` in a `priorities` entry, `coalescing.defaults`, `coalescing.keys` entry, `configurePriority()` or `configureCoalescingKey()` | `postDelay` |
|
|
706
|
+
| `start` in the same places | `startDelay` |
|
|
707
|
+
| `delay` as a task option passed to `enqueue()` | `postDelay` |
|
|
708
|
+
| `start` as a task option passed to `enqueue()` | `startDelay` |
|
|
709
|
+
| `coalescingWindowDuration` (constructor) | `coalescing.defaults.windowDuration` |
|
|
710
|
+
| `coalescingMaxDelay` (constructor) | `coalescing.defaults.maxDelay` |
|
|
711
|
+
| `coalescingMultipleCallbacks` (constructor) | `coalescing.defaults.multipleCallbacks` |
|
|
712
|
+
| `coalescingResolveAllPromises` (constructor) | `coalescing.defaults.resolveAllPromises` |
|
|
713
|
+
|
|
714
|
+
Each task-level alias (`delay`, `start`) warns once per queue instance, not once per task. If both a deprecated alias and its replacement are given, the replacement wins. The per-task `coalescingWindowDuration` / `coalescingMaxDelay` / `coalescingMultipleCallbacks` / `coalescingResolveAllPromises` overrides are not deprecated and remain the current names for task options.
|
|
715
|
+
|
|
716
|
+
---
|
|
717
|
+
|
|
627
718
|
## ⚙️ Concurrency & Delays Interaction
|
|
628
719
|
|
|
629
720
|
Understanding how concurrency and delays work together is crucial for optimal queue behavior:
|
|
@@ -633,7 +724,7 @@ Understanding how concurrency and delays work together is crucial for optimal qu
|
|
|
633
724
|
```javascript
|
|
634
725
|
const queue = new HoldMyTask({
|
|
635
726
|
concurrency: 3, // Up to 3 tasks can run simultaneously
|
|
636
|
-
|
|
727
|
+
priorities: { 1: { postDelay: 500 }, 2: { postDelay: 1000 } }
|
|
637
728
|
});
|
|
638
729
|
```
|
|
639
730
|
|
|
@@ -646,7 +737,7 @@ const queue = new HoldMyTask({
|
|
|
646
737
|
### Timing Examples
|
|
647
738
|
|
|
648
739
|
```javascript
|
|
649
|
-
// Timeline with concurrency: 2,
|
|
740
|
+
// Timeline with concurrency: 2, priorities: { 1: { postDelay: 1000 } }
|
|
650
741
|
|
|
651
742
|
// 10:00:00 - Start: TaskA (pri 1), TaskB (pri 1) - both running
|
|
652
743
|
// 10:00:02 - TaskA completes → 1000ms delay starts, TaskB still running
|
|
@@ -694,10 +785,6 @@ HoldMyTask uses a sophisticated dual-heap scheduling system for optimal performa
|
|
|
694
785
|
|
|
695
786
|
This architecture enables handling thousands of tasks with precise timing control while maintaining excellent performance.
|
|
696
787
|
|
|
697
|
-
[](https://www.codefactor.io/repository/github/cldmv/holdmytask) [](https://npms.io/search?q=%40cldmv%2Fholdmytask)
|
|
698
|
-
|
|
699
|
-
[](https://www.npmjs.com/package/@cldmv/holdmytask) [](https://github.com/CLDMV/holdmytask)
|
|
700
|
-
|
|
701
788
|
### Unlimited Queue Capacity
|
|
702
789
|
|
|
703
790
|
For scenarios requiring unlimited task queuing, you can set `maxQueue` to `-1`:
|
|
@@ -725,6 +812,8 @@ for (let i = 0; i < 100000; i++) {
|
|
|
725
812
|
|
|
726
813
|
While `-1` allows unlimited queuing, be mindful of memory usage with very large task sets. Each queued task consumes memory until executed.
|
|
727
814
|
|
|
815
|
+
---
|
|
816
|
+
|
|
728
817
|
## 🎯 Advanced Features
|
|
729
818
|
|
|
730
819
|
### Priority System
|
|
@@ -754,50 +843,50 @@ HoldMyTask supports comprehensive priority and coalescing configuration for soph
|
|
|
754
843
|
```javascript
|
|
755
844
|
const queue = new HoldMyTask({
|
|
756
845
|
concurrency: 3,
|
|
757
|
-
// Priority-specific defaults
|
|
846
|
+
// Priority-specific defaults
|
|
758
847
|
priorities: {
|
|
759
|
-
1: {
|
|
760
|
-
2: {
|
|
761
|
-
3: {
|
|
848
|
+
1: { postDelay: 200, startDelay: 0 }, // High priority: 200ms delay, immediate start
|
|
849
|
+
2: { postDelay: 100, startDelay: 25 }, // Medium priority: 100ms delay, 25ms start delay
|
|
850
|
+
3: { postDelay: 50, startDelay: 50 } // Low priority: 50ms delay, 50ms start delay
|
|
762
851
|
},
|
|
763
852
|
// Enhanced coalescing with per-key settings
|
|
764
853
|
coalescing: {
|
|
765
854
|
defaults: {
|
|
766
855
|
windowDuration: 200,
|
|
767
856
|
maxDelay: 1000,
|
|
768
|
-
|
|
769
|
-
|
|
857
|
+
postDelay: 75, // Default completion delay for coalescing tasks
|
|
858
|
+
startDelay: 25, // Default start delay for coalescing tasks
|
|
770
859
|
resolveAllPromises: true
|
|
771
860
|
},
|
|
772
861
|
keys: {
|
|
773
862
|
"ui.update": {
|
|
774
863
|
windowDuration: 100,
|
|
775
864
|
maxDelay: 500,
|
|
776
|
-
|
|
777
|
-
|
|
865
|
+
postDelay: 25, // Fast UI updates
|
|
866
|
+
startDelay: 0
|
|
778
867
|
},
|
|
779
868
|
"api.batch": {
|
|
780
869
|
windowDuration: 1000,
|
|
781
870
|
maxDelay: 5000,
|
|
782
|
-
|
|
783
|
-
|
|
871
|
+
postDelay: 200, // Slower API operations
|
|
872
|
+
startDelay: 100
|
|
784
873
|
}
|
|
785
874
|
}
|
|
786
875
|
}
|
|
787
876
|
});
|
|
788
877
|
|
|
789
878
|
// Dynamic configuration
|
|
790
|
-
queue.configurePriority(4, {
|
|
879
|
+
queue.configurePriority(4, { postDelay: 300, startDelay: 75 });
|
|
791
880
|
queue.configureCoalescingKey("data.sync", {
|
|
792
881
|
windowDuration: 800,
|
|
793
882
|
maxDelay: 3000,
|
|
794
|
-
|
|
795
|
-
|
|
883
|
+
postDelay: 150,
|
|
884
|
+
startDelay: 50
|
|
796
885
|
});
|
|
797
886
|
|
|
798
887
|
// Get configuration information
|
|
799
888
|
const priority4Config = queue.getPriorityConfig(4);
|
|
800
|
-
console.log(`Priority 4: ${priority4Config.
|
|
889
|
+
console.log(`Priority 4: ${priority4Config.postDelay}ms delay, ${priority4Config.startDelay}ms start delay`);
|
|
801
890
|
|
|
802
891
|
const dataSyncConfig = queue.getCoalescingConfig("data.sync");
|
|
803
892
|
console.log(`Data sync: ${dataSyncConfig.windowDuration}ms window, ${dataSyncConfig.maxDelay}ms max delay`);
|
|
@@ -817,7 +906,7 @@ console.log("All coalescing configs:", allCoalescingKeys);
|
|
|
817
906
|
4. Coalescing defaults
|
|
818
907
|
5. System defaults (lowest priority)
|
|
819
908
|
|
|
820
|
-
**Backward Compatibility:**
|
|
909
|
+
**Backward Compatibility:** Deprecated option names are converted automatically; see [Deprecated Options](#deprecated-options).
|
|
821
910
|
|
|
822
911
|
### Smart Scheduling
|
|
823
912
|
|
|
@@ -968,10 +1057,10 @@ Priority delays create "cool-down" periods after task completion based on the co
|
|
|
968
1057
|
```javascript
|
|
969
1058
|
const queue = new HoldMyTask({
|
|
970
1059
|
concurrency: 1,
|
|
971
|
-
|
|
972
|
-
1: 1000, // 1 second delay after priority 1 tasks complete
|
|
973
|
-
2: 500, // 500ms delay after priority 2 tasks complete
|
|
974
|
-
3: 0 // No delay after priority 3 tasks (explicit)
|
|
1060
|
+
priorities: {
|
|
1061
|
+
1: { postDelay: 1000 }, // 1 second delay after priority 1 tasks complete
|
|
1062
|
+
2: { postDelay: 500 }, // 500ms delay after priority 2 tasks complete
|
|
1063
|
+
3: { postDelay: 0 } // No delay after priority 3 tasks (explicit)
|
|
975
1064
|
}
|
|
976
1065
|
});
|
|
977
1066
|
|
|
@@ -982,22 +1071,22 @@ const queue = new HoldMyTask({
|
|
|
982
1071
|
// → Next task can't start until 10:00:07.5 (500ms delay)
|
|
983
1072
|
|
|
984
1073
|
// Override delay for specific task
|
|
985
|
-
queue.enqueue(task, callback, { priority: 1,
|
|
1074
|
+
queue.enqueue(task, callback, { priority: 1, postDelay: 200 }); // Uses 200ms instead of 1000ms
|
|
986
1075
|
|
|
987
1076
|
// Set zero delay for specific task
|
|
988
|
-
queue.enqueue(task, callback, { priority: 1,
|
|
1077
|
+
queue.enqueue(task, callback, { priority: 1, postDelay: 0 }); // No delay after this task
|
|
989
1078
|
```
|
|
990
1079
|
|
|
991
1080
|
### Delay Bypass - Emergency Task Injection
|
|
992
1081
|
|
|
993
|
-
When urgent tasks need to execute immediately, bypassing active delay periods, use the `bypassDelay` option or `
|
|
1082
|
+
When urgent tasks need to execute immediately, bypassing active delay periods, use the `bypassDelay` option or `postDelay: -1` syntax. This is perfect for emergency situations, high-priority interrupts, or critical system tasks.
|
|
994
1083
|
|
|
995
1084
|
#### Bypass Behavior
|
|
996
1085
|
|
|
997
1086
|
```javascript
|
|
998
1087
|
const queue = new HoldMyTask({
|
|
999
1088
|
concurrency: 1,
|
|
1000
|
-
|
|
1089
|
+
priorities: { 1: { postDelay: 1000 } } // 1 second delay after priority 1 tasks
|
|
1001
1090
|
});
|
|
1002
1091
|
|
|
1003
1092
|
// Timeline example:
|
|
@@ -1015,7 +1104,7 @@ queue.enqueue(urgentTaskC, callback, {
|
|
|
1015
1104
|
// Alternative bypass syntax
|
|
1016
1105
|
queue.enqueue(emergencyTaskD, callback, {
|
|
1017
1106
|
priority: 1,
|
|
1018
|
-
|
|
1107
|
+
postDelay: -1 // Same as bypassDelay: true
|
|
1019
1108
|
});
|
|
1020
1109
|
```
|
|
1021
1110
|
|
|
@@ -1024,7 +1113,7 @@ queue.enqueue(emergencyTaskD, callback, {
|
|
|
1024
1113
|
```javascript
|
|
1025
1114
|
const queue = new HoldMyTask({
|
|
1026
1115
|
concurrency: 2,
|
|
1027
|
-
|
|
1116
|
+
priorities: { 1: { postDelay: 800 }, 2: { postDelay: 400 } }
|
|
1028
1117
|
});
|
|
1029
1118
|
|
|
1030
1119
|
// Multiple tasks with different bypass behavior
|
|
@@ -1044,6 +1133,8 @@ queue.enqueue(taskD, callback, { priority: 1 }); // Waits for taskC's completion
|
|
|
1044
1133
|
- ✅ **Concurrency aware** - works correctly with multiple concurrent execution slots
|
|
1045
1134
|
- ⚠️ **Use sparingly** - frequent bypassing defeats the purpose of delay-based rate limiting
|
|
1046
1135
|
|
|
1136
|
+
---
|
|
1137
|
+
|
|
1047
1138
|
## 🔄 Task Coalescing System
|
|
1048
1139
|
|
|
1049
1140
|
The coalescing system allows multiple similar tasks to be intelligently merged, reducing redundant operations while ensuring all promises resolve with accurate results. This is perfect for scenarios like UI updates, API calls, or device commands where only the final result matters.
|
|
@@ -1057,7 +1148,7 @@ When tasks with the same `coalescingKey` are enqueued within a time window, they
|
|
|
1057
1148
|
3. **One representative task** executes for the entire group
|
|
1058
1149
|
4. **All promises** in the group resolve with the same result
|
|
1059
1150
|
|
|
1060
|
-
⚠️ **Critical Timing Consideration**: Real-world tasks take time to execute (100ms-2000ms+). If your coalescing tasks need to see the final state from other operations, ensure proper timing with `
|
|
1151
|
+
⚠️ **Critical Timing Consideration**: Real-world tasks take time to execute (100ms-2000ms+). If your coalescing tasks need to see the final state from other operations, ensure proper timing with `startDelay` or `timestamp` scheduling. Tasks that start too early may see intermediate states rather than final results.
|
|
1061
1152
|
|
|
1062
1153
|
### 🎯 Correct Coalescing Pattern: Fire-and-Forget with Embedded Updates
|
|
1063
1154
|
|
|
@@ -1106,9 +1197,13 @@ function volumeUp() {
|
|
|
1106
1197
|
```javascript
|
|
1107
1198
|
const queue = new HoldMyTask({
|
|
1108
1199
|
concurrency: 1,
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1200
|
+
coalescing: {
|
|
1201
|
+
defaults: {
|
|
1202
|
+
windowDuration: 200, // 200ms window for grouping tasks
|
|
1203
|
+
maxDelay: 1000, // Maximum 1000ms delay before forcing execution
|
|
1204
|
+
resolveAllPromises: true // All promises get the result (default: true)
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1112
1207
|
});
|
|
1113
1208
|
```
|
|
1114
1209
|
|
|
@@ -1128,7 +1223,7 @@ async function updateVolume(change) {
|
|
|
1128
1223
|
{
|
|
1129
1224
|
coalescingKey: "volume.update", // Tasks with same key get grouped
|
|
1130
1225
|
priority: 1,
|
|
1131
|
-
|
|
1226
|
+
postDelay: 100 // 100ms delay after completion
|
|
1132
1227
|
}
|
|
1133
1228
|
);
|
|
1134
1229
|
}
|
|
@@ -1154,8 +1249,12 @@ The correct pattern for coalescing with updates is to enqueue update tasks **fro
|
|
|
1154
1249
|
```javascript
|
|
1155
1250
|
const queue = new HoldMyTask({
|
|
1156
1251
|
concurrency: 2, // Allow volume and update tasks to run concurrently
|
|
1157
|
-
|
|
1158
|
-
|
|
1252
|
+
coalescing: {
|
|
1253
|
+
defaults: {
|
|
1254
|
+
windowDuration: 200,
|
|
1255
|
+
maxDelay: 1000
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1159
1258
|
});
|
|
1160
1259
|
|
|
1161
1260
|
// Volume commands using fire-and-forget pattern
|
|
@@ -1182,7 +1281,7 @@ function volumeUp(amount = 1) {
|
|
|
1182
1281
|
{
|
|
1183
1282
|
coalescingKey: "volume.ui.update", // UI updates get coalesced
|
|
1184
1283
|
priority: 5, // Lower priority than volume commands
|
|
1185
|
-
|
|
1284
|
+
postDelay: 100 // Brief delay after UI updates
|
|
1186
1285
|
}
|
|
1187
1286
|
);
|
|
1188
1287
|
|
|
@@ -1190,7 +1289,7 @@ function volumeUp(amount = 1) {
|
|
|
1190
1289
|
},
|
|
1191
1290
|
{
|
|
1192
1291
|
priority: 1, // High priority for user actions
|
|
1193
|
-
|
|
1292
|
+
postDelay: 100 // Brief delay after volume operations
|
|
1194
1293
|
}
|
|
1195
1294
|
);
|
|
1196
1295
|
}
|
|
@@ -1223,7 +1322,7 @@ async function processDataBatch(data) {
|
|
|
1223
1322
|
{
|
|
1224
1323
|
coalescingKey: "api.batch.process",
|
|
1225
1324
|
priority: 2,
|
|
1226
|
-
|
|
1325
|
+
startDelay: 100 // 100ms delay allows grouping
|
|
1227
1326
|
}
|
|
1228
1327
|
);
|
|
1229
1328
|
}
|
|
@@ -1240,12 +1339,16 @@ async function processDataBatch(data) {
|
|
|
1240
1339
|
|
|
1241
1340
|
### Coalescing with Explicit Timestamps
|
|
1242
1341
|
|
|
1243
|
-
For precise scheduling, use `timestamp` instead of `
|
|
1342
|
+
For precise scheduling, use `timestamp` instead of `startDelay`:
|
|
1244
1343
|
|
|
1245
1344
|
```javascript
|
|
1246
1345
|
const queue = new HoldMyTask({
|
|
1247
|
-
|
|
1248
|
-
|
|
1346
|
+
coalescing: {
|
|
1347
|
+
defaults: {
|
|
1348
|
+
windowDuration: 300,
|
|
1349
|
+
maxDelay: 2000
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1249
1352
|
});
|
|
1250
1353
|
|
|
1251
1354
|
// Schedule all updates for the same exact time
|
|
@@ -1279,8 +1382,12 @@ Control maximum delay with `mustRunBy` to ensure tasks don't wait too long:
|
|
|
1279
1382
|
|
|
1280
1383
|
```javascript
|
|
1281
1384
|
const queue = new HoldMyTask({
|
|
1282
|
-
|
|
1283
|
-
|
|
1385
|
+
coalescing: {
|
|
1386
|
+
defaults: {
|
|
1387
|
+
windowDuration: 500, // Try to group for 500ms
|
|
1388
|
+
maxDelay: 2000 // But never wait more than 2 seconds
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1284
1391
|
});
|
|
1285
1392
|
|
|
1286
1393
|
// Critical system updates
|
|
@@ -1309,8 +1416,12 @@ Control how promises resolve within coalescing groups:
|
|
|
1309
1416
|
|
|
1310
1417
|
```javascript
|
|
1311
1418
|
const queue = new HoldMyTask({
|
|
1312
|
-
|
|
1313
|
-
|
|
1419
|
+
coalescing: {
|
|
1420
|
+
defaults: {
|
|
1421
|
+
resolveAllPromises: true, // Default: all promises get the result
|
|
1422
|
+
windowDuration: 200
|
|
1423
|
+
}
|
|
1424
|
+
}
|
|
1314
1425
|
});
|
|
1315
1426
|
|
|
1316
1427
|
// Mode 1: All promises resolve (default behavior)
|
|
@@ -1321,18 +1432,23 @@ const results1 = await Promise.all([
|
|
|
1321
1432
|
]);
|
|
1322
1433
|
// All three promises resolve with the same result
|
|
1323
1434
|
|
|
1324
|
-
// Mode 2: Only representative promise resolves
|
|
1435
|
+
// Mode 2: Only the representative (newest) task's promise resolves
|
|
1325
1436
|
const queue2 = new HoldMyTask({
|
|
1326
|
-
|
|
1327
|
-
|
|
1437
|
+
coalescing: {
|
|
1438
|
+
defaults: {
|
|
1439
|
+
resolveAllPromises: false,
|
|
1440
|
+
windowDuration: 200
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1328
1443
|
});
|
|
1329
1444
|
|
|
1330
|
-
const
|
|
1331
|
-
queue2.enqueue(task, { coalescingKey: "test" }), //
|
|
1332
|
-
queue2.enqueue(task, { coalescingKey: "test" }), //
|
|
1333
|
-
queue2.enqueue(task, { coalescingKey: "test" }) // Resolves with
|
|
1445
|
+
const settled = await Promise.allSettled([
|
|
1446
|
+
queue2.enqueue(task, { coalescingKey: "test" }), // Rejects: "Task was coalesced with a newer task"
|
|
1447
|
+
queue2.enqueue(task, { coalescingKey: "test" }), // Rejects: "Task was coalesced with a newer task"
|
|
1448
|
+
queue2.enqueue(task, { coalescingKey: "test" }) // Resolves with the result
|
|
1334
1449
|
]);
|
|
1335
|
-
// Only the
|
|
1450
|
+
// Only the newest task in the group gets the result; the others are rejected
|
|
1451
|
+
// (callback-style tasks receive the same error as their first argument)
|
|
1336
1452
|
```
|
|
1337
1453
|
|
|
1338
1454
|
### Real-World Coalescing Patterns
|
|
@@ -1344,8 +1460,12 @@ class VolumeController {
|
|
|
1344
1460
|
constructor() {
|
|
1345
1461
|
this.queue = new HoldMyTask({
|
|
1346
1462
|
concurrency: 2, // Allow volume and update tasks concurrently
|
|
1347
|
-
|
|
1348
|
-
|
|
1463
|
+
coalescing: {
|
|
1464
|
+
defaults: {
|
|
1465
|
+
windowDuration: 200,
|
|
1466
|
+
maxDelay: 1000
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1349
1469
|
});
|
|
1350
1470
|
}
|
|
1351
1471
|
|
|
@@ -1368,7 +1488,7 @@ class VolumeController {
|
|
|
1368
1488
|
{
|
|
1369
1489
|
coalescingKey: "volume.ui.update", // Updates coalesce together
|
|
1370
1490
|
priority: 3, // Lower priority than volume changes
|
|
1371
|
-
|
|
1491
|
+
postDelay: 100 // Brief delay after UI updates
|
|
1372
1492
|
}
|
|
1373
1493
|
);
|
|
1374
1494
|
|
|
@@ -1376,7 +1496,7 @@ class VolumeController {
|
|
|
1376
1496
|
},
|
|
1377
1497
|
{
|
|
1378
1498
|
priority: 1, // High priority for user actions
|
|
1379
|
-
|
|
1499
|
+
postDelay: 50 // Brief delay between volume operations
|
|
1380
1500
|
}
|
|
1381
1501
|
);
|
|
1382
1502
|
}
|
|
@@ -1390,8 +1510,12 @@ class APIBatcher {
|
|
|
1390
1510
|
constructor() {
|
|
1391
1511
|
this.queue = new HoldMyTask({
|
|
1392
1512
|
concurrency: 2,
|
|
1393
|
-
|
|
1394
|
-
|
|
1513
|
+
coalescing: {
|
|
1514
|
+
defaults: {
|
|
1515
|
+
windowDuration: 300,
|
|
1516
|
+
maxDelay: 1500
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1395
1519
|
});
|
|
1396
1520
|
}
|
|
1397
1521
|
|
|
@@ -1421,8 +1545,12 @@ Real-world performance improvements with the embedded update pattern:
|
|
|
1421
1545
|
// Fire-and-forget pattern with embedded coalescing updates
|
|
1422
1546
|
const queue = new HoldMyTask({
|
|
1423
1547
|
concurrency: 2,
|
|
1424
|
-
|
|
1425
|
-
|
|
1548
|
+
coalescing: {
|
|
1549
|
+
defaults: {
|
|
1550
|
+
windowDuration: 200,
|
|
1551
|
+
maxDelay: 1000
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1426
1554
|
});
|
|
1427
1555
|
|
|
1428
1556
|
// Volume control example - realistic embedded pattern
|
|
@@ -1461,9 +1589,11 @@ function processVolumeCommands() {
|
|
|
1461
1589
|
// - Total time: ~25-30 seconds (vs 50+ seconds without coalescing)
|
|
1462
1590
|
```
|
|
1463
1591
|
|
|
1464
|
-
|
|
1592
|
+
---
|
|
1593
|
+
|
|
1594
|
+
## ⏱️ Timeouts
|
|
1465
1595
|
|
|
1466
|
-
Tasks automatically
|
|
1596
|
+
Tasks automatically time out and either call the callback with an error payload or reject the promise. Callback-style timeouts also emit an `error` event on the queue (see [Events](#events)):
|
|
1467
1597
|
|
|
1468
1598
|
**Callback API:**
|
|
1469
1599
|
|
|
@@ -1503,6 +1633,8 @@ try {
|
|
|
1503
1633
|
}
|
|
1504
1634
|
```
|
|
1505
1635
|
|
|
1636
|
+
---
|
|
1637
|
+
|
|
1506
1638
|
## 🛑 AbortController Support
|
|
1507
1639
|
|
|
1508
1640
|
The library uses `AbortController` for cooperative task cancellation. This allows tasks to be cancelled gracefully without forcing termination.
|
|
@@ -1664,6 +1796,8 @@ try {
|
|
|
1664
1796
|
- **Cooperative**: Tasks must actively check and respond to the abort signal
|
|
1665
1797
|
- **Async operations**: Only cancellable if the underlying operation supports AbortSignal
|
|
1666
1798
|
|
|
1799
|
+
---
|
|
1800
|
+
|
|
1667
1801
|
## 📝 Examples
|
|
1668
1802
|
|
|
1669
1803
|
### Basic Usage
|
|
@@ -1697,10 +1831,10 @@ function processUser(userId) {
|
|
|
1697
1831
|
```javascript
|
|
1698
1832
|
const queue = new HoldMyTask({
|
|
1699
1833
|
concurrency: 1,
|
|
1700
|
-
|
|
1701
|
-
1: 1000, // 1 second between high-priority tasks
|
|
1702
|
-
2: 100, // 100ms between medium-priority tasks
|
|
1703
|
-
3: 0 // No delay for low-priority tasks
|
|
1834
|
+
priorities: {
|
|
1835
|
+
1: { postDelay: 1000 }, // 1 second between high-priority tasks
|
|
1836
|
+
2: { postDelay: 100 }, // 100ms between medium-priority tasks
|
|
1837
|
+
3: { postDelay: 0 } // No delay for low-priority tasks
|
|
1704
1838
|
}
|
|
1705
1839
|
});
|
|
1706
1840
|
|
|
@@ -1717,7 +1851,7 @@ queue.enqueue(mediumTask2, callback, { priority: 2 });
|
|
|
1717
1851
|
```javascript
|
|
1718
1852
|
const queue = new HoldMyTask({
|
|
1719
1853
|
concurrency: 5,
|
|
1720
|
-
|
|
1854
|
+
priorities: { 1: { postDelay: 50 } } // Small delay between batches
|
|
1721
1855
|
});
|
|
1722
1856
|
|
|
1723
1857
|
async function processBatch(items) {
|
|
@@ -1752,10 +1886,10 @@ Real-world scenario: API rate limiting with emergency override capability.
|
|
|
1752
1886
|
```javascript
|
|
1753
1887
|
const apiQueue = new HoldMyTask({
|
|
1754
1888
|
concurrency: 2,
|
|
1755
|
-
|
|
1756
|
-
1: 2000, // 2 second delay between API calls (rate limiting)
|
|
1757
|
-
2: 5000, // 5 second delay for heavy operations
|
|
1758
|
-
9: 0 // No delay for monitoring tasks
|
|
1889
|
+
priorities: {
|
|
1890
|
+
1: { postDelay: 2000 }, // 2 second delay between API calls (rate limiting)
|
|
1891
|
+
2: { postDelay: 5000 }, // 5 second delay for heavy operations
|
|
1892
|
+
9: { postDelay: 0 } // No delay for monitoring tasks
|
|
1759
1893
|
}
|
|
1760
1894
|
});
|
|
1761
1895
|
|
|
@@ -1778,7 +1912,7 @@ apiQueue.enqueue(processSecurityAlert, handleEmergency, {
|
|
|
1778
1912
|
// Alternative syntax for bypass
|
|
1779
1913
|
apiQueue.enqueue(emergencyShutdown, handleEmergency, {
|
|
1780
1914
|
priority: 1,
|
|
1781
|
-
|
|
1915
|
+
postDelay: -1, // Same as bypassDelay: true
|
|
1782
1916
|
metadata: { action: "shutdown" }
|
|
1783
1917
|
});
|
|
1784
1918
|
|
|
@@ -1798,23 +1932,26 @@ setInterval(() => {
|
|
|
1798
1932
|
}, 1000);
|
|
1799
1933
|
```
|
|
1800
1934
|
|
|
1935
|
+
---
|
|
1936
|
+
|
|
1801
1937
|
## 🧪 Testing & Development
|
|
1802
1938
|
|
|
1803
1939
|
```bash
|
|
1804
|
-
# Run tests
|
|
1940
|
+
# Run tests (vitest suite + CommonJS entry tests)
|
|
1805
1941
|
npm test
|
|
1806
1942
|
|
|
1807
1943
|
# Run tests with coverage
|
|
1808
|
-
npm run
|
|
1944
|
+
npm run coverage
|
|
1809
1945
|
|
|
1810
1946
|
# Run tests in watch mode
|
|
1811
|
-
npm run test:
|
|
1947
|
+
npm run test:watch
|
|
1812
1948
|
|
|
1813
|
-
# Lint
|
|
1949
|
+
# Lint and format check
|
|
1814
1950
|
npm run lint
|
|
1951
|
+
npm run format:check
|
|
1815
1952
|
|
|
1816
|
-
# Build for publishing
|
|
1817
|
-
npm run build
|
|
1953
|
+
# Build types, type-check, and build for publishing
|
|
1954
|
+
npm run build:ci
|
|
1818
1955
|
```
|
|
1819
1956
|
|
|
1820
1957
|
### Testing with Injectable Clock
|
|
@@ -1838,21 +1975,74 @@ console.log(Date.now()); // Returns actual system time
|
|
|
1838
1975
|
queue.enqueue(task, { timestamp: queue.now() + 5000 }); // 5 seconds from mock time
|
|
1839
1976
|
```
|
|
1840
1977
|
|
|
1841
|
-
|
|
1978
|
+
---
|
|
1979
|
+
|
|
1980
|
+
## 📚 Documentation
|
|
1981
|
+
|
|
1982
|
+
- **[Changelog](https://github.com/CLDMV/holdmytask/tree/master/docs/changelog/)** — release notes for every version (v1 + v2)
|
|
1983
|
+
- **[Examples](https://github.com/CLDMV/holdmytask/tree/master/examples/)** — runnable scripts exercising priorities, coalescing, and stress scenarios
|
|
1984
|
+
- **[Type definitions](https://github.com/CLDMV/holdmytask/tree/master/types/)** — the TypeScript declarations shipped with the package
|
|
1842
1985
|
|
|
1843
|
-
[![
|
|
1986
|
+
[![CodeFactor]][codefactor_url] [![OpenSSF Scorecard]][ossf_scorecard_url] [![npms.io score]][npms_url] [![npm unpacked size]][npm_size_url] [![Repo size]][repo_size_url]
|
|
1844
1987
|
|
|
1845
|
-
|
|
1988
|
+
---
|
|
1846
1989
|
|
|
1847
1990
|
## 🤝 Contributing
|
|
1848
1991
|
|
|
1849
|
-
Contributions welcome! Please ensure:
|
|
1992
|
+
Contributions are welcome! Please ensure:
|
|
1850
1993
|
|
|
1851
1994
|
- All tests pass
|
|
1852
1995
|
- Code follows existing style
|
|
1853
1996
|
- New features include tests
|
|
1854
1997
|
- Documentation is updated
|
|
1855
1998
|
|
|
1999
|
+
[![Contributors]][contributors_url] [![Sponsor shinrai]][sponsor_url]
|
|
2000
|
+
|
|
1856
2001
|
---
|
|
1857
2002
|
|
|
1858
|
-
|
|
2003
|
+
## 🔗 Links
|
|
2004
|
+
|
|
2005
|
+
- **npm**: [@cldmv/holdmytask](https://www.npmjs.com/package/@cldmv/holdmytask)
|
|
2006
|
+
- **GitHub**: [CLDMV/holdmytask](https://github.com/CLDMV/holdmytask)
|
|
2007
|
+
- **Issues**: [GitHub Issues](https://github.com/CLDMV/holdmytask/issues)
|
|
2008
|
+
- **Changelog**: [docs/changelog/](https://github.com/CLDMV/holdmytask/tree/master/docs/changelog/)
|
|
2009
|
+
- **Releases**: [GitHub Releases](https://github.com/CLDMV/holdmytask/releases)
|
|
2010
|
+
|
|
2011
|
+
---
|
|
2012
|
+
|
|
2013
|
+
## 📄 License
|
|
2014
|
+
|
|
2015
|
+
[![GitHub license]][github_license_url] [![npm license]][npm_license_url]
|
|
2016
|
+
|
|
2017
|
+
Apache-2.0 © Shinrai / CLDMV — see [LICENSE](https://github.com/CLDMV/holdmytask/blob/HEAD/LICENSE) for details.
|
|
2018
|
+
|
|
2019
|
+
[npm version]: https://img.shields.io/npm/v/%40cldmv%2Fholdmytask.svg?style=for-the-badge&logo=npm&logoColor=white&labelColor=CB3837
|
|
2020
|
+
[npm_version_url]: https://www.npmjs.com/package/@cldmv/holdmytask
|
|
2021
|
+
[last commit]: https://img.shields.io/github/last-commit/CLDMV/holdmytask?style=for-the-badge&logo=github&logoColor=white&labelColor=181717
|
|
2022
|
+
[last_commit_url]: https://github.com/CLDMV/holdmytask/commits
|
|
2023
|
+
[npm last update]: https://img.shields.io/npm/last-update/%40cldmv%2Fholdmytask?style=for-the-badge&logo=npm&logoColor=white&labelColor=CB3837
|
|
2024
|
+
[npm_last_update_url]: https://www.npmjs.com/package/@cldmv/holdmytask
|
|
2025
|
+
[codefactor]: https://img.shields.io/codefactor/grade/github/CLDMV/holdmytask?style=for-the-badge&logo=codefactor&logoColor=white&labelColor=F44A6A
|
|
2026
|
+
[codefactor_url]: https://www.codefactor.io/repository/github/cldmv/holdmytask
|
|
2027
|
+
[openssf scorecard]: https://img.shields.io/ossf-scorecard/github.com/CLDMV/holdmytask?style=for-the-badge&label=OpenSSF%20Scorecard
|
|
2028
|
+
[ossf_scorecard_url]: https://scorecard.dev/viewer/?uri=github.com/CLDMV/holdmytask
|
|
2029
|
+
[npms.io score]: https://img.shields.io/npms-io/final-score/%40cldmv%2Fholdmytask?style=for-the-badge&logo=npms&logoColor=white&labelColor=0B5D57
|
|
2030
|
+
[npms_url]: https://npms.io/search?q=%40cldmv%2Fholdmytask
|
|
2031
|
+
[npm downloads]: https://img.shields.io/npm/dm/%40cldmv%2Fholdmytask.svg?style=for-the-badge&logo=npm&logoColor=white&labelColor=CB3837
|
|
2032
|
+
[npm_downloads_url]: https://www.npmjs.com/package/@cldmv/holdmytask
|
|
2033
|
+
[github downloads]: https://img.shields.io/github/downloads/CLDMV/holdmytask/total?style=for-the-badge&logo=github&logoColor=white&labelColor=181717
|
|
2034
|
+
[github_downloads_url]: https://github.com/CLDMV/holdmytask/releases
|
|
2035
|
+
[npm unpacked size]: https://img.shields.io/npm/unpacked-size/%40cldmv%2Fholdmytask.svg?style=for-the-badge&logo=npm&logoColor=white&labelColor=CB3837
|
|
2036
|
+
[npm_size_url]: https://www.npmjs.com/package/@cldmv/holdmytask
|
|
2037
|
+
[repo size]: https://img.shields.io/github/repo-size/CLDMV/holdmytask?style=for-the-badge&logo=github&logoColor=white&labelColor=181717
|
|
2038
|
+
[repo_size_url]: https://github.com/CLDMV/holdmytask
|
|
2039
|
+
[github license]: https://img.shields.io/github/license/CLDMV/holdmytask.svg?style=for-the-badge&logo=github&logoColor=white&labelColor=181717
|
|
2040
|
+
[github_license_url]: https://github.com/CLDMV/holdmytask/blob/HEAD/LICENSE
|
|
2041
|
+
[npm license]: https://img.shields.io/npm/l/%40cldmv%2Fholdmytask.svg?style=for-the-badge&logo=npm&logoColor=white&labelColor=CB3837
|
|
2042
|
+
[npm_license_url]: https://www.npmjs.com/package/@cldmv/holdmytask
|
|
2043
|
+
[coverage]: https://img.shields.io/endpoint?url=https%3A%2F%2Fraw.githubusercontent.com%2FCLDMV%2Fholdmytask%2Fbadges%2Fcoverage.json&style=for-the-badge&logo=vitest&logoColor=white
|
|
2044
|
+
[coverage_url]: https://github.com/CLDMV/holdmytask/blob/badges/coverage.json
|
|
2045
|
+
[contributors]: https://img.shields.io/github/contributors/CLDMV/holdmytask.svg?style=for-the-badge&logo=github&logoColor=white&labelColor=181717
|
|
2046
|
+
[contributors_url]: https://github.com/CLDMV/holdmytask/graphs/contributors
|
|
2047
|
+
[sponsor shinrai]: https://img.shields.io/github/sponsors/shinrai?style=for-the-badge&logo=githubsponsors&logoColor=white&labelColor=EA4AAA&label=Sponsor
|
|
2048
|
+
[sponsor_url]: https://github.com/sponsors/shinrai
|