@cldmv/holdmytask 2.0.4 → 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 +356 -181
- package/dist/hold-my-task.mjs +150 -31
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,31 +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.
|
|
8
|
+
|
|
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
|
+
---
|
|
12
16
|
|
|
13
17
|
## ✨ What's New
|
|
14
18
|
|
|
15
|
-
### Latest: v2.0.
|
|
19
|
+
### Latest: v2.0.5 (October 2026)
|
|
16
20
|
|
|
17
|
-
- **
|
|
18
|
-
- **
|
|
19
|
-
-
|
|
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)
|
|
20
25
|
|
|
21
26
|
### Recent Releases
|
|
22
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))
|
|
23
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))
|
|
24
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))
|
|
25
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))
|
|
26
|
-
- **v2.0.0** (August 2026) — `HoldMyTask` and its aliases are the real class again (breaking for code adapted to v1.6.1's factories); scheduler deadlock fix; `holdmytask-dev` export condition ([Changelog](https://github.com/CLDMV/holdmytask/blob/master/docs/changelog/v2/v2.0.0.md))
|
|
27
32
|
|
|
28
|
-
|
|
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
|
|
29
38
|
|
|
30
39
|
- **Smart scheduling** - Dynamic timeout-based scheduling for optimal performance
|
|
31
40
|
- **Task coalescing** - Intelligent merging of similar tasks for efficiency
|
|
@@ -40,13 +49,72 @@ A tiny, dependency-free task queue for Node.js that executes tasks with priority
|
|
|
40
49
|
- **TypeScript ready** - Full type definitions included
|
|
41
50
|
- **Zero dependencies** - Lightweight and fast
|
|
42
51
|
|
|
52
|
+
---
|
|
53
|
+
|
|
43
54
|
## 📦 Installation
|
|
44
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
|
+
|
|
45
63
|
```bash
|
|
46
64
|
npm install @cldmv/holdmytask
|
|
47
65
|
```
|
|
48
66
|
|
|
49
|
-
|
|
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
|
|
50
118
|
|
|
51
119
|
The library provides flexible import options for different use cases:
|
|
52
120
|
|
|
@@ -62,13 +130,10 @@ import HoldMyTask from "@cldmv/holdmytask";
|
|
|
62
130
|
// Import everything
|
|
63
131
|
import * as HoldMyTaskLib from "@cldmv/holdmytask";
|
|
64
132
|
// Uses optimized distribution files
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
### Development Source Access
|
|
68
133
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
|
72
137
|
```
|
|
73
138
|
|
|
74
139
|
### Common Queue System Aliases
|
|
@@ -89,54 +154,27 @@ import {
|
|
|
89
154
|
// All aliases are functionally identical
|
|
90
155
|
const myQueue = new queue({ concurrency: 5 });
|
|
91
156
|
const stdQueue = new Queue({ concurrency: 5 });
|
|
92
|
-
const manager = new TaskManager({ priorities: {
|
|
157
|
+
const manager = new TaskManager({ priorities: { 1: { postDelay: 100 } } });
|
|
93
158
|
```
|
|
94
159
|
|
|
95
|
-
|
|
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
|
|
96
163
|
|
|
97
164
|
```javascript
|
|
98
|
-
import { HoldMyTask } from "@cldmv/holdmytask/
|
|
99
|
-
//
|
|
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
|
|
100
167
|
```
|
|
101
168
|
|
|
102
|
-
|
|
169
|
+
To run against the unbuilt source (from a checkout of this repository), start Node.js with the `holdmytask-dev` export condition:
|
|
103
170
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
```
|
|
107
|
-
import { HoldMyTask } from "@cldmv/holdmytask";
|
|
108
|
-
|
|
109
|
-
const queue = new HoldMyTask({
|
|
110
|
-
concurrency: 2,
|
|
111
|
-
delays: { 1: 100, 2: 200 }, // 100ms delay after priority 1 tasks, 200ms after priority 2
|
|
112
|
-
coalescingWindowDuration: 200, // Group similar tasks within 200ms
|
|
113
|
-
coalescingMaxDelay: 1000 // Force execution after 1000ms max
|
|
114
|
-
});
|
|
171
|
+
```bash
|
|
172
|
+
node --conditions=holdmytask-dev your-script.mjs
|
|
173
|
+
```
|
|
115
174
|
|
|
116
|
-
|
|
117
|
-
queue.enqueue(
|
|
118
|
-
async (signal) => {
|
|
119
|
-
// Your task logic here
|
|
120
|
-
return "task result";
|
|
121
|
-
},
|
|
122
|
-
(error, result) => {
|
|
123
|
-
if (error) {
|
|
124
|
-
console.error("Task failed:", error);
|
|
125
|
-
} else {
|
|
126
|
-
console.log("Task completed:", result);
|
|
127
|
-
}
|
|
128
|
-
},
|
|
129
|
-
{ priority: 1, timeout: 5000 }
|
|
130
|
-
);
|
|
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.
|
|
131
176
|
|
|
132
|
-
|
|
133
|
-
const results = await Promise.all([
|
|
134
|
-
queue.enqueue(async () => updateUI(), { coalescingKey: "ui.update" }),
|
|
135
|
-
queue.enqueue(async () => updateUI(), { coalescingKey: "ui.update" }),
|
|
136
|
-
queue.enqueue(async () => updateUI(), { coalescingKey: "ui.update" })
|
|
137
|
-
]);
|
|
138
|
-
// Only one updateUI() call executes, all three promises resolve with the same result
|
|
139
|
-
```
|
|
177
|
+
---
|
|
140
178
|
|
|
141
179
|
## 🔄 Promise API
|
|
142
180
|
|
|
@@ -180,6 +218,8 @@ queue.enqueue(task1, callback, options);
|
|
|
180
218
|
const result = await queue.enqueue(task2, options);
|
|
181
219
|
```
|
|
182
220
|
|
|
221
|
+
---
|
|
222
|
+
|
|
183
223
|
## 🏗️ Async Constructor Pattern
|
|
184
224
|
|
|
185
225
|
HoldMyTask works excellently with async constructor patterns for classes that need initialization:
|
|
@@ -241,6 +281,8 @@ This pattern is particularly useful for:
|
|
|
241
281
|
- **Resource acquisition**: Set up required resources in a controlled manner
|
|
242
282
|
- **Dependency injection**: Initialize dependencies in the correct order
|
|
243
283
|
|
|
284
|
+
---
|
|
285
|
+
|
|
244
286
|
## 📚 API Reference
|
|
245
287
|
|
|
246
288
|
### Constructor
|
|
@@ -251,25 +293,22 @@ const queue = new HoldMyTask(options?)
|
|
|
251
293
|
|
|
252
294
|
### Async Initialization Pattern
|
|
253
295
|
|
|
254
|
-
|
|
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:
|
|
255
297
|
|
|
256
298
|
```javascript
|
|
257
299
|
// Create instance with async initialization
|
|
258
|
-
const
|
|
300
|
+
const queue = await new HoldMyTask({
|
|
259
301
|
concurrency: 5,
|
|
260
302
|
maxQueue: 100,
|
|
261
303
|
sync: false // Enable async initialization
|
|
262
304
|
});
|
|
263
305
|
|
|
264
|
-
//
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
// Wait for initialization to complete
|
|
269
|
-
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));
|
|
270
309
|
```
|
|
271
310
|
|
|
272
|
-
|
|
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.
|
|
273
312
|
|
|
274
313
|
**Options:**
|
|
275
314
|
|
|
@@ -279,26 +318,25 @@ This pattern is particularly useful when you need to handle initialization error
|
|
|
279
318
|
- `autoStart` (boolean, default: true) - Whether to start processing immediately
|
|
280
319
|
- `defaultPriority` (number, default: 0) - Default task priority
|
|
281
320
|
- `maxQueue` (number, default: Infinity) - Maximum queued tasks. Use `-1` for unlimited queue capacity (equivalent to `Infinity`)
|
|
282
|
-
- `
|
|
283
|
-
- `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)
|
|
284
322
|
- `concurrency` (number) - Maximum concurrent tasks for this priority (defaults to global concurrency limit)
|
|
285
|
-
- `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))
|
|
286
324
|
- `startDelay` (number) - Delay before task execution (pre-execution delay)
|
|
287
325
|
- `coalescing` (object) - Enhanced coalescing configuration
|
|
288
326
|
- `defaults` (object) - Default settings for all coalescing keys
|
|
289
327
|
- `windowDuration` (number, default: 200) - Window duration in milliseconds
|
|
290
328
|
- `maxDelay` (number, default: 1000) - Maximum delay before forcing execution
|
|
291
|
-
- `
|
|
292
|
-
- `
|
|
293
|
-
- `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`
|
|
294
332
|
- `multipleCallbacks` (boolean, default: false) - Whether to call multiple callbacks
|
|
295
|
-
- `keys` (object) - Per-key configuration overrides: `{ [key]: { windowDuration, maxDelay,
|
|
296
|
-
- `
|
|
297
|
-
- `
|
|
298
|
-
- `coalescingResolveAllPromises` (boolean, default: true) - **DEPRECATED:** Use `coalescing.defaults.resolveAllPromises`
|
|
299
|
-
- `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
|
|
300
336
|
- `now` (function) - Injectable clock for testing
|
|
301
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
|
+
|
|
302
340
|
### Methods
|
|
303
341
|
|
|
304
342
|
#### `enqueue(task, callback?, options?)`
|
|
@@ -314,12 +352,12 @@ Adds a task to the queue.
|
|
|
314
352
|
**Task Options:**
|
|
315
353
|
|
|
316
354
|
- `priority` (number) - Task priority (higher = more important)
|
|
317
|
-
- `
|
|
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
|
|
318
356
|
- `bypassDelay` (boolean) - If true, skip any active delay period and start immediately
|
|
319
357
|
- `timeout` (number) - Timeout in milliseconds
|
|
320
358
|
- `signal` (AbortSignal) - External abort signal
|
|
321
359
|
- `timestamp` (number) - Absolute execution timestamp
|
|
322
|
-
- `
|
|
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
|
|
323
361
|
- `coalescingKey` (string) - Tasks with the same coalescing key can be merged for efficiency
|
|
324
362
|
- `mustRunBy` (number) - Absolute timestamp by which the task must execute (overrides coalescing delays)
|
|
325
363
|
- `metadata` (any) - Custom metadata attached to the task. Individual metadata is always directly accessible via the returned task handle
|
|
@@ -439,10 +477,10 @@ queue.enqueue(task2, { id: "duplicate" }); // throws: Task ID "duplicate" alread
|
|
|
439
477
|
|
|
440
478
|
- `configurePriority(priority, config)` - Configure or update priority-specific settings
|
|
441
479
|
- `priority` (string|number) - Priority level to configure
|
|
442
|
-
- `config` (object) - Configuration: `{
|
|
480
|
+
- `config` (object) - Configuration: `{ postDelay?, startDelay? }`
|
|
443
481
|
- `configureCoalescingKey(key, config)` - Configure or update coalescing key settings
|
|
444
482
|
- `key` (string) - Coalescing key to configure
|
|
445
|
-
- `config` (object) - Configuration: `{ windowDuration?, maxDelay?,
|
|
483
|
+
- `config` (object) - Configuration: `{ windowDuration?, maxDelay?, postDelay?, startDelay?, multipleCallbacks?, resolveAllPromises? }`
|
|
446
484
|
- `getPriorityConfig(priority, taskOptions?)` - Get effective configuration for a specific priority
|
|
447
485
|
- `getPriorityConfigurations()` - Get all configured priorities and their settings
|
|
448
486
|
- `getCoalescingConfig(coalescingKey, taskOptions?)` - Get effective configuration for a specific coalescing key
|
|
@@ -575,7 +613,7 @@ const queue = new HoldMyTask({ concurrency: 2 });
|
|
|
575
613
|
|
|
576
614
|
// Add some tasks
|
|
577
615
|
queue.enqueue(async () => "task1");
|
|
578
|
-
queue.enqueue(async () => "task2", { priority: 2,
|
|
616
|
+
queue.enqueue(async () => "task2", { priority: 2, startDelay: 5000 });
|
|
579
617
|
|
|
580
618
|
// Check initial state
|
|
581
619
|
console.log("Initial state:");
|
|
@@ -617,28 +655,66 @@ queue.on("warning", (warning) => {
|
|
|
617
655
|
});
|
|
618
656
|
```
|
|
619
657
|
|
|
620
|
-
|
|
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 }`.
|
|
661
|
+
|
|
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.
|
|
621
665
|
|
|
622
|
-
|
|
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
|
|
623
680
|
|
|
624
681
|
```javascript
|
|
625
682
|
const queue = new HoldMyTask({
|
|
626
|
-
delays: { 1: 100 }, // Deprecated
|
|
627
|
-
coalescingWindowDuration: 200 // Deprecated
|
|
683
|
+
delays: { 1: 100 }, // Deprecated: use priorities
|
|
684
|
+
coalescingWindowDuration: 200 // Deprecated: use coalescing.defaults.windowDuration
|
|
628
685
|
});
|
|
629
686
|
|
|
630
687
|
queue.on("warning", (warning) => {
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
688
|
+
if (warning.type === "deprecation") {
|
|
689
|
+
console.warn(`${warning.message} (${warning.deprecated} -> ${warning.replacement})`);
|
|
690
|
+
}
|
|
634
691
|
});
|
|
635
692
|
|
|
636
693
|
// Output:
|
|
637
|
-
//
|
|
638
|
-
// Use
|
|
639
|
-
// 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)
|
|
640
696
|
```
|
|
641
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
|
+
|
|
642
718
|
## ⚙️ Concurrency & Delays Interaction
|
|
643
719
|
|
|
644
720
|
Understanding how concurrency and delays work together is crucial for optimal queue behavior:
|
|
@@ -648,7 +724,7 @@ Understanding how concurrency and delays work together is crucial for optimal qu
|
|
|
648
724
|
```javascript
|
|
649
725
|
const queue = new HoldMyTask({
|
|
650
726
|
concurrency: 3, // Up to 3 tasks can run simultaneously
|
|
651
|
-
|
|
727
|
+
priorities: { 1: { postDelay: 500 }, 2: { postDelay: 1000 } }
|
|
652
728
|
});
|
|
653
729
|
```
|
|
654
730
|
|
|
@@ -661,7 +737,7 @@ const queue = new HoldMyTask({
|
|
|
661
737
|
### Timing Examples
|
|
662
738
|
|
|
663
739
|
```javascript
|
|
664
|
-
// Timeline with concurrency: 2,
|
|
740
|
+
// Timeline with concurrency: 2, priorities: { 1: { postDelay: 1000 } }
|
|
665
741
|
|
|
666
742
|
// 10:00:00 - Start: TaskA (pri 1), TaskB (pri 1) - both running
|
|
667
743
|
// 10:00:02 - TaskA completes → 1000ms delay starts, TaskB still running
|
|
@@ -709,10 +785,6 @@ HoldMyTask uses a sophisticated dual-heap scheduling system for optimal performa
|
|
|
709
785
|
|
|
710
786
|
This architecture enables handling thousands of tasks with precise timing control while maintaining excellent performance.
|
|
711
787
|
|
|
712
|
-
[](https://www.codefactor.io/repository/github/cldmv/holdmytask) [](https://npms.io/search?q=%40cldmv%2Fholdmytask)
|
|
713
|
-
|
|
714
|
-
[](https://www.npmjs.com/package/@cldmv/holdmytask) [](https://github.com/CLDMV/holdmytask)
|
|
715
|
-
|
|
716
788
|
### Unlimited Queue Capacity
|
|
717
789
|
|
|
718
790
|
For scenarios requiring unlimited task queuing, you can set `maxQueue` to `-1`:
|
|
@@ -740,6 +812,8 @@ for (let i = 0; i < 100000; i++) {
|
|
|
740
812
|
|
|
741
813
|
While `-1` allows unlimited queuing, be mindful of memory usage with very large task sets. Each queued task consumes memory until executed.
|
|
742
814
|
|
|
815
|
+
---
|
|
816
|
+
|
|
743
817
|
## 🎯 Advanced Features
|
|
744
818
|
|
|
745
819
|
### Priority System
|
|
@@ -769,50 +843,50 @@ HoldMyTask supports comprehensive priority and coalescing configuration for soph
|
|
|
769
843
|
```javascript
|
|
770
844
|
const queue = new HoldMyTask({
|
|
771
845
|
concurrency: 3,
|
|
772
|
-
// Priority-specific defaults
|
|
846
|
+
// Priority-specific defaults
|
|
773
847
|
priorities: {
|
|
774
|
-
1: {
|
|
775
|
-
2: {
|
|
776
|
-
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
|
|
777
851
|
},
|
|
778
852
|
// Enhanced coalescing with per-key settings
|
|
779
853
|
coalescing: {
|
|
780
854
|
defaults: {
|
|
781
855
|
windowDuration: 200,
|
|
782
856
|
maxDelay: 1000,
|
|
783
|
-
|
|
784
|
-
|
|
857
|
+
postDelay: 75, // Default completion delay for coalescing tasks
|
|
858
|
+
startDelay: 25, // Default start delay for coalescing tasks
|
|
785
859
|
resolveAllPromises: true
|
|
786
860
|
},
|
|
787
861
|
keys: {
|
|
788
862
|
"ui.update": {
|
|
789
863
|
windowDuration: 100,
|
|
790
864
|
maxDelay: 500,
|
|
791
|
-
|
|
792
|
-
|
|
865
|
+
postDelay: 25, // Fast UI updates
|
|
866
|
+
startDelay: 0
|
|
793
867
|
},
|
|
794
868
|
"api.batch": {
|
|
795
869
|
windowDuration: 1000,
|
|
796
870
|
maxDelay: 5000,
|
|
797
|
-
|
|
798
|
-
|
|
871
|
+
postDelay: 200, // Slower API operations
|
|
872
|
+
startDelay: 100
|
|
799
873
|
}
|
|
800
874
|
}
|
|
801
875
|
}
|
|
802
876
|
});
|
|
803
877
|
|
|
804
878
|
// Dynamic configuration
|
|
805
|
-
queue.configurePriority(4, {
|
|
879
|
+
queue.configurePriority(4, { postDelay: 300, startDelay: 75 });
|
|
806
880
|
queue.configureCoalescingKey("data.sync", {
|
|
807
881
|
windowDuration: 800,
|
|
808
882
|
maxDelay: 3000,
|
|
809
|
-
|
|
810
|
-
|
|
883
|
+
postDelay: 150,
|
|
884
|
+
startDelay: 50
|
|
811
885
|
});
|
|
812
886
|
|
|
813
887
|
// Get configuration information
|
|
814
888
|
const priority4Config = queue.getPriorityConfig(4);
|
|
815
|
-
console.log(`Priority 4: ${priority4Config.
|
|
889
|
+
console.log(`Priority 4: ${priority4Config.postDelay}ms delay, ${priority4Config.startDelay}ms start delay`);
|
|
816
890
|
|
|
817
891
|
const dataSyncConfig = queue.getCoalescingConfig("data.sync");
|
|
818
892
|
console.log(`Data sync: ${dataSyncConfig.windowDuration}ms window, ${dataSyncConfig.maxDelay}ms max delay`);
|
|
@@ -832,7 +906,7 @@ console.log("All coalescing configs:", allCoalescingKeys);
|
|
|
832
906
|
4. Coalescing defaults
|
|
833
907
|
5. System defaults (lowest priority)
|
|
834
908
|
|
|
835
|
-
**Backward Compatibility:**
|
|
909
|
+
**Backward Compatibility:** Deprecated option names are converted automatically; see [Deprecated Options](#deprecated-options).
|
|
836
910
|
|
|
837
911
|
### Smart Scheduling
|
|
838
912
|
|
|
@@ -983,10 +1057,10 @@ Priority delays create "cool-down" periods after task completion based on the co
|
|
|
983
1057
|
```javascript
|
|
984
1058
|
const queue = new HoldMyTask({
|
|
985
1059
|
concurrency: 1,
|
|
986
|
-
|
|
987
|
-
1: 1000, // 1 second delay after priority 1 tasks complete
|
|
988
|
-
2: 500, // 500ms delay after priority 2 tasks complete
|
|
989
|
-
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)
|
|
990
1064
|
}
|
|
991
1065
|
});
|
|
992
1066
|
|
|
@@ -997,22 +1071,22 @@ const queue = new HoldMyTask({
|
|
|
997
1071
|
// → Next task can't start until 10:00:07.5 (500ms delay)
|
|
998
1072
|
|
|
999
1073
|
// Override delay for specific task
|
|
1000
|
-
queue.enqueue(task, callback, { priority: 1,
|
|
1074
|
+
queue.enqueue(task, callback, { priority: 1, postDelay: 200 }); // Uses 200ms instead of 1000ms
|
|
1001
1075
|
|
|
1002
1076
|
// Set zero delay for specific task
|
|
1003
|
-
queue.enqueue(task, callback, { priority: 1,
|
|
1077
|
+
queue.enqueue(task, callback, { priority: 1, postDelay: 0 }); // No delay after this task
|
|
1004
1078
|
```
|
|
1005
1079
|
|
|
1006
1080
|
### Delay Bypass - Emergency Task Injection
|
|
1007
1081
|
|
|
1008
|
-
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.
|
|
1009
1083
|
|
|
1010
1084
|
#### Bypass Behavior
|
|
1011
1085
|
|
|
1012
1086
|
```javascript
|
|
1013
1087
|
const queue = new HoldMyTask({
|
|
1014
1088
|
concurrency: 1,
|
|
1015
|
-
|
|
1089
|
+
priorities: { 1: { postDelay: 1000 } } // 1 second delay after priority 1 tasks
|
|
1016
1090
|
});
|
|
1017
1091
|
|
|
1018
1092
|
// Timeline example:
|
|
@@ -1030,7 +1104,7 @@ queue.enqueue(urgentTaskC, callback, {
|
|
|
1030
1104
|
// Alternative bypass syntax
|
|
1031
1105
|
queue.enqueue(emergencyTaskD, callback, {
|
|
1032
1106
|
priority: 1,
|
|
1033
|
-
|
|
1107
|
+
postDelay: -1 // Same as bypassDelay: true
|
|
1034
1108
|
});
|
|
1035
1109
|
```
|
|
1036
1110
|
|
|
@@ -1039,7 +1113,7 @@ queue.enqueue(emergencyTaskD, callback, {
|
|
|
1039
1113
|
```javascript
|
|
1040
1114
|
const queue = new HoldMyTask({
|
|
1041
1115
|
concurrency: 2,
|
|
1042
|
-
|
|
1116
|
+
priorities: { 1: { postDelay: 800 }, 2: { postDelay: 400 } }
|
|
1043
1117
|
});
|
|
1044
1118
|
|
|
1045
1119
|
// Multiple tasks with different bypass behavior
|
|
@@ -1059,6 +1133,8 @@ queue.enqueue(taskD, callback, { priority: 1 }); // Waits for taskC's completion
|
|
|
1059
1133
|
- ✅ **Concurrency aware** - works correctly with multiple concurrent execution slots
|
|
1060
1134
|
- ⚠️ **Use sparingly** - frequent bypassing defeats the purpose of delay-based rate limiting
|
|
1061
1135
|
|
|
1136
|
+
---
|
|
1137
|
+
|
|
1062
1138
|
## 🔄 Task Coalescing System
|
|
1063
1139
|
|
|
1064
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.
|
|
@@ -1072,7 +1148,7 @@ When tasks with the same `coalescingKey` are enqueued within a time window, they
|
|
|
1072
1148
|
3. **One representative task** executes for the entire group
|
|
1073
1149
|
4. **All promises** in the group resolve with the same result
|
|
1074
1150
|
|
|
1075
|
-
⚠️ **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.
|
|
1076
1152
|
|
|
1077
1153
|
### 🎯 Correct Coalescing Pattern: Fire-and-Forget with Embedded Updates
|
|
1078
1154
|
|
|
@@ -1121,9 +1197,13 @@ function volumeUp() {
|
|
|
1121
1197
|
```javascript
|
|
1122
1198
|
const queue = new HoldMyTask({
|
|
1123
1199
|
concurrency: 1,
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
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
|
+
}
|
|
1127
1207
|
});
|
|
1128
1208
|
```
|
|
1129
1209
|
|
|
@@ -1143,7 +1223,7 @@ async function updateVolume(change) {
|
|
|
1143
1223
|
{
|
|
1144
1224
|
coalescingKey: "volume.update", // Tasks with same key get grouped
|
|
1145
1225
|
priority: 1,
|
|
1146
|
-
|
|
1226
|
+
postDelay: 100 // 100ms delay after completion
|
|
1147
1227
|
}
|
|
1148
1228
|
);
|
|
1149
1229
|
}
|
|
@@ -1169,8 +1249,12 @@ The correct pattern for coalescing with updates is to enqueue update tasks **fro
|
|
|
1169
1249
|
```javascript
|
|
1170
1250
|
const queue = new HoldMyTask({
|
|
1171
1251
|
concurrency: 2, // Allow volume and update tasks to run concurrently
|
|
1172
|
-
|
|
1173
|
-
|
|
1252
|
+
coalescing: {
|
|
1253
|
+
defaults: {
|
|
1254
|
+
windowDuration: 200,
|
|
1255
|
+
maxDelay: 1000
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1174
1258
|
});
|
|
1175
1259
|
|
|
1176
1260
|
// Volume commands using fire-and-forget pattern
|
|
@@ -1197,7 +1281,7 @@ function volumeUp(amount = 1) {
|
|
|
1197
1281
|
{
|
|
1198
1282
|
coalescingKey: "volume.ui.update", // UI updates get coalesced
|
|
1199
1283
|
priority: 5, // Lower priority than volume commands
|
|
1200
|
-
|
|
1284
|
+
postDelay: 100 // Brief delay after UI updates
|
|
1201
1285
|
}
|
|
1202
1286
|
);
|
|
1203
1287
|
|
|
@@ -1205,7 +1289,7 @@ function volumeUp(amount = 1) {
|
|
|
1205
1289
|
},
|
|
1206
1290
|
{
|
|
1207
1291
|
priority: 1, // High priority for user actions
|
|
1208
|
-
|
|
1292
|
+
postDelay: 100 // Brief delay after volume operations
|
|
1209
1293
|
}
|
|
1210
1294
|
);
|
|
1211
1295
|
}
|
|
@@ -1238,7 +1322,7 @@ async function processDataBatch(data) {
|
|
|
1238
1322
|
{
|
|
1239
1323
|
coalescingKey: "api.batch.process",
|
|
1240
1324
|
priority: 2,
|
|
1241
|
-
|
|
1325
|
+
startDelay: 100 // 100ms delay allows grouping
|
|
1242
1326
|
}
|
|
1243
1327
|
);
|
|
1244
1328
|
}
|
|
@@ -1255,12 +1339,16 @@ async function processDataBatch(data) {
|
|
|
1255
1339
|
|
|
1256
1340
|
### Coalescing with Explicit Timestamps
|
|
1257
1341
|
|
|
1258
|
-
For precise scheduling, use `timestamp` instead of `
|
|
1342
|
+
For precise scheduling, use `timestamp` instead of `startDelay`:
|
|
1259
1343
|
|
|
1260
1344
|
```javascript
|
|
1261
1345
|
const queue = new HoldMyTask({
|
|
1262
|
-
|
|
1263
|
-
|
|
1346
|
+
coalescing: {
|
|
1347
|
+
defaults: {
|
|
1348
|
+
windowDuration: 300,
|
|
1349
|
+
maxDelay: 2000
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1264
1352
|
});
|
|
1265
1353
|
|
|
1266
1354
|
// Schedule all updates for the same exact time
|
|
@@ -1294,8 +1382,12 @@ Control maximum delay with `mustRunBy` to ensure tasks don't wait too long:
|
|
|
1294
1382
|
|
|
1295
1383
|
```javascript
|
|
1296
1384
|
const queue = new HoldMyTask({
|
|
1297
|
-
|
|
1298
|
-
|
|
1385
|
+
coalescing: {
|
|
1386
|
+
defaults: {
|
|
1387
|
+
windowDuration: 500, // Try to group for 500ms
|
|
1388
|
+
maxDelay: 2000 // But never wait more than 2 seconds
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1299
1391
|
});
|
|
1300
1392
|
|
|
1301
1393
|
// Critical system updates
|
|
@@ -1324,8 +1416,12 @@ Control how promises resolve within coalescing groups:
|
|
|
1324
1416
|
|
|
1325
1417
|
```javascript
|
|
1326
1418
|
const queue = new HoldMyTask({
|
|
1327
|
-
|
|
1328
|
-
|
|
1419
|
+
coalescing: {
|
|
1420
|
+
defaults: {
|
|
1421
|
+
resolveAllPromises: true, // Default: all promises get the result
|
|
1422
|
+
windowDuration: 200
|
|
1423
|
+
}
|
|
1424
|
+
}
|
|
1329
1425
|
});
|
|
1330
1426
|
|
|
1331
1427
|
// Mode 1: All promises resolve (default behavior)
|
|
@@ -1336,18 +1432,23 @@ const results1 = await Promise.all([
|
|
|
1336
1432
|
]);
|
|
1337
1433
|
// All three promises resolve with the same result
|
|
1338
1434
|
|
|
1339
|
-
// Mode 2: Only representative promise resolves
|
|
1435
|
+
// Mode 2: Only the representative (newest) task's promise resolves
|
|
1340
1436
|
const queue2 = new HoldMyTask({
|
|
1341
|
-
|
|
1342
|
-
|
|
1437
|
+
coalescing: {
|
|
1438
|
+
defaults: {
|
|
1439
|
+
resolveAllPromises: false,
|
|
1440
|
+
windowDuration: 200
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1343
1443
|
});
|
|
1344
1444
|
|
|
1345
|
-
const
|
|
1346
|
-
queue2.enqueue(task, { coalescingKey: "test" }), //
|
|
1347
|
-
queue2.enqueue(task, { coalescingKey: "test" }), //
|
|
1348
|
-
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
|
|
1349
1449
|
]);
|
|
1350
|
-
// 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)
|
|
1351
1452
|
```
|
|
1352
1453
|
|
|
1353
1454
|
### Real-World Coalescing Patterns
|
|
@@ -1359,8 +1460,12 @@ class VolumeController {
|
|
|
1359
1460
|
constructor() {
|
|
1360
1461
|
this.queue = new HoldMyTask({
|
|
1361
1462
|
concurrency: 2, // Allow volume and update tasks concurrently
|
|
1362
|
-
|
|
1363
|
-
|
|
1463
|
+
coalescing: {
|
|
1464
|
+
defaults: {
|
|
1465
|
+
windowDuration: 200,
|
|
1466
|
+
maxDelay: 1000
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1364
1469
|
});
|
|
1365
1470
|
}
|
|
1366
1471
|
|
|
@@ -1383,7 +1488,7 @@ class VolumeController {
|
|
|
1383
1488
|
{
|
|
1384
1489
|
coalescingKey: "volume.ui.update", // Updates coalesce together
|
|
1385
1490
|
priority: 3, // Lower priority than volume changes
|
|
1386
|
-
|
|
1491
|
+
postDelay: 100 // Brief delay after UI updates
|
|
1387
1492
|
}
|
|
1388
1493
|
);
|
|
1389
1494
|
|
|
@@ -1391,7 +1496,7 @@ class VolumeController {
|
|
|
1391
1496
|
},
|
|
1392
1497
|
{
|
|
1393
1498
|
priority: 1, // High priority for user actions
|
|
1394
|
-
|
|
1499
|
+
postDelay: 50 // Brief delay between volume operations
|
|
1395
1500
|
}
|
|
1396
1501
|
);
|
|
1397
1502
|
}
|
|
@@ -1405,8 +1510,12 @@ class APIBatcher {
|
|
|
1405
1510
|
constructor() {
|
|
1406
1511
|
this.queue = new HoldMyTask({
|
|
1407
1512
|
concurrency: 2,
|
|
1408
|
-
|
|
1409
|
-
|
|
1513
|
+
coalescing: {
|
|
1514
|
+
defaults: {
|
|
1515
|
+
windowDuration: 300,
|
|
1516
|
+
maxDelay: 1500
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1410
1519
|
});
|
|
1411
1520
|
}
|
|
1412
1521
|
|
|
@@ -1436,8 +1545,12 @@ Real-world performance improvements with the embedded update pattern:
|
|
|
1436
1545
|
// Fire-and-forget pattern with embedded coalescing updates
|
|
1437
1546
|
const queue = new HoldMyTask({
|
|
1438
1547
|
concurrency: 2,
|
|
1439
|
-
|
|
1440
|
-
|
|
1548
|
+
coalescing: {
|
|
1549
|
+
defaults: {
|
|
1550
|
+
windowDuration: 200,
|
|
1551
|
+
maxDelay: 1000
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1441
1554
|
});
|
|
1442
1555
|
|
|
1443
1556
|
// Volume control example - realistic embedded pattern
|
|
@@ -1476,9 +1589,11 @@ function processVolumeCommands() {
|
|
|
1476
1589
|
// - Total time: ~25-30 seconds (vs 50+ seconds without coalescing)
|
|
1477
1590
|
```
|
|
1478
1591
|
|
|
1479
|
-
|
|
1592
|
+
---
|
|
1593
|
+
|
|
1594
|
+
## ⏱️ Timeouts
|
|
1480
1595
|
|
|
1481
|
-
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)):
|
|
1482
1597
|
|
|
1483
1598
|
**Callback API:**
|
|
1484
1599
|
|
|
@@ -1518,6 +1633,8 @@ try {
|
|
|
1518
1633
|
}
|
|
1519
1634
|
```
|
|
1520
1635
|
|
|
1636
|
+
---
|
|
1637
|
+
|
|
1521
1638
|
## 🛑 AbortController Support
|
|
1522
1639
|
|
|
1523
1640
|
The library uses `AbortController` for cooperative task cancellation. This allows tasks to be cancelled gracefully without forcing termination.
|
|
@@ -1679,6 +1796,8 @@ try {
|
|
|
1679
1796
|
- **Cooperative**: Tasks must actively check and respond to the abort signal
|
|
1680
1797
|
- **Async operations**: Only cancellable if the underlying operation supports AbortSignal
|
|
1681
1798
|
|
|
1799
|
+
---
|
|
1800
|
+
|
|
1682
1801
|
## 📝 Examples
|
|
1683
1802
|
|
|
1684
1803
|
### Basic Usage
|
|
@@ -1712,10 +1831,10 @@ function processUser(userId) {
|
|
|
1712
1831
|
```javascript
|
|
1713
1832
|
const queue = new HoldMyTask({
|
|
1714
1833
|
concurrency: 1,
|
|
1715
|
-
|
|
1716
|
-
1: 1000, // 1 second between high-priority tasks
|
|
1717
|
-
2: 100, // 100ms between medium-priority tasks
|
|
1718
|
-
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
|
|
1719
1838
|
}
|
|
1720
1839
|
});
|
|
1721
1840
|
|
|
@@ -1732,7 +1851,7 @@ queue.enqueue(mediumTask2, callback, { priority: 2 });
|
|
|
1732
1851
|
```javascript
|
|
1733
1852
|
const queue = new HoldMyTask({
|
|
1734
1853
|
concurrency: 5,
|
|
1735
|
-
|
|
1854
|
+
priorities: { 1: { postDelay: 50 } } // Small delay between batches
|
|
1736
1855
|
});
|
|
1737
1856
|
|
|
1738
1857
|
async function processBatch(items) {
|
|
@@ -1767,10 +1886,10 @@ Real-world scenario: API rate limiting with emergency override capability.
|
|
|
1767
1886
|
```javascript
|
|
1768
1887
|
const apiQueue = new HoldMyTask({
|
|
1769
1888
|
concurrency: 2,
|
|
1770
|
-
|
|
1771
|
-
1: 2000, // 2 second delay between API calls (rate limiting)
|
|
1772
|
-
2: 5000, // 5 second delay for heavy operations
|
|
1773
|
-
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
|
|
1774
1893
|
}
|
|
1775
1894
|
});
|
|
1776
1895
|
|
|
@@ -1793,7 +1912,7 @@ apiQueue.enqueue(processSecurityAlert, handleEmergency, {
|
|
|
1793
1912
|
// Alternative syntax for bypass
|
|
1794
1913
|
apiQueue.enqueue(emergencyShutdown, handleEmergency, {
|
|
1795
1914
|
priority: 1,
|
|
1796
|
-
|
|
1915
|
+
postDelay: -1, // Same as bypassDelay: true
|
|
1797
1916
|
metadata: { action: "shutdown" }
|
|
1798
1917
|
});
|
|
1799
1918
|
|
|
@@ -1813,23 +1932,26 @@ setInterval(() => {
|
|
|
1813
1932
|
}, 1000);
|
|
1814
1933
|
```
|
|
1815
1934
|
|
|
1935
|
+
---
|
|
1936
|
+
|
|
1816
1937
|
## 🧪 Testing & Development
|
|
1817
1938
|
|
|
1818
1939
|
```bash
|
|
1819
|
-
# Run tests
|
|
1940
|
+
# Run tests (vitest suite + CommonJS entry tests)
|
|
1820
1941
|
npm test
|
|
1821
1942
|
|
|
1822
1943
|
# Run tests with coverage
|
|
1823
|
-
npm run
|
|
1944
|
+
npm run coverage
|
|
1824
1945
|
|
|
1825
1946
|
# Run tests in watch mode
|
|
1826
|
-
npm run test:
|
|
1947
|
+
npm run test:watch
|
|
1827
1948
|
|
|
1828
|
-
# Lint
|
|
1949
|
+
# Lint and format check
|
|
1829
1950
|
npm run lint
|
|
1951
|
+
npm run format:check
|
|
1830
1952
|
|
|
1831
|
-
# Build for publishing
|
|
1832
|
-
npm run build
|
|
1953
|
+
# Build types, type-check, and build for publishing
|
|
1954
|
+
npm run build:ci
|
|
1833
1955
|
```
|
|
1834
1956
|
|
|
1835
1957
|
### Testing with Injectable Clock
|
|
@@ -1853,21 +1975,74 @@ console.log(Date.now()); // Returns actual system time
|
|
|
1853
1975
|
queue.enqueue(task, { timestamp: queue.now() + 5000 }); // 5 seconds from mock time
|
|
1854
1976
|
```
|
|
1855
1977
|
|
|
1856
|
-
|
|
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
|
|
1857
1985
|
|
|
1858
|
-
[![
|
|
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]
|
|
1859
1987
|
|
|
1860
|
-
|
|
1988
|
+
---
|
|
1861
1989
|
|
|
1862
1990
|
## 🤝 Contributing
|
|
1863
1991
|
|
|
1864
|
-
Contributions welcome! Please ensure:
|
|
1992
|
+
Contributions are welcome! Please ensure:
|
|
1865
1993
|
|
|
1866
1994
|
- All tests pass
|
|
1867
1995
|
- Code follows existing style
|
|
1868
1996
|
- New features include tests
|
|
1869
1997
|
- Documentation is updated
|
|
1870
1998
|
|
|
1999
|
+
[![Contributors]][contributors_url] [![Sponsor shinrai]][sponsor_url]
|
|
2000
|
+
|
|
1871
2001
|
---
|
|
1872
2002
|
|
|
1873
|
-
|
|
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
|