@cldmv/holdmytask 1.0.0

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/index.cjs ADDED
@@ -0,0 +1,29 @@
1
+ /**
2
+ * @Project: @cldmv/holdmytask
3
+ * @Filename: /index.cjs
4
+ * @Date: 2025-11-08 14:04:10 -08:00 (1762639450)
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:18 -08:00 (1762734138)
10
+ * -----
11
+ * @Copyright: Copyright (c) 2013-2025 Catalyzed Motivation Inc. All rights reserved.
12
+ */
13
+
14
+ /**
15
+ * CommonJS entry point for holdmytask
16
+ *
17
+ * This file provides CommonJS (require) support for the holdmytask library.
18
+ * It imports and re-exports the main HoldMyTask class from the ESM module.
19
+ *
20
+ * @module holdmytask
21
+ */
22
+
23
+ const { createRequire } = require("module");
24
+ const requireESM = createRequire(__filename);
25
+
26
+ const { HoldMyTask } = requireESM("./index.mjs");
27
+
28
+ module.exports = { HoldMyTask };
29
+ module.exports.HoldMyTask = HoldMyTask;
package/index.mjs ADDED
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @Project: @cldmv/holdmytask
3
+ * @Filename: /index.mjs
4
+ * @Date: 2025-11-08 14:04:10 -08:00 (1762639450)
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:22 -08:00 (1762734142)
10
+ * -----
11
+ * @Copyright: Copyright (c) 2013-2025 Catalyzed Motivation Inc. All rights reserved.
12
+ */
13
+
14
+ // Development environment check (must happen before holdmytask imports)
15
+ (async () => {
16
+ try {
17
+ await import("@cldmv/holdmytask/devcheck");
18
+ } catch {
19
+ // ignore
20
+ }
21
+ })();
22
+
23
+ export { HoldMyTask } from "@cldmv/holdmytask/main";
package/package.json ADDED
@@ -0,0 +1,117 @@
1
+ {
2
+ "name": "@cldmv/holdmytask",
3
+ "version": "1.0.0",
4
+ "description": "A tiny task queue that waits until your task is ready",
5
+ "main": "./index.cjs",
6
+ "module": "./index.mjs",
7
+ "types": "./types/index.d.mts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./types/index.d.mts",
11
+ "import": "./index.mjs",
12
+ "require": "./index.cjs"
13
+ },
14
+ "./devcheck": {
15
+ "types": "./types/devcheck.d.mts",
16
+ "import": "./devcheck.mjs"
17
+ },
18
+ "./main": {
19
+ "development": {
20
+ "types": "./types/src/hold-my-task.d.mts",
21
+ "import": "./src/hold-my-task.mjs"
22
+ },
23
+ "types": "./types/dist/hold-my-task.d.mts",
24
+ "import": "./dist/hold-my-task.mjs"
25
+ }
26
+ },
27
+ "engines": {
28
+ "node": ">=18.12"
29
+ },
30
+ "scripts": {
31
+ "test": "vitest run --config .configs/vitest.config.mjs",
32
+ "test:run": "vitest run",
33
+ "test:coverage": "vitest run --coverage",
34
+ "test:types": "tsc --noEmit --project .configs/tsconfig.dts.jsonc",
35
+ "lint": "eslint --config .configs/eslint.config.mjs .",
36
+ "build": "node build.mjs",
37
+ "build:types": "tsc --project .configs/tsconfig.dts.jsonc",
38
+ "build:ci": "npm run build:types && npm run test:types && npm run build",
39
+ "precommit": "npm run build:types && npm run test:types && npm run lint && npm run test"
40
+ },
41
+ "keywords": [
42
+ "task",
43
+ "queue",
44
+ "scheduler",
45
+ "priority",
46
+ "delay",
47
+ "concurrency",
48
+ "async",
49
+ "promise",
50
+ "callback",
51
+ "timeout",
52
+ "expire",
53
+ "bypass",
54
+ "task-queue",
55
+ "job-queue",
56
+ "priority-queue",
57
+ "nodejs",
58
+ "esm",
59
+ "cjs",
60
+ "typescript"
61
+ ],
62
+ "author": {
63
+ "name": "Shinrai",
64
+ "company": "CLDMV",
65
+ "email": "git+npm@cldmv.net",
66
+ "url": "https://cldmv.net"
67
+ },
68
+ "contributors": [
69
+ {
70
+ "name": "CLDMV",
71
+ "url": "https://github.com/CLDMV"
72
+ }
73
+ ],
74
+ "license": "Apache-2.0",
75
+ "funding": {
76
+ "type": "github",
77
+ "url": "https://github.com/sponsors/shinrai"
78
+ },
79
+ "type": "module",
80
+ "devDependencies": {
81
+ "@eslint/css": "^0.14.1",
82
+ "@eslint/js": "^9.39.1",
83
+ "@eslint/json": "^0.14.0",
84
+ "@eslint/markdown": "^7.5.1",
85
+ "@html-eslint/eslint-plugin": "^0.48.0",
86
+ "@html-eslint/parser": "^0.48.0",
87
+ "@types/node": "^20.0.0",
88
+ "@vitest/ui": "^1.0.0",
89
+ "eslint": "^9.39.1",
90
+ "globals": "^16.5.0",
91
+ "prettier": "^3.0.0",
92
+ "typescript": "^5.0.0",
93
+ "vitest": "^1.0.0"
94
+ },
95
+ "repository": {
96
+ "type": "git",
97
+ "url": "git+https://github.com/CLDMV/holdmytask.git"
98
+ },
99
+ "bugs": {
100
+ "url": "https://github.com/CLDMV/holdmytask/issues"
101
+ },
102
+ "homepage": "https://github.com/CLDMV/holdmytask#readme",
103
+ "publishConfig": {
104
+ "access": "public"
105
+ },
106
+ "files": [
107
+ "index.mjs",
108
+ "index.cjs",
109
+ "README.md",
110
+ "LICENSE",
111
+ "types/dist/",
112
+ "types/index.d.mts",
113
+ "types/index.d.mts.map",
114
+ "dist/"
115
+ ],
116
+ "sideEffects": false
117
+ }
@@ -0,0 +1,189 @@
1
+ /**
2
+ * A sophisticated task queue that manages task execution with priorities, delays, and concurrency control.
3
+ * Tasks can be scheduled with timestamps, priorities, and completion delays between tasks of the same priority.
4
+ * Supports both callback and promise-based APIs with comprehensive lifecycle management.
5
+ * @extends EventEmitter
6
+ */
7
+ export class HoldMyTask extends EventEmitter<[never]> {
8
+ /**
9
+ * Creates a new HoldMyTask queue instance.
10
+ * @param {Object} [options={}] - Configuration options for the task queue
11
+ * @param {number} [options.concurrency=1] - Maximum number of tasks to run concurrently
12
+ * @param {number} [options.tick=25] - Scheduler tick interval in milliseconds
13
+ * @param {boolean} [options.autoStart=true] - Whether to automatically start the scheduler
14
+ * @param {number} [options.defaultPriority=0] - Default priority for tasks (higher numbers = higher priority)
15
+ * @param {number} [options.maxQueue=Infinity] - Maximum number of tasks that can be queued
16
+ * @param {Object} [options.delays={}] - Map of priority levels to delay times in milliseconds between task completions
17
+ * @example
18
+ * const queue = new HoldMyTask({
19
+ * concurrency: 2,
20
+ * tick: 50,
21
+ * defaultPriority: 1,
22
+ * delays: { 0: 1000, 1: 500 }
23
+ * });
24
+ */
25
+ constructor(options?: {
26
+ concurrency?: number;
27
+ tick?: number;
28
+ autoStart?: boolean;
29
+ defaultPriority?: number;
30
+ maxQueue?: number;
31
+ delays?: any;
32
+ });
33
+ options: {
34
+ concurrency: number;
35
+ tick: number;
36
+ autoStart: boolean;
37
+ defaultPriority: number;
38
+ maxQueue: number;
39
+ delays: any;
40
+ };
41
+ pendingHeap: MinHeap;
42
+ readyHeap: MinHeap;
43
+ running: Set<any>;
44
+ tasks: Map<any, any>;
45
+ nextId: number;
46
+ enqueueSeq: number;
47
+ isActive: boolean;
48
+ destroyed: boolean;
49
+ lastCompletedPriority: any;
50
+ nextAvailableTime: number;
51
+ /**
52
+ * Adds a task to the queue for execution. Supports both callback and promise-based APIs.
53
+ * @param {Function} task - The task function to execute. Can be sync or async.
54
+ * @param {Function|Object} [optionsOrCallback] - Either a callback function or options object
55
+ * @param {Object} [options={}] - Additional options (if callback was provided as second parameter)
56
+ * @param {number} [options.priority] - Task priority (higher numbers run first)
57
+ * @param {number} [options.timestamp] - When the task should be ready to run (milliseconds since epoch)
58
+ * @param {AbortSignal} [options.signal] - AbortSignal to cancel the task
59
+ * @param {number} [options.timeout] - Task timeout in milliseconds
60
+ * @param {number} [options.delay] - Delay after task completion before next task of same priority
61
+ * @param {boolean} [options.bypassDelay] - If true, skip any active delay period and start immediately
62
+ * @param {*} [options.metadata] - Arbitrary metadata to attach to the task
63
+ * @returns {Promise|Object} Promise (if no callback) or task control object with id, cancel, status methods
64
+ * @throws {Error} If queue is destroyed or full
65
+ * @example
66
+ * // Promise API
67
+ * const result = await queue.enqueue(async () => fetchData());
68
+ *
69
+ * // Callback API
70
+ * queue.enqueue(() => processData(), (err, result) => {
71
+ * if (err) console.error(err);
72
+ * else console.log(result);
73
+ * });
74
+ *
75
+ * // With options
76
+ * const task = queue.enqueue(myTask, { priority: 5, timeout: 30000 });
77
+ *
78
+ * // Bypass current delay for urgent task
79
+ * const urgent = queue.enqueue(urgentTask, { priority: 10, bypassDelay: true });
80
+ *
81
+ * // Alternative: use delay: -1 to bypass
82
+ * const urgent2 = queue.enqueue(urgentTask, { priority: 10, delay: -1 });
83
+ */
84
+ enqueue(task: Function, optionsOrCallback?: Function | any, options?: {
85
+ priority?: number;
86
+ timestamp?: number;
87
+ signal?: AbortSignal;
88
+ timeout?: number;
89
+ delay?: number;
90
+ bypassDelay?: boolean;
91
+ metadata?: any;
92
+ }): Promise<any> | any;
93
+ /**
94
+ * Cancels a pending task by ID.
95
+ * @param {string} id - The task ID to cancel
96
+ * @param {string} [reason="Task canceled"] - Reason for cancellation
97
+ * @returns {void}
98
+ * @example
99
+ * const task = queue.enqueue(() => longRunningTask());
100
+ * queue.cancelTask(task.id, "User requested cancellation");
101
+ */
102
+ cancelTask(id: string, reason?: string): void;
103
+ /**
104
+ * Pauses the task queue, stopping execution of new tasks.
105
+ * Currently running tasks will continue to completion.
106
+ * @returns {void}
107
+ * @example
108
+ * queue.pause();
109
+ * // Queue stops processing new tasks
110
+ */
111
+ pause(): void;
112
+ /**
113
+ * Resumes the task queue after being paused.
114
+ * @returns {void}
115
+ * @example
116
+ * queue.resume();
117
+ * // Queue resumes processing tasks
118
+ */
119
+ resume(): void;
120
+ /**
121
+ * Clears all pending and ready tasks from the queue.
122
+ * Currently running tasks will continue to completion.
123
+ * @returns {void}
124
+ * @example
125
+ * queue.clear();
126
+ * // All queued tasks are removed
127
+ */
128
+ clear(): void;
129
+ /**
130
+ * Returns the total number of tasks in the queue (pending, ready, and running).
131
+ * @returns {number} Total number of tasks
132
+ * @example
133
+ * const totalTasks = queue.size(); // 5
134
+ */
135
+ size(): number;
136
+ /**
137
+ * Returns the number of currently running tasks.
138
+ * @returns {number} Number of running tasks
139
+ * @example
140
+ * const runningTasks = queue.inflight(); // 2
141
+ */
142
+ inflight(): number;
143
+ /**
144
+ * Destroys the queue, canceling all tasks and stopping the scheduler.
145
+ * Once destroyed, the queue cannot be reused.
146
+ * @returns {void}
147
+ * @example
148
+ * queue.destroy();
149
+ * // Queue is permanently shut down
150
+ */
151
+ destroy(): void;
152
+ /**
153
+ * Returns the current timestamp in milliseconds.
154
+ * @returns {number} Current timestamp
155
+ * @example
156
+ * const timestamp = queue.now(); // 1699564800000
157
+ */
158
+ now(): number;
159
+ /**
160
+ * Schedules the next scheduler tick based on when tasks become ready.
161
+ * @returns {void}
162
+ * @private
163
+ */
164
+ private scheduleNextTick;
165
+ intervalId: NodeJS.Timeout;
166
+ timeoutId: NodeJS.Timeout;
167
+ /**
168
+ * Main scheduler tick that moves ready tasks and starts execution.
169
+ * @returns {void}
170
+ * @private
171
+ */
172
+ private schedulerTick;
173
+ /**
174
+ * Starts execution of a ready task.
175
+ * @param {Object} item - The task item to execute
176
+ * @returns {Promise<void>}
177
+ * @private
178
+ */
179
+ private startTask;
180
+ /**
181
+ * Clears all active timers (intervals and timeouts).
182
+ * @returns {void}
183
+ * @private
184
+ */
185
+ private clearTimers;
186
+ }
187
+ import { EventEmitter } from "events";
188
+ import { MinHeap } from "./utils.mjs";
189
+ //# sourceMappingURL=hold-my-task.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hold-my-task.d.mts","sourceRoot":"","sources":["../../dist/hold-my-task.mjs"],"names":[],"mappings":"AAgBA;;;;;GAKG;AACH;IACC;;;;;;;;;;;;;;;;OAgBG;IACH,sBAdG;QAAyB,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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
+ * A min-heap implementation with custom comparison function.
15
+ * Maintains the heap property where parent nodes are smaller than their children.
16
+ */
17
+ export class MinHeap {
18
+ /**
19
+ * Creates a new MinHeap with a custom comparison function.
20
+ * @param {Function} compare - Comparison function that returns negative if a < b, positive if a > b, 0 if equal
21
+ * @example
22
+ * // Priority queue (higher priority = smaller value)
23
+ * const heap = new MinHeap((a, b) => a.priority - b.priority);
24
+ */
25
+ constructor(compare: Function);
26
+ heap: any[];
27
+ compare: Function;
28
+ /**
29
+ * Adds an item to the heap, maintaining heap property.
30
+ * @param {*} item - The item to add to the heap
31
+ * @returns {void}
32
+ * @example
33
+ * heap.push({ value: 5, priority: 1 });
34
+ */
35
+ push(item: any): void;
36
+ /**
37
+ * Removes and returns the minimum item from the heap.
38
+ * @returns {*|undefined} The minimum item, or undefined if heap is empty
39
+ * @example
40
+ * const min = heap.pop(); // Returns item with smallest comparison value
41
+ */
42
+ pop(): any | undefined;
43
+ /**
44
+ * Returns the minimum item without removing it from the heap.
45
+ * @returns {*|undefined} The minimum item, or undefined if heap is empty
46
+ * @example
47
+ * const min = heap.peek(); // Look at minimum without removing
48
+ */
49
+ peek(): any | undefined;
50
+ /**
51
+ * Returns the number of items in the heap.
52
+ * @returns {number} The size of the heap
53
+ * @example
54
+ * const count = heap.size(); // 5
55
+ */
56
+ size(): number;
57
+ /**
58
+ * Moves an item up the heap to maintain heap property after insertion.
59
+ * @param {number} index - Index of the item to bubble up
60
+ * @returns {void}
61
+ * @private
62
+ */
63
+ private bubbleUp;
64
+ /**
65
+ * Moves an item down the heap to maintain heap property after removal.
66
+ * @param {number} index - Index of the item to sink down
67
+ * @returns {void}
68
+ * @private
69
+ */
70
+ private sinkDown;
71
+ }
72
+ //# sourceMappingURL=utils.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../../dist/utils.mjs"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH;;;GAGG;AACH;IACC;;;;;;OAMG;IACH,+BAGC;IAFA,YAAc;IACd,kBAAsB;IAGvB;;;;;;OAMG;IACH,WALW,GAAC,GACC,IAAI,CAOhB;IAED;;;;;OAKG;IACH,OAJa,GAAC,GAAC,SAAS,CAavB;IAED;;;;;OAKG;IACH,QAJa,GAAC,GAAC,SAAS,CAMvB;IAED;;;;;OAKG;IACH,QAJa,MAAM,CAMlB;IAED;;;;;OAKG;IACH,iBAOC;IAED;;;;;OAKG;IACH,iBAkBC;CACD"}