@cldmv/holdmytask 1.6.1 → 1.6.2
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/dist/hold-my-task.mjs +2901 -0
- package/dist/utils.mjs +120 -0
- package/package.json +4 -8
- package/types/dist/hold-my-task.d.mts +15 -4
- package/types/dist/hold-my-task.d.mts.map +1 -1
- package/types/index.d.mts +25 -2
- package/types/index.d.mts.map +1 -1
package/dist/utils.mjs
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @Project: @cldmv/holdmytask
|
|
3
|
+
* @Filename: /src/utils.mjs
|
|
4
|
+
* @Date: 2025-11-08 17:43:09 -08:00 (1762652589)
|
|
5
|
+
* @Author: Nate Hyson <CLDMV>
|
|
6
|
+
* @Email: <Shinrai@users.noreply.github.com>
|
|
7
|
+
* -----
|
|
8
|
+
* @Last modified by: Nate Hyson <CLDMV> (Shinrai@users.noreply.github.com)
|
|
9
|
+
* @Last modified time: 2025-11-09 16:22:05 -08:00 (1762734125)
|
|
10
|
+
* -----
|
|
11
|
+
* @Copyright: Copyright (c) 2013-2025 Catalyzed Motivation Inc. All rights reserved.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A min-heap implementation with custom comparison function.
|
|
16
|
+
* Maintains the heap property where parent nodes are smaller than their children.
|
|
17
|
+
*/
|
|
18
|
+
export class MinHeap {
|
|
19
|
+
/**
|
|
20
|
+
* Creates a new MinHeap with a custom comparison function.
|
|
21
|
+
* @param {Function} compare - Comparison function that returns negative if a < b, positive if a > b, 0 if equal
|
|
22
|
+
* @example
|
|
23
|
+
* // Priority queue (higher priority = smaller value)
|
|
24
|
+
* const heap = new MinHeap((a, b) => a.priority - b.priority);
|
|
25
|
+
*/
|
|
26
|
+
constructor(compare) {
|
|
27
|
+
this.heap = [];
|
|
28
|
+
this.compare = compare;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Adds an item to the heap, maintaining heap property.
|
|
33
|
+
* @param {*} item - The item to add to the heap
|
|
34
|
+
* @returns {void}
|
|
35
|
+
* @example
|
|
36
|
+
* heap.push({ value: 5, priority: 1 });
|
|
37
|
+
*/
|
|
38
|
+
push(item) {
|
|
39
|
+
this.heap.push(item);
|
|
40
|
+
this.bubbleUp(this.heap.length - 1);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Removes and returns the minimum item from the heap.
|
|
45
|
+
* @returns {*|undefined} The minimum item, or undefined if heap is empty
|
|
46
|
+
* @example
|
|
47
|
+
* const min = heap.pop(); // Returns item with smallest comparison value
|
|
48
|
+
*/
|
|
49
|
+
pop() {
|
|
50
|
+
if (this.heap.length === 0) return undefined;
|
|
51
|
+
const root = this.heap[0];
|
|
52
|
+
const last = this.heap.pop();
|
|
53
|
+
if (this.heap.length > 0) {
|
|
54
|
+
this.heap[0] = last;
|
|
55
|
+
this.sinkDown(0);
|
|
56
|
+
}
|
|
57
|
+
return root;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Returns the minimum item without removing it from the heap.
|
|
62
|
+
* @returns {*|undefined} The minimum item, or undefined if heap is empty
|
|
63
|
+
* @example
|
|
64
|
+
* const min = heap.peek(); // Look at minimum without removing
|
|
65
|
+
*/
|
|
66
|
+
peek() {
|
|
67
|
+
return this.heap[0];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Returns the number of items in the heap.
|
|
72
|
+
* @returns {number} The size of the heap
|
|
73
|
+
* @example
|
|
74
|
+
* const count = heap.size(); // 5
|
|
75
|
+
*/
|
|
76
|
+
size() {
|
|
77
|
+
return this.heap.length;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Moves an item up the heap to maintain heap property after insertion.
|
|
82
|
+
* @param {number} index - Index of the item to bubble up
|
|
83
|
+
* @returns {void}
|
|
84
|
+
* @private
|
|
85
|
+
*/
|
|
86
|
+
bubbleUp(index) {
|
|
87
|
+
while (index > 0) {
|
|
88
|
+
const parentIndex = Math.floor((index - 1) / 2);
|
|
89
|
+
if (this.compare(this.heap[index], this.heap[parentIndex]) >= 0) break;
|
|
90
|
+
[this.heap[index], this.heap[parentIndex]] = [this.heap[parentIndex], this.heap[index]];
|
|
91
|
+
index = parentIndex;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Moves an item down the heap to maintain heap property after removal.
|
|
97
|
+
* @param {number} index - Index of the item to sink down
|
|
98
|
+
* @returns {void}
|
|
99
|
+
* @private
|
|
100
|
+
*/
|
|
101
|
+
sinkDown(index) {
|
|
102
|
+
const length = this.heap.length;
|
|
103
|
+
while (true) {
|
|
104
|
+
let left = 2 * index + 1;
|
|
105
|
+
let right = 2 * index + 2;
|
|
106
|
+
let smallest = index;
|
|
107
|
+
|
|
108
|
+
if (left < length && this.compare(this.heap[left], this.heap[smallest]) < 0) {
|
|
109
|
+
smallest = left;
|
|
110
|
+
}
|
|
111
|
+
if (right < length && this.compare(this.heap[right], this.heap[smallest]) < 0) {
|
|
112
|
+
smallest = right;
|
|
113
|
+
}
|
|
114
|
+
if (smallest === index) break;
|
|
115
|
+
|
|
116
|
+
[this.heap[index], this.heap[smallest]] = [this.heap[smallest], this.heap[index]];
|
|
117
|
+
index = smallest;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cldmv/holdmytask",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.2",
|
|
4
4
|
"description": "A tiny task queue that waits until your task is ready",
|
|
5
5
|
"main": "./index.cjs",
|
|
6
6
|
"module": "./index.mjs",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"lint": "eslint --config .configs/eslint.config.mjs .",
|
|
36
36
|
"build": "node build.mjs",
|
|
37
37
|
"build:types": "tsc --project .configs/tsconfig.dts.jsonc",
|
|
38
|
-
"build:ci": "npm run build
|
|
38
|
+
"build:ci": "npm run build && npm run build:types && npm run test:types",
|
|
39
39
|
"precommit": "npm run build:types && npm run test:types && npm run lint && npm run test"
|
|
40
40
|
},
|
|
41
41
|
"keywords": [
|
|
@@ -78,15 +78,11 @@
|
|
|
78
78
|
},
|
|
79
79
|
"type": "module",
|
|
80
80
|
"devDependencies": {
|
|
81
|
-
"@eslint/
|
|
82
|
-
"@eslint/js": "^9.39.1",
|
|
83
|
-
"@eslint/json": "^0.14.0",
|
|
81
|
+
"@eslint/js": "^10.0.1",
|
|
84
82
|
"@eslint/markdown": "^7.5.1",
|
|
85
|
-
"@html-eslint/eslint-plugin": "^0.48.0",
|
|
86
|
-
"@html-eslint/parser": "^0.48.0",
|
|
87
83
|
"@types/node": "^20.0.0",
|
|
88
84
|
"@vitest/ui": "^4.0.8",
|
|
89
|
-
"eslint": "^
|
|
85
|
+
"eslint": "^10.0.0",
|
|
90
86
|
"globals": "^16.5.0",
|
|
91
87
|
"prettier": "^3.0.0",
|
|
92
88
|
"typescript": "^5.0.0",
|
|
@@ -18,6 +18,9 @@ export class HoldMyTask extends EventEmitter<[never]> {
|
|
|
18
18
|
* @param {number} [options.tick=25] - Polling interval in milliseconds when smartScheduling is disabled
|
|
19
19
|
* @param {number} [options.healingInterval=5000] - Self-healing check interval in milliseconds (smart scheduling only)
|
|
20
20
|
* @param {Object} [options.priorities={}] - Priority-specific default configurations
|
|
21
|
+
* @param {number} [options.priorities[priority].concurrency] - Maximum concurrent tasks for this priority (defaults to global concurrency limit)
|
|
22
|
+
* @param {number} [options.priorities[priority].postDelay] - Delay after task completion before next task of same priority
|
|
23
|
+
* @param {number} [options.priorities[priority].startDelay] - Delay before task execution (pre-execution delay)
|
|
21
24
|
* @param {Object} [options.coalescing] - Enhanced coalescing configuration (preferred over flat options)
|
|
22
25
|
* @param {Object} [options.coalescing.defaults] - Default coalescing settings for all keys
|
|
23
26
|
* @param {number} [options.coalescing.defaults.windowDuration=200] - Default window duration in milliseconds
|
|
@@ -36,12 +39,12 @@ export class HoldMyTask extends EventEmitter<[never]> {
|
|
|
36
39
|
* @example
|
|
37
40
|
* // Enhanced configuration with priority defaults and extended coalescing
|
|
38
41
|
* const queue = new HoldMyTask({
|
|
39
|
-
* concurrency:
|
|
42
|
+
* concurrency: 8, // Global maximum: 8 total tasks across all priorities
|
|
40
43
|
* delays: { 0: 1000, 1: 500 },
|
|
41
44
|
* priorities: {
|
|
42
|
-
* 1: { postDelay: 100, startDelay: 0 }, //
|
|
43
|
-
* 2: { postDelay: 200, startDelay: 50 }, //
|
|
44
|
-
* 3: { postDelay: 0, startDelay: 100 } //
|
|
45
|
+
* 1: { concurrency: 1, postDelay: 100, startDelay: 0 }, // Critical: Only 1 at a time, 100ms post-delay
|
|
46
|
+
* 2: { concurrency: 3, postDelay: 200, startDelay: 50 }, // Important: Up to 3 at a time, 200ms post-delay
|
|
47
|
+
* 3: { concurrency: 5, postDelay: 0, startDelay: 100 } // Background: Up to 5 at a time, 100ms pre-delay
|
|
45
48
|
* },
|
|
46
49
|
* coalescing: {
|
|
47
50
|
* defaults: {
|
|
@@ -113,6 +116,7 @@ export class HoldMyTask extends EventEmitter<[never]> {
|
|
|
113
116
|
pendingHeap: MinHeap;
|
|
114
117
|
readyHeap: MinHeap;
|
|
115
118
|
running: Set<any>;
|
|
119
|
+
runningByPriority: Map<any, any>;
|
|
116
120
|
tasks: Map<any, any>;
|
|
117
121
|
nextId: number;
|
|
118
122
|
enqueueSeq: number;
|
|
@@ -448,6 +452,13 @@ export class HoldMyTask extends EventEmitter<[never]> {
|
|
|
448
452
|
* @private
|
|
449
453
|
*/
|
|
450
454
|
private schedulerTick;
|
|
455
|
+
/**
|
|
456
|
+
* Checks if a task can start based on both global and per-priority concurrency limits.
|
|
457
|
+
* @param {Object} task - The task to check
|
|
458
|
+
* @returns {boolean} True if the task can start, false if concurrency limits prevent it
|
|
459
|
+
* @private
|
|
460
|
+
*/
|
|
461
|
+
private _canStartTask;
|
|
451
462
|
/**
|
|
452
463
|
* Handles an expired task by removing it and calling appropriate error handling.
|
|
453
464
|
* @param {Object} item - The expired task item
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hold-my-task.d.mts","sourceRoot":"","sources":["../../dist/hold-my-task.mjs"],"names":[],"mappings":"AAgBA;;;;;GAKG;AACH;IACC
|
|
1
|
+
{"version":3,"file":"hold-my-task.d.mts","sourceRoot":"","sources":["../../dist/hold-my-task.mjs"],"names":[],"mappings":"AAgBA;;;;;GAKG;AACH;IACC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAgEG;IAEH;;;;;;;;OAQG;IACH,yCAwCC;IAyND;;;;;;;;;OASG;IACH,uBAGC;IApOD,0BAaC;IAED;;;;OAIG;IACH,wBAGC;IAFA,mBAAqB;IAItB;;;;;OAKG;IACH,yBAUC;IAED;;;;OAIG;IACH,0BAqKC;IA3DA,aAuBC;IAGD,qBAA+D;IAC/D,mBAIE;IAEF,kBAAwB;IACxB,iCAAkC;IAClC,qBAAsB;IACtB,eAAe;IACf,mBAAmB;IACnB,kBAAqB;IACrB,mBAAsB;IACtB,2BAAiC;IACjC,uBAA0B;IAG1B,iCAA4B;IAC5B,gCAA2B;IAC3B,yBAAyB;IAGzB,2BAAsB;IAGtB,gCAAiC;IACjC,yCAA0C;IAC1C,oBAAoB;IAsBrB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4CG;IACH,4CA1CW,cAAe,YAEvB;QAAgC,EAAE,GAA1B,MAAM,GAAC,MAAM;QACI,QAAQ,GAAzB,MAAM;QACW,SAAS,GAA1B,MAAM;QACW,KAAK,GAAtB,MAAM;QACgB,MAAM,GAA5B,WAAW;QACM,OAAO,GAAxB,MAAM;QACW,MAAM,GAAvB,MAAM;Q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AM,GACX,MAAO,IAAI,CAIvB;IAED;;;;OAIG;IACH,QAHW,MAAM,GAAC,MAAM,GACX,OAAO,CAInB;IAED;;;;OAIG;IACH,YAHW,MAAM,GAAC,MAAM,GACX,MAAO,IAAI,CAIvB;IAED;;;;OAIG;IACH,YAHW,MAAM,GAAC,MAAM,GACX,OAAO,CAInB;IAMD;;;OAGG;IACH,eA+HC;IAED;;;OAGG;IACH,qBAmBC;IAED;;;OAGG;IACH,oBA+FC;IAED;;;OAGG;IACH,wBAyGC;IAED;;;OAGG;IACH,oBAFW,OAAO,QAuEjB;CACD;6BAv0F4B,QAAQ;wBACb,aAAa"}
|
package/types/index.d.mts
CHANGED
|
@@ -1,3 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Creates a HoldMyTask instance for task queue management
|
|
3
|
+
* @param {object} [options={}] - Configuration options
|
|
4
|
+
* @returns {Promise<object>} HoldMyTask instance
|
|
5
|
+
*/
|
|
6
|
+
export default function createHoldMyTask(options?: object): Promise<object>;
|
|
7
|
+
/**
|
|
8
|
+
* Create a task queue instance
|
|
9
|
+
* @param {object} [options={}] - Configuration options
|
|
10
|
+
* @returns {Promise<object>} HoldMyTask instance
|
|
11
|
+
*/
|
|
12
|
+
export function createQueue(options?: object): Promise<object>;
|
|
13
|
+
/**
|
|
14
|
+
* Create a task manager instance
|
|
15
|
+
* @param {object} [options={}] - Configuration options
|
|
16
|
+
* @returns {Promise<object>} HoldMyTask instance
|
|
17
|
+
*/
|
|
18
|
+
export function createTaskManager(options?: object): Promise<object>;
|
|
19
|
+
/**
|
|
20
|
+
* Create a task processor instance
|
|
21
|
+
* @param {object} [options={}] - Configuration options
|
|
22
|
+
* @returns {Promise<object>} HoldMyTask instance
|
|
23
|
+
*/
|
|
24
|
+
export function createTaskProcessor(options?: object): Promise<object>;
|
|
25
|
+
export { createHoldMyTask as HoldMyTask, createQueue as queue, createQueue as Queue, createTaskManager as TaskManager, createQueue as TaskQueue, createQueue as QueueManager, createTaskProcessor as TaskProcessor };
|
|
3
26
|
//# sourceMappingURL=index.d.mts.map
|
package/types/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../index.mjs"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../index.mjs"],"names":[],"mappings":"AAsBA;;;;GAIG;AACH,mDAHW,MAAM,GACJ,OAAO,CAAC,MAAM,CAAC,CAO3B;AAED;;;;GAIG;AACH,sCAHW,MAAM,GACJ,OAAO,CAAC,MAAM,CAAC,CAM3B;AAED;;;;GAIG;AACH,4CAHW,MAAM,GACJ,OAAO,CAAC,MAAM,CAAC,CAM3B;AAED;;;;GAIG;AACH,8CAHW,MAAM,GACJ,OAAO,CAAC,MAAM,CAAC,CAM3B"}
|