@cldmv/holdmytask 2.0.4 → 2.0.6
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 -182
- package/dist/hold-my-task.mjs +150 -31
- package/package.json +3 -3
package/dist/hold-my-task.mjs
CHANGED
|
@@ -35,7 +35,7 @@ export class HoldMyTask extends EventEmitter {
|
|
|
35
35
|
* @param {boolean} [options.smartScheduling=true] - Use dynamic timeouts instead of constant polling for better performance
|
|
36
36
|
* @param {number} [options.tick=25] - Polling interval in milliseconds when smartScheduling is disabled
|
|
37
37
|
* @param {number} [options.healingInterval=5000] - Self-healing check interval in milliseconds (smart scheduling only)
|
|
38
|
-
* @param {Object} [options.priorities={}] - Priority-specific default configurations
|
|
38
|
+
* @param {Object} [options.priorities={}] - Priority-specific default configurations, keyed by integer priority. A non-integer key emits an "invalid-priority" warning event
|
|
39
39
|
* @param {number} [options.priorities[priority].concurrency] - Maximum concurrent tasks for this priority (defaults to global concurrency limit)
|
|
40
40
|
* @param {number} [options.priorities[priority].postDelay] - Delay after task completion before next task of same priority
|
|
41
41
|
* @param {number} [options.priorities[priority].startDelay] - Delay before task execution (pre-execution delay)
|
|
@@ -140,6 +140,61 @@ export class HoldMyTask extends EventEmitter {
|
|
|
140
140
|
return transformed;
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
+
/**
|
|
144
|
+
* Normalizes task-level delay option names passed to enqueue().
|
|
145
|
+
* `postDelay` / `startDelay` are the current names; `delay` / `start` are deprecated aliases.
|
|
146
|
+
* When both forms are given, the current name wins. Each deprecated alias emits one
|
|
147
|
+
* deprecation `warning` event per queue instance (not one per task), so a hot enqueue
|
|
148
|
+
* path doesn't flood listeners.
|
|
149
|
+
* @param {Object} options - Task options (never mutated)
|
|
150
|
+
* @returns {Object} A copy of the options using the current names
|
|
151
|
+
* @private
|
|
152
|
+
* @internal
|
|
153
|
+
*/
|
|
154
|
+
_normalizeTaskDelayOptions(options) {
|
|
155
|
+
if (!("delay" in options) && !("start" in options)) {
|
|
156
|
+
return options;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const normalized = { ...options };
|
|
160
|
+
const aliases = [
|
|
161
|
+
{ deprecated: "start", replacement: "startDelay" },
|
|
162
|
+
{ deprecated: "delay", replacement: "postDelay" }
|
|
163
|
+
];
|
|
164
|
+
|
|
165
|
+
for (const { deprecated, replacement } of aliases) {
|
|
166
|
+
if (!(deprecated in options)) continue;
|
|
167
|
+
if (!(replacement in options)) {
|
|
168
|
+
normalized[replacement] = options[deprecated];
|
|
169
|
+
this._warnTaskOptionDeprecated(deprecated, replacement);
|
|
170
|
+
}
|
|
171
|
+
delete normalized[deprecated];
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return normalized;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Emits a deprecation warning for a task-level option, once per option name per instance.
|
|
179
|
+
* @param {string} deprecated - Deprecated option name
|
|
180
|
+
* @param {string} replacement - Current option name
|
|
181
|
+
* @returns {void}
|
|
182
|
+
* @private
|
|
183
|
+
* @internal
|
|
184
|
+
*/
|
|
185
|
+
_warnTaskOptionDeprecated(deprecated, replacement) {
|
|
186
|
+
if (this._warnedTaskOptions.has(deprecated)) return;
|
|
187
|
+
this._warnedTaskOptions.add(deprecated);
|
|
188
|
+
setImmediate(() =>
|
|
189
|
+
this.emit("warning", {
|
|
190
|
+
type: "deprecation",
|
|
191
|
+
message: `Task option '${deprecated}' is deprecated. Use '${replacement}' instead.`,
|
|
192
|
+
deprecated,
|
|
193
|
+
replacement
|
|
194
|
+
})
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
|
|
143
198
|
constructor(options = {}) {
|
|
144
199
|
super();
|
|
145
200
|
|
|
@@ -155,6 +210,41 @@ export class HoldMyTask extends EventEmitter {
|
|
|
155
210
|
}
|
|
156
211
|
}
|
|
157
212
|
|
|
213
|
+
/**
|
|
214
|
+
* Resolves a `priorities` / `delays` object key to a numeric priority.
|
|
215
|
+
* Integer keys ("0", "10", "-1") resolve silently. Any other key emits a `warning` event of
|
|
216
|
+
* type "invalid-priority" so a typo can't silently disable a priority config: a key
|
|
217
|
+
* parseInt can't read (e.g. "high") is ignored, and a key it truncates (e.g. "2.5") keeps
|
|
218
|
+
* its historical meaning - the truncated priority - but is reported.
|
|
219
|
+
* @param {string} key - The object key as written in the config
|
|
220
|
+
* @param {string} option - The option the key came from ("priorities" or "delays")
|
|
221
|
+
* @returns {number|null} The numeric priority, or null when the key is ignored
|
|
222
|
+
* @private
|
|
223
|
+
* @internal
|
|
224
|
+
*/
|
|
225
|
+
_resolvePriorityKey(key, option) {
|
|
226
|
+
if (/^-?\d+$/.test(key)) {
|
|
227
|
+
return Number(key);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const parsed = parseInt(key);
|
|
231
|
+
const priority = isNaN(parsed) ? null : parsed;
|
|
232
|
+
const message =
|
|
233
|
+
priority === null
|
|
234
|
+
? `Priority key '${key}' in '${option}' is not an integer and was ignored. Priority keys must be integers.`
|
|
235
|
+
: `Priority key '${key}' in '${option}' is not an integer; it was applied to priority ${priority}. Priority keys must be integers.`;
|
|
236
|
+
setImmediate(() =>
|
|
237
|
+
this.emit("warning", {
|
|
238
|
+
type: "invalid-priority",
|
|
239
|
+
message,
|
|
240
|
+
option,
|
|
241
|
+
key,
|
|
242
|
+
priority
|
|
243
|
+
})
|
|
244
|
+
);
|
|
245
|
+
return priority;
|
|
246
|
+
}
|
|
247
|
+
|
|
158
248
|
/**
|
|
159
249
|
* Synchronous initialization for backwards compatibility
|
|
160
250
|
* @private
|
|
@@ -249,8 +339,9 @@ export class HoldMyTask extends EventEmitter {
|
|
|
249
339
|
// First, transform any existing priority configurations to use new property names
|
|
250
340
|
if (cleanOptions.priorities && typeof cleanOptions.priorities === "object") {
|
|
251
341
|
for (const [priority, config] of Object.entries(cleanOptions.priorities)) {
|
|
252
|
-
|
|
253
|
-
|
|
342
|
+
if (config == null) continue;
|
|
343
|
+
const priorityNum = this._resolvePriorityKey(priority, "priorities");
|
|
344
|
+
if (priorityNum !== null) {
|
|
254
345
|
transformedPriorities[priorityNum] = HoldMyTask._transformDelayProperties(config, this);
|
|
255
346
|
}
|
|
256
347
|
}
|
|
@@ -270,8 +361,9 @@ export class HoldMyTask extends EventEmitter {
|
|
|
270
361
|
);
|
|
271
362
|
|
|
272
363
|
for (const [priority, delay] of Object.entries(cleanOptions.delays)) {
|
|
273
|
-
|
|
274
|
-
|
|
364
|
+
if (delay == null) continue;
|
|
365
|
+
const priorityNum = this._resolvePriorityKey(priority, "delays");
|
|
366
|
+
if (priorityNum !== null) {
|
|
275
367
|
transformedPriorities[priorityNum] = {
|
|
276
368
|
postDelay: delay, // Use new property name
|
|
277
369
|
startDelay: 0, // Use new property name
|
|
@@ -350,6 +442,9 @@ export class HoldMyTask extends EventEmitter {
|
|
|
350
442
|
this.coalescingRepresentatives = new Map(); // representativeTaskId -> { coalescingKey, groupId }
|
|
351
443
|
this.nextGroupId = 1;
|
|
352
444
|
|
|
445
|
+
// Deprecated task-level option names already warned about (one warning per name per instance)
|
|
446
|
+
this._warnedTaskOptions = new Set();
|
|
447
|
+
|
|
353
448
|
if (this.options.autoStart) {
|
|
354
449
|
this.resume();
|
|
355
450
|
}
|
|
@@ -378,11 +473,13 @@ export class HoldMyTask extends EventEmitter {
|
|
|
378
473
|
* @param {string|number} [options.id] - Custom task ID for identification and later reference (must be unique)
|
|
379
474
|
* @param {number} [options.priority] - Task priority (higher numbers run first)
|
|
380
475
|
* @param {number} [options.timestamp] - When the task should be ready to run (milliseconds since epoch)
|
|
381
|
-
* @param {number} [options.
|
|
476
|
+
* @param {number} [options.startDelay] - Milliseconds from now when the task should be ready to run (convenience for timestamp calculation). Overrides the priority's startDelay
|
|
477
|
+
* @param {number} [options.start] - DEPRECATED: Use startDelay instead
|
|
382
478
|
* @param {AbortSignal} [options.signal] - AbortSignal to cancel the task
|
|
383
479
|
* @param {number} [options.timeout] - Task timeout in milliseconds (for execution time limit)
|
|
384
480
|
* @param {number} [options.expire] - Task expiration timestamp or milliseconds from now (for queue waiting time limit)
|
|
385
|
-
* @param {number} [options.
|
|
481
|
+
* @param {number} [options.postDelay] - Delay after this task completes before the next task can start. Overrides the priority's postDelay; use -1 to bypass the current delay period
|
|
482
|
+
* @param {number} [options.delay] - DEPRECATED: Use postDelay instead
|
|
386
483
|
* @param {boolean} [options.bypassDelay] - If true, skip any active delay period and start immediately
|
|
387
484
|
* @param {string} [options.coalescingKey] - Key for task coalescing - tasks with same key will be coalesced within windows
|
|
388
485
|
* @param {number} [options.coalescingWindowDuration] - Override coalescing window duration (task-level override of key-level and defaults)
|
|
@@ -408,8 +505,8 @@ export class HoldMyTask extends EventEmitter {
|
|
|
408
505
|
* // Bypass current delay for urgent task
|
|
409
506
|
* const urgent = queue.enqueue(urgentTask, { priority: 10, bypassDelay: true });
|
|
410
507
|
*
|
|
411
|
-
* // Alternative: use
|
|
412
|
-
* const urgent2 = queue.enqueue(urgentTask, { priority: 10,
|
|
508
|
+
* // Alternative: use postDelay: -1 to bypass
|
|
509
|
+
* const urgent2 = queue.enqueue(urgentTask, { priority: 10, postDelay: -1 });
|
|
413
510
|
*
|
|
414
511
|
* // Coalescing tasks - multiple device status checks become one
|
|
415
512
|
* queue.enqueue(checkDeviceStatus, callback1, { coalescingKey: "device-123", coalescingWindowDuration: 1000 });
|
|
@@ -438,6 +535,9 @@ export class HoldMyTask extends EventEmitter {
|
|
|
438
535
|
finalOptions = { ...optionsOrCallback, ...options };
|
|
439
536
|
}
|
|
440
537
|
|
|
538
|
+
// Task-level delay names: postDelay/startDelay are current, delay/start are deprecated aliases
|
|
539
|
+
finalOptions = this._normalizeTaskDelayOptions(finalOptions);
|
|
540
|
+
|
|
441
541
|
// Generate ID - use custom ID if provided, otherwise auto-generate
|
|
442
542
|
const id = finalOptions.id ? String(finalOptions.id) : String(this.nextId++);
|
|
443
543
|
|
|
@@ -457,7 +557,7 @@ export class HoldMyTask extends EventEmitter {
|
|
|
457
557
|
// Regular task handling - apply priority defaults for start delay
|
|
458
558
|
const priority = finalOptions.priority ?? this.options.defaultPriority;
|
|
459
559
|
const priorityConfig = this.getPriorityConfig(priority, finalOptions);
|
|
460
|
-
const effectiveStart =
|
|
560
|
+
const effectiveStart = priorityConfig.startDelay ?? 0;
|
|
461
561
|
const readyAt = finalOptions.timestamp ?? (effectiveStart ? now + effectiveStart : now);
|
|
462
562
|
|
|
463
563
|
// Calculate expiration timestamp
|
|
@@ -484,8 +584,8 @@ export class HoldMyTask extends EventEmitter {
|
|
|
484
584
|
status: "pending",
|
|
485
585
|
signal: finalOptions.signal,
|
|
486
586
|
timeout: finalOptions.timeout,
|
|
487
|
-
delay: finalOptions.
|
|
488
|
-
bypassDelay: finalOptions.bypassDelay || finalOptions.
|
|
587
|
+
delay: finalOptions.postDelay, // task-level completion delay override (undefined = use the priority's postDelay)
|
|
588
|
+
bypassDelay: finalOptions.bypassDelay || finalOptions.postDelay === -1, // bypass current delay period
|
|
489
589
|
metadata: finalOptions.metadata
|
|
490
590
|
};
|
|
491
591
|
|
|
@@ -620,14 +720,14 @@ export class HoldMyTask extends EventEmitter {
|
|
|
620
720
|
const priorityConfig = this.getPriorityConfig(priority, options);
|
|
621
721
|
|
|
622
722
|
// Apply configuration priority: task options > coalescing key config > priority defaults > coalescing defaults
|
|
623
|
-
const effectiveStart = options.
|
|
723
|
+
const effectiveStart = options.startDelay ?? coalescingConfig.startDelay ?? priorityConfig.startDelay ?? 0;
|
|
624
724
|
|
|
625
725
|
const readyAt = options.timestamp ?? (effectiveStart ? now + effectiveStart : now);
|
|
626
726
|
const windowEnd = now + coalescingConfig.windowDuration;
|
|
627
727
|
const mustRunBy = now + coalescingConfig.maxDelay;
|
|
628
728
|
|
|
629
729
|
// Apply effective delay configuration
|
|
630
|
-
const effectiveDelay = options.
|
|
730
|
+
const effectiveDelay = options.postDelay ?? coalescingConfig.postDelay ?? priorityConfig.postDelay;
|
|
631
731
|
|
|
632
732
|
// Create task item (not added to main queue directly)
|
|
633
733
|
const taskItem = {
|
|
@@ -640,7 +740,7 @@ export class HoldMyTask extends EventEmitter {
|
|
|
640
740
|
signal: options.signal,
|
|
641
741
|
timeout: options.timeout,
|
|
642
742
|
delay: effectiveDelay,
|
|
643
|
-
bypassDelay: options.bypassDelay || options.
|
|
743
|
+
bypassDelay: options.bypassDelay || options.postDelay === -1,
|
|
644
744
|
metadata: options.metadata,
|
|
645
745
|
coalescingKey,
|
|
646
746
|
enqueueSeq: this.enqueueSeq++
|
|
@@ -1671,8 +1771,9 @@ export class HoldMyTask extends EventEmitter {
|
|
|
1671
1771
|
// Start tasks up to concurrency limits (both global and per-priority)
|
|
1672
1772
|
let hasWaitingTasks = false;
|
|
1673
1773
|
const currentTime = this.now();
|
|
1674
|
-
|
|
1675
|
-
|
|
1774
|
+
// nextAvailableTime is only set (non-zero) when the completed task's effective delay - its own
|
|
1775
|
+
// postDelay override or its priority's postDelay - was positive, so it alone decides the gate.
|
|
1776
|
+
const delayActive = currentTime < this.nextAvailableTime;
|
|
1676
1777
|
|
|
1677
1778
|
while (this.readyHeap.size() > 0) {
|
|
1678
1779
|
const task = this.readyHeap.peek();
|
|
@@ -1835,20 +1936,37 @@ export class HoldMyTask extends EventEmitter {
|
|
|
1835
1936
|
error.taskId = item.id;
|
|
1836
1937
|
|
|
1837
1938
|
if (item.callback) {
|
|
1838
|
-
// Callback API -
|
|
1939
|
+
// Callback API - deliver the failure to the callback
|
|
1839
1940
|
try {
|
|
1840
1941
|
item.callback(error, null);
|
|
1841
1942
|
} catch (err) {
|
|
1842
1943
|
this.emit("error", { error: err, task: item });
|
|
1843
1944
|
}
|
|
1844
|
-
//
|
|
1845
|
-
|
|
1945
|
+
// Also report it as a queue "error" event, but only to listeners: the callback already
|
|
1946
|
+
// has the failure, so an unhandled-"error" throw would only crash the caller's process.
|
|
1947
|
+
this._emitTaskError({ error, task: item });
|
|
1846
1948
|
} else if (item.reject) {
|
|
1847
1949
|
// Promise API - just reject, don't emit error event (handled by promise)
|
|
1848
1950
|
item.reject(error);
|
|
1849
1951
|
}
|
|
1850
1952
|
}
|
|
1851
1953
|
|
|
1954
|
+
/**
|
|
1955
|
+
* Emits a task failure as an `error` event when, and only when, a listener is attached.
|
|
1956
|
+
* Used for callback-API task failures, which are always delivered to the task's callback
|
|
1957
|
+
* as well; with no listener, EventEmitter would otherwise throw the event and crash the
|
|
1958
|
+
* process before (or after) the callback could handle it.
|
|
1959
|
+
* @param {Object} payload - Event payload
|
|
1960
|
+
* @returns {void}
|
|
1961
|
+
* @private
|
|
1962
|
+
* @internal
|
|
1963
|
+
*/
|
|
1964
|
+
_emitTaskError(payload) {
|
|
1965
|
+
if (this.listenerCount("error") > 0) {
|
|
1966
|
+
this.emit("error", payload);
|
|
1967
|
+
}
|
|
1968
|
+
}
|
|
1969
|
+
|
|
1852
1970
|
/**
|
|
1853
1971
|
* Starts execution of a ready task.
|
|
1854
1972
|
* @param {Object} item - The task item to execute
|
|
@@ -1934,9 +2052,10 @@ export class HoldMyTask extends EventEmitter {
|
|
|
1934
2052
|
: "error";
|
|
1935
2053
|
item.finishedAt = this.now();
|
|
1936
2054
|
|
|
1937
|
-
// Emit error event only for callback API (promise API conveys error via rejection)
|
|
2055
|
+
// Emit error event only for callback API (promise API conveys error via rejection),
|
|
2056
|
+
// and only to listeners - the callback below always receives the failure.
|
|
1938
2057
|
if (item.callback) {
|
|
1939
|
-
this.
|
|
2058
|
+
this._emitTaskError(item);
|
|
1940
2059
|
}
|
|
1941
2060
|
|
|
1942
2061
|
// Store final values on promise handle before rejecting (for promise API)
|
|
@@ -2050,8 +2169,7 @@ export class HoldMyTask extends EventEmitter {
|
|
|
2050
2169
|
|
|
2051
2170
|
// Check if we have ready tasks that can run immediately
|
|
2052
2171
|
if (this.readyHeap.size() > 0 && this.running.size < this.options.concurrency) {
|
|
2053
|
-
const
|
|
2054
|
-
const delayActive = delay > 0 && now < this.nextAvailableTime;
|
|
2172
|
+
const delayActive = now < this.nextAvailableTime;
|
|
2055
2173
|
|
|
2056
2174
|
if (!delayActive) {
|
|
2057
2175
|
shouldRunNow = true;
|
|
@@ -2246,7 +2364,7 @@ export class HoldMyTask extends EventEmitter {
|
|
|
2246
2364
|
* // Check with task-level overrides
|
|
2247
2365
|
* const effectiveConfig = queue.getCoalescingConfig('ui.update', {
|
|
2248
2366
|
* coalescingWindowDuration: 50,
|
|
2249
|
-
*
|
|
2367
|
+
* postDelay: 30 // Deprecated task-level name delay is still accepted
|
|
2250
2368
|
* });
|
|
2251
2369
|
*/
|
|
2252
2370
|
getCoalescingConfig(coalescingKey, taskOptions = {}) {
|
|
@@ -2254,8 +2372,9 @@ export class HoldMyTask extends EventEmitter {
|
|
|
2254
2372
|
const defaults = this.options.coalescing.defaults;
|
|
2255
2373
|
const priorityConfig = this.getPriorityConfig(taskOptions.priority ?? this.options.defaultPriority);
|
|
2256
2374
|
|
|
2257
|
-
const postDelay = taskOptions.delay ?? keyConfig.postDelay ?? priorityConfig.postDelay ?? defaults.postDelay;
|
|
2258
|
-
const startDelay =
|
|
2375
|
+
const postDelay = taskOptions.postDelay ?? taskOptions.delay ?? keyConfig.postDelay ?? priorityConfig.postDelay ?? defaults.postDelay;
|
|
2376
|
+
const startDelay =
|
|
2377
|
+
taskOptions.startDelay ?? taskOptions.start ?? keyConfig.startDelay ?? priorityConfig.startDelay ?? defaults.startDelay;
|
|
2259
2378
|
|
|
2260
2379
|
return {
|
|
2261
2380
|
windowDuration: taskOptions.coalescingWindowDuration ?? keyConfig.windowDuration ?? defaults.windowDuration,
|
|
@@ -2356,15 +2475,15 @@ export class HoldMyTask extends EventEmitter {
|
|
|
2356
2475
|
*
|
|
2357
2476
|
* // Check with task-level overrides
|
|
2358
2477
|
* const effectiveConfig = queue.getPriorityConfig(1, {
|
|
2359
|
-
*
|
|
2360
|
-
*
|
|
2478
|
+
* postDelay: 50,
|
|
2479
|
+
* startDelay: 10 // Deprecated task-level names delay/start are still accepted
|
|
2361
2480
|
* });
|
|
2362
2481
|
*/
|
|
2363
2482
|
getPriorityConfig(priority, taskOptions = {}) {
|
|
2364
2483
|
const priorityConfig = this.options.priorities[priority] || {};
|
|
2365
2484
|
|
|
2366
|
-
const postDelay = taskOptions.delay ?? priorityConfig.postDelay;
|
|
2367
|
-
const startDelay = taskOptions.start ?? priorityConfig.startDelay;
|
|
2485
|
+
const postDelay = taskOptions.postDelay ?? taskOptions.delay ?? priorityConfig.postDelay;
|
|
2486
|
+
const startDelay = taskOptions.startDelay ?? taskOptions.start ?? priorityConfig.startDelay;
|
|
2368
2487
|
|
|
2369
2488
|
return {
|
|
2370
2489
|
// New clear property names
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cldmv/holdmytask",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.6",
|
|
4
4
|
"description": "A tiny task queue that waits until your task is ready",
|
|
5
5
|
"main": "./index.cjs",
|
|
6
6
|
"module": "./index.mjs",
|
|
@@ -82,8 +82,8 @@
|
|
|
82
82
|
},
|
|
83
83
|
"type": "module",
|
|
84
84
|
"devDependencies": {
|
|
85
|
-
"@cldmv/configs": "^1.2.
|
|
86
|
-
"@cldmv/fix-headers": "^2.
|
|
85
|
+
"@cldmv/configs": "^1.2.4",
|
|
86
|
+
"@cldmv/fix-headers": "^2.2.0",
|
|
87
87
|
"@cldmv/vitest-runner": "^1.2.0",
|
|
88
88
|
"@eslint/css": "^2.0.0",
|
|
89
89
|
"@eslint/js": "^10.0.1",
|