@cldmv/holdmytask 1.2.0 → 1.3.1
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 +324 -5
- package/dist/hold-my-task.mjs +1318 -53
- package/index.cjs +10 -1
- package/index.mjs +13 -1
- package/package.json +1 -1
- package/types/dist/hold-my-task.d.mts +473 -32
- package/types/dist/hold-my-task.d.mts.map +1 -1
- package/types/index.d.mts +3 -0
- package/types/index.d.mts.map +1 -0
package/index.cjs
CHANGED
|
@@ -25,5 +25,14 @@ const requireESM = createRequire(__filename);
|
|
|
25
25
|
|
|
26
26
|
const { HoldMyTask } = requireESM("./index.mjs");
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
// Export main class
|
|
29
|
+
module.exports = HoldMyTask; // Default export
|
|
29
30
|
module.exports.HoldMyTask = HoldMyTask;
|
|
31
|
+
|
|
32
|
+
// Common queue system aliases
|
|
33
|
+
module.exports.queue = HoldMyTask;
|
|
34
|
+
module.exports.Queue = HoldMyTask;
|
|
35
|
+
module.exports.TaskManager = HoldMyTask;
|
|
36
|
+
module.exports.TaskQueue = HoldMyTask;
|
|
37
|
+
module.exports.QueueManager = HoldMyTask;
|
|
38
|
+
module.exports.TaskProcessor = HoldMyTask;
|
package/index.mjs
CHANGED
|
@@ -20,4 +20,16 @@
|
|
|
20
20
|
}
|
|
21
21
|
})();
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
// Export everything from main module
|
|
24
|
+
export * from "@cldmv/holdmytask/main";
|
|
25
|
+
|
|
26
|
+
// Default export
|
|
27
|
+
export { HoldMyTask as default } from "@cldmv/holdmytask/main";
|
|
28
|
+
|
|
29
|
+
// Common queue system aliases
|
|
30
|
+
export { HoldMyTask as queue } from "@cldmv/holdmytask/main";
|
|
31
|
+
export { HoldMyTask as Queue } from "@cldmv/holdmytask/main";
|
|
32
|
+
export { HoldMyTask as TaskManager } from "@cldmv/holdmytask/main";
|
|
33
|
+
export { HoldMyTask as TaskQueue } from "@cldmv/holdmytask/main";
|
|
34
|
+
export { HoldMyTask as QueueManager } from "@cldmv/holdmytask/main";
|
|
35
|
+
export { HoldMyTask as TaskProcessor } from "@cldmv/holdmytask/main";
|
package/package.json
CHANGED
|
@@ -13,31 +13,103 @@ export class HoldMyTask extends EventEmitter<[never]> {
|
|
|
13
13
|
* @param {boolean} [options.autoStart=true] - Whether to automatically start the scheduler
|
|
14
14
|
* @param {number} [options.defaultPriority=0] - Default priority for tasks (higher numbers = higher priority)
|
|
15
15
|
* @param {number} [options.maxQueue=Infinity] - Maximum number of tasks that can be queued
|
|
16
|
-
* @param {Object} [options.delays={}] -
|
|
16
|
+
* @param {Object} [options.delays={}] - DEPRECATED: Use priorities[priority].delay instead. Legacy priority-to-delay mapping (auto-converted to priorities)
|
|
17
|
+
* @param {boolean} [options.smartScheduling=true] - Use dynamic timeouts instead of constant polling for better performance
|
|
18
|
+
* @param {number} [options.tick=25] - Polling interval in milliseconds when smartScheduling is disabled
|
|
19
|
+
* @param {number} [options.healingInterval=5000] - Self-healing check interval in milliseconds (smart scheduling only)
|
|
20
|
+
* @param {Object} [options.priorities={}] - Priority-specific default configurations
|
|
21
|
+
* @param {Object} [options.coalescing] - Enhanced coalescing configuration (preferred over flat options)
|
|
22
|
+
* @param {Object} [options.coalescing.defaults] - Default coalescing settings for all keys
|
|
23
|
+
* @param {number} [options.coalescing.defaults.windowDuration=200] - Default window duration in milliseconds
|
|
24
|
+
* @param {number} [options.coalescing.defaults.maxDelay=1000] - Default maximum delay in milliseconds
|
|
25
|
+
* @param {number} [options.coalescing.defaults.postDelay] - Default post-completion delay in milliseconds
|
|
26
|
+
* @param {number} [options.coalescing.defaults.startDelay] - Default pre-execution delay in milliseconds
|
|
27
|
+
* @param {number} [options.coalescing.defaults.delay] - DEPRECATED: Use postDelay instead
|
|
28
|
+
* @param {number} [options.coalescing.defaults.start] - DEPRECATED: Use startDelay instead
|
|
29
|
+
* @param {boolean} [options.coalescing.defaults.resolveAllPromises=true] - Default promise resolution behavior
|
|
30
|
+
* @param {boolean} [options.coalescing.defaults.multipleCallbacks=false] - Default callback execution behavior
|
|
31
|
+
* @param {Object} [options.coalescing.keys] - Per-coalescingKey configuration overrides (windowDuration, maxDelay, postDelay, startDelay, etc.)
|
|
32
|
+
* @param {number} [options.coalescingWindowDuration=200] - DEPRECATED: Use coalescing.defaults.windowDuration instead
|
|
33
|
+
* @param {number} [options.coalescingMaxDelay=1000] - DEPRECATED: Use coalescing.defaults.maxDelay instead
|
|
34
|
+
* @param {boolean} [options.coalescingMultipleCallbacks=false] - DEPRECATED: Use coalescing.defaults.multipleCallbacks instead
|
|
35
|
+
* @param {boolean} [options.coalescingResolveAllPromises=true] - DEPRECATED: Use coalescing.defaults.resolveAllPromises instead
|
|
17
36
|
* @example
|
|
37
|
+
* // Enhanced configuration with priority defaults and extended coalescing
|
|
18
38
|
* const queue = new HoldMyTask({
|
|
19
39
|
* concurrency: 2,
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
40
|
+
* delays: { 0: 1000, 1: 500 },
|
|
41
|
+
* priorities: {
|
|
42
|
+
* 1: { postDelay: 100, startDelay: 0 }, // High priority: 100ms post-completion delay, immediate start
|
|
43
|
+
* 2: { postDelay: 200, startDelay: 50 }, // Medium priority: 200ms post-completion delay, 50ms pre-execution delay
|
|
44
|
+
* 3: { postDelay: 0, startDelay: 100 } // Low priority: no post-completion delay, 100ms pre-execution delay
|
|
45
|
+
* },
|
|
46
|
+
* coalescing: {
|
|
47
|
+
* defaults: {
|
|
48
|
+
* windowDuration: 200,
|
|
49
|
+
* maxDelay: 1000,
|
|
50
|
+
* postDelay: 50,
|
|
51
|
+
* startDelay: 25,
|
|
52
|
+
* resolveAllPromises: true
|
|
53
|
+
* },
|
|
54
|
+
* keys: {
|
|
55
|
+
* 'ui.update': { windowDuration: 100, maxDelay: 500, postDelay: 25, startDelay: 0 },
|
|
56
|
+
* 'api.batch': { windowDuration: 1000, maxDelay: 5000, postDelay: 100, startDelay: 200 },
|
|
57
|
+
* 'device.control': { windowDuration: 50, maxDelay: 200, delay: 10, start: 5 }
|
|
58
|
+
* }
|
|
59
|
+
* }
|
|
23
60
|
* });
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* // Backward compatible (deprecated but still supported)
|
|
64
|
+
* const legacyQueue = new HoldMyTask({
|
|
65
|
+
* coalescingWindowDuration: 1500,
|
|
66
|
+
* coalescingMaxDelay: 3000,
|
|
67
|
+
* coalescingResolveAllPromises: true
|
|
68
|
+
* });
|
|
69
|
+
*/
|
|
70
|
+
/**
|
|
71
|
+
* Transforms delay properties for backwards compatibility.
|
|
72
|
+
* Converts 'start' -> 'startDelay' and 'delay' -> 'postDelay' while preserving new names.
|
|
73
|
+
* @param {Object} config - Configuration object that may contain old or new property names
|
|
74
|
+
* @param {HoldMyTask} [instance] - Optional instance to emit deprecation warnings on
|
|
75
|
+
* @returns {Object} Transformed configuration with new property names
|
|
76
|
+
* @private
|
|
77
|
+
* @internal
|
|
78
|
+
*/
|
|
79
|
+
private static _transformDelayProperties;
|
|
80
|
+
/**
|
|
81
|
+
* Internal convenience method to create a new HoldMyTask instance with async initialization.
|
|
82
|
+
* This enables event listeners to be attached before validation errors can occur.
|
|
83
|
+
* @param {Object} [options={}] - Configuration options
|
|
84
|
+
* @returns {Promise<HoldMyTask>} Promise that resolves to the initialized instance
|
|
85
|
+
* @private
|
|
86
|
+
* @example
|
|
87
|
+
* // Internal usage - prefer new HoldMyTask({ sync: false }) for public API
|
|
88
|
+
* const queue = await HoldMyTask._create({ maxQueue: 100 });
|
|
89
|
+
*/
|
|
90
|
+
private static _create;
|
|
91
|
+
constructor(options?: {});
|
|
92
|
+
/**
|
|
93
|
+
* Synchronous initialization for backwards compatibility
|
|
94
|
+
* @private
|
|
95
|
+
* @param {Object} options - Configuration options
|
|
96
|
+
*/
|
|
97
|
+
private _initializeSync;
|
|
98
|
+
_syncMode: boolean;
|
|
99
|
+
/**
|
|
100
|
+
* Asynchronous initialization for modern usage
|
|
101
|
+
* @private
|
|
102
|
+
* @param {Object} options - Configuration options
|
|
103
|
+
* @returns {Promise<HoldMyTask>} Promise that resolves to this instance
|
|
104
|
+
*/
|
|
105
|
+
private _initializeAsync;
|
|
106
|
+
/**
|
|
107
|
+
* Common initialization logic used by both sync and async modes
|
|
108
|
+
* @private
|
|
109
|
+
* @param {Object} options - Configuration options
|
|
24
110
|
*/
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
};
|
|
111
|
+
private _initializeCommon;
|
|
112
|
+
options: any;
|
|
41
113
|
pendingHeap: MinHeap;
|
|
42
114
|
readyHeap: MinHeap;
|
|
43
115
|
running: Set<any>;
|
|
@@ -47,18 +119,33 @@ export class HoldMyTask extends EventEmitter<[never]> {
|
|
|
47
119
|
isActive: boolean;
|
|
48
120
|
destroyed: boolean;
|
|
49
121
|
lastCompletedPriority: any;
|
|
50
|
-
nextAvailableTime:
|
|
122
|
+
nextAvailableTime: any;
|
|
123
|
+
schedulerTimeout: NodeJS.Timeout;
|
|
124
|
+
healingInterval: NodeJS.Timeout;
|
|
125
|
+
lastSchedulerRun: number;
|
|
126
|
+
intervalId: NodeJS.Timeout;
|
|
127
|
+
coalescingGroups: Map<any, any>;
|
|
128
|
+
coalescingRepresentatives: Map<any, any>;
|
|
129
|
+
nextGroupId: number;
|
|
51
130
|
/**
|
|
52
131
|
* Adds a task to the queue for execution. Supports both callback and promise-based APIs.
|
|
53
132
|
* @param {Function} task - The task function to execute. Can be sync or async.
|
|
54
133
|
* @param {Function|Object} [optionsOrCallback] - Either a callback function or options object
|
|
55
134
|
* @param {Object} [options={}] - Additional options (if callback was provided as second parameter)
|
|
135
|
+
* @param {string|number} [options.id] - Custom task ID for identification and later reference (must be unique)
|
|
56
136
|
* @param {number} [options.priority] - Task priority (higher numbers run first)
|
|
57
137
|
* @param {number} [options.timestamp] - When the task should be ready to run (milliseconds since epoch)
|
|
138
|
+
* @param {number} [options.start] - Milliseconds from now when the task should be ready to run (convenience for timestamp calculation)
|
|
58
139
|
* @param {AbortSignal} [options.signal] - AbortSignal to cancel the task
|
|
59
|
-
* @param {number} [options.timeout] - Task timeout in milliseconds
|
|
60
|
-
* @param {number} [options.
|
|
140
|
+
* @param {number} [options.timeout] - Task timeout in milliseconds (for execution time limit)
|
|
141
|
+
* @param {number} [options.expire] - Task expiration timestamp or milliseconds from now (for queue waiting time limit)
|
|
142
|
+
* @param {number} [options.delay] - DEPRECATED: Use postDelay instead. Delay after task completion before next task of same priority
|
|
61
143
|
* @param {boolean} [options.bypassDelay] - If true, skip any active delay period and start immediately
|
|
144
|
+
* @param {string} [options.coalescingKey] - Key for task coalescing - tasks with same key will be coalesced within windows
|
|
145
|
+
* @param {number} [options.coalescingWindowDuration] - Override coalescing window duration (task-level override of key-level and defaults)
|
|
146
|
+
* @param {number} [options.coalescingMaxDelay] - Override coalescing max delay (task-level override of key-level and defaults)
|
|
147
|
+
* @param {boolean} [options.coalescingMultipleCallbacks] - Override callback behavior (task-level override of key-level and defaults)
|
|
148
|
+
* @param {boolean} [options.coalescingResolveAllPromises] - Override promise resolution behavior (task-level override of key-level and defaults)
|
|
62
149
|
* @param {*} [options.metadata] - Arbitrary metadata to attach to the task
|
|
63
150
|
* @returns {Promise|Object} Promise (if no callback) or task control object with id, cancel, status methods
|
|
64
151
|
* @throws {Error} If queue is destroyed or full
|
|
@@ -73,33 +160,154 @@ export class HoldMyTask extends EventEmitter<[never]> {
|
|
|
73
160
|
* });
|
|
74
161
|
*
|
|
75
162
|
* // With options
|
|
76
|
-
* const task = queue.enqueue(myTask, { priority: 5, timeout: 30000 });
|
|
163
|
+
* const task = queue.enqueue(myTask, { priority: 5, timeout: 30000, expire: 10000 });
|
|
77
164
|
*
|
|
78
165
|
* // Bypass current delay for urgent task
|
|
79
166
|
* const urgent = queue.enqueue(urgentTask, { priority: 10, bypassDelay: true });
|
|
80
167
|
*
|
|
81
168
|
* // Alternative: use delay: -1 to bypass
|
|
82
169
|
* const urgent2 = queue.enqueue(urgentTask, { priority: 10, delay: -1 });
|
|
170
|
+
*
|
|
171
|
+
* // Coalescing tasks - multiple device status checks become one
|
|
172
|
+
* queue.enqueue(checkDeviceStatus, callback1, { coalescingKey: "device-123", coalescingWindowDuration: 1000 });
|
|
173
|
+
* queue.enqueue(checkDeviceStatus, callback2, { coalescingKey: "device-123" }); // Gets coalesced with first
|
|
83
174
|
*/
|
|
84
175
|
enqueue(task: Function, optionsOrCallback?: Function | any, options?: {
|
|
176
|
+
id?: string | number;
|
|
85
177
|
priority?: number;
|
|
86
178
|
timestamp?: number;
|
|
179
|
+
start?: number;
|
|
87
180
|
signal?: AbortSignal;
|
|
88
181
|
timeout?: number;
|
|
182
|
+
expire?: number;
|
|
89
183
|
delay?: number;
|
|
90
184
|
bypassDelay?: boolean;
|
|
185
|
+
coalescingKey?: string;
|
|
186
|
+
coalescingWindowDuration?: number;
|
|
187
|
+
coalescingMaxDelay?: number;
|
|
188
|
+
coalescingMultipleCallbacks?: boolean;
|
|
189
|
+
coalescingResolveAllPromises?: boolean;
|
|
91
190
|
metadata?: any;
|
|
92
191
|
}): Promise<any> | any;
|
|
192
|
+
/**
|
|
193
|
+
* Handles tasks with coalescingKey through the coalescing system.
|
|
194
|
+
* @private
|
|
195
|
+
* @internal
|
|
196
|
+
* @param {string} id - Task ID
|
|
197
|
+
* @param {Function} task - Task function
|
|
198
|
+
* @param {Function|null} callback - Callback function (null for promise API)
|
|
199
|
+
* @param {Object} options - Task options
|
|
200
|
+
* @param {number} now - Current timestamp
|
|
201
|
+
* @returns {Promise|Object} Promise or task handle
|
|
202
|
+
*/
|
|
203
|
+
private _handleCoalescingTask;
|
|
204
|
+
/**
|
|
205
|
+
* Finds a compatible coalescing group for a new task.
|
|
206
|
+
* @private
|
|
207
|
+
* @internal
|
|
208
|
+
* @param {string} coalescingKey - The coalescing key
|
|
209
|
+
* @param {number} now - Current timestamp
|
|
210
|
+
* @param {number} windowEnd - End of new task's coalescing window
|
|
211
|
+
* @param {number} mustRunBy - New task's mustRunBy deadline
|
|
212
|
+
* @returns {Object|null} Compatible coalescing group or null
|
|
213
|
+
*/
|
|
214
|
+
private _findCompatibleCoalescingGroup;
|
|
215
|
+
/**
|
|
216
|
+
* Creates a new coalescing group with a representative task in the main queue.
|
|
217
|
+
* @private
|
|
218
|
+
* @internal
|
|
219
|
+
* @param {string} coalescingKey - The coalescing key
|
|
220
|
+
* @param {Object} taskItem - The first task item for this group
|
|
221
|
+
* @param {number} windowEnd - End of coalescing window
|
|
222
|
+
* @param {number} mustRunBy - Latest execution deadline
|
|
223
|
+
* @param {boolean} multipleCallbacks - Whether to call multiple callbacks
|
|
224
|
+
* @param {boolean} resolveAllPromises - Whether to resolve all promises with same result
|
|
225
|
+
* @returns {Object} The created coalescing group
|
|
226
|
+
*/
|
|
227
|
+
private _createCoalescingGroup;
|
|
228
|
+
/**
|
|
229
|
+
* Adds a task to an existing coalescing group, updating the representative if needed.
|
|
230
|
+
* @private
|
|
231
|
+
* @internal
|
|
232
|
+
* @param {Object} group - The existing coalescing group
|
|
233
|
+
* @param {Object} taskItem - The new task item to add
|
|
234
|
+
* @param {number} windowEnd - End of coalescing window
|
|
235
|
+
* @param {number} mustRunBy - Latest execution deadline
|
|
236
|
+
* @param {number} now - Current timestamp
|
|
237
|
+
* @returns {void}
|
|
238
|
+
*/
|
|
239
|
+
private _addToCoalescingGroup;
|
|
240
|
+
/**
|
|
241
|
+
* Creates the representative task function that manages coalesced task execution.
|
|
242
|
+
* @private
|
|
243
|
+
* @internal
|
|
244
|
+
* @param {string} coalescingKey - The coalescing key
|
|
245
|
+
* @param {string} groupId - The group ID
|
|
246
|
+
* @returns {Function} The representative task function
|
|
247
|
+
*/
|
|
248
|
+
private _createCoalescingRepresentativeTask;
|
|
249
|
+
/**
|
|
250
|
+
* Resolves all callbacks/promises in a coalescing group.
|
|
251
|
+
* @private
|
|
252
|
+
* @internal
|
|
253
|
+
* @param {Object} group - The coalescing group
|
|
254
|
+
* @param {Error|null} error - Error if task failed
|
|
255
|
+
* @param {*} result - Result if task succeeded
|
|
256
|
+
* @returns {void}
|
|
257
|
+
*/
|
|
258
|
+
private _resolveCoalescingGroup;
|
|
259
|
+
/**
|
|
260
|
+
* Cancels a specific task within a coalescing group.
|
|
261
|
+
* @private
|
|
262
|
+
* @internal
|
|
263
|
+
* @param {string} taskId - ID of task to cancel
|
|
264
|
+
* @param {string} coalescingKey - The coalescing key
|
|
265
|
+
* @param {string} reason - Cancellation reason
|
|
266
|
+
* @returns {void}
|
|
267
|
+
*/
|
|
268
|
+
private _cancelCoalescingTask;
|
|
269
|
+
/**
|
|
270
|
+
* Cleans up a coalescing group after completion or cancellation.
|
|
271
|
+
* @private
|
|
272
|
+
* @internal
|
|
273
|
+
* @param {string} coalescingKey - The coalescing key
|
|
274
|
+
* @param {string} groupId - The group ID to clean up
|
|
275
|
+
* @returns {void}
|
|
276
|
+
*/
|
|
277
|
+
private _cleanupCoalescingGroup;
|
|
278
|
+
/**
|
|
279
|
+
* Rebuilds the heap structures to reflect updated priorities/timing.
|
|
280
|
+
* @private
|
|
281
|
+
* @internal
|
|
282
|
+
* @returns {void}
|
|
283
|
+
*/
|
|
284
|
+
private _rebuildHeaps;
|
|
285
|
+
/**
|
|
286
|
+
* Creates a task handle for both regular and coalescing tasks.
|
|
287
|
+
* @private
|
|
288
|
+
* @internal
|
|
289
|
+
* @param {Object} item - The task item
|
|
290
|
+
* @param {Function|null} callback - Callback function
|
|
291
|
+
* @returns {Promise|Object} Promise or task control object
|
|
292
|
+
*/
|
|
293
|
+
private _createTaskHandle;
|
|
93
294
|
/**
|
|
94
295
|
* Cancels a pending task by ID.
|
|
95
296
|
* @param {string} id - The task ID to cancel
|
|
96
297
|
* @param {string} [reason="Task canceled"] - Reason for cancellation
|
|
97
|
-
* @returns {
|
|
298
|
+
* @returns {boolean} True if task was found and cancelled, false otherwise
|
|
98
299
|
* @example
|
|
99
300
|
* const task = queue.enqueue(() => longRunningTask());
|
|
100
|
-
* queue.
|
|
301
|
+
* const cancelled = queue.cancel(task.id, "User requested cancellation");
|
|
101
302
|
*/
|
|
102
|
-
|
|
303
|
+
cancel(id: string, reason?: string): boolean;
|
|
304
|
+
/**
|
|
305
|
+
* Alias for cancel() method for backward compatibility.
|
|
306
|
+
* @param {string|number} id - The task ID to cancel
|
|
307
|
+
* @param {string} [reason="Task canceled"] - Reason for cancellation
|
|
308
|
+
* @returns {boolean} True if task was found and cancelled, false otherwise
|
|
309
|
+
*/
|
|
310
|
+
cancelTask(id: string | number, reason?: string): boolean;
|
|
103
311
|
/**
|
|
104
312
|
* Pauses the task queue, stopping execution of new tasks.
|
|
105
313
|
* Currently running tasks will continue to completion.
|
|
@@ -127,12 +335,19 @@ export class HoldMyTask extends EventEmitter<[never]> {
|
|
|
127
335
|
*/
|
|
128
336
|
clear(): void;
|
|
129
337
|
/**
|
|
130
|
-
* Returns the
|
|
131
|
-
* @returns {number}
|
|
338
|
+
* Returns the number of tasks in the queue.
|
|
339
|
+
* @returns {number} Number of tasks in the queue
|
|
132
340
|
* @example
|
|
133
341
|
* const totalTasks = queue.size(); // 5
|
|
134
342
|
*/
|
|
135
343
|
size(): number;
|
|
344
|
+
/**
|
|
345
|
+
* Returns the number of tasks in the queue (alias for size()).
|
|
346
|
+
* @returns {number} Number of tasks in the queue
|
|
347
|
+
* @example
|
|
348
|
+
* const totalTasks = queue.length(); // 5
|
|
349
|
+
*/
|
|
350
|
+
length(): number;
|
|
136
351
|
/**
|
|
137
352
|
* Returns the number of currently running tasks.
|
|
138
353
|
* @returns {number} Number of running tasks
|
|
@@ -160,9 +375,9 @@ export class HoldMyTask extends EventEmitter<[never]> {
|
|
|
160
375
|
* Schedules the next scheduler tick based on when tasks become ready.
|
|
161
376
|
* @returns {void}
|
|
162
377
|
* @private
|
|
378
|
+
* @internal
|
|
163
379
|
*/
|
|
164
|
-
private
|
|
165
|
-
intervalId: NodeJS.Timeout;
|
|
380
|
+
private _scheduleNextTick;
|
|
166
381
|
timeoutId: NodeJS.Timeout;
|
|
167
382
|
/**
|
|
168
383
|
* Main scheduler tick that moves ready tasks and starts execution.
|
|
@@ -170,19 +385,245 @@ export class HoldMyTask extends EventEmitter<[never]> {
|
|
|
170
385
|
* @private
|
|
171
386
|
*/
|
|
172
387
|
private schedulerTick;
|
|
388
|
+
/**
|
|
389
|
+
* Handles an expired task by removing it and calling appropriate error handling.
|
|
390
|
+
* @param {Object} item - The expired task item
|
|
391
|
+
* @returns {void}
|
|
392
|
+
* @private
|
|
393
|
+
* @internal
|
|
394
|
+
*/
|
|
395
|
+
private _expireTask;
|
|
173
396
|
/**
|
|
174
397
|
* Starts execution of a ready task.
|
|
175
398
|
* @param {Object} item - The task item to execute
|
|
176
399
|
* @returns {Promise<void>}
|
|
177
400
|
* @private
|
|
401
|
+
* @internal
|
|
178
402
|
*/
|
|
179
|
-
private
|
|
403
|
+
private _startTask;
|
|
180
404
|
/**
|
|
181
405
|
* Clears all active timers (intervals and timeouts).
|
|
182
406
|
* @returns {void}
|
|
183
407
|
* @private
|
|
184
408
|
*/
|
|
185
409
|
private clearTimers;
|
|
410
|
+
/**
|
|
411
|
+
* Calculates when the next scheduler run should happen and sets appropriate timeout.
|
|
412
|
+
* @private
|
|
413
|
+
* @returns {void}
|
|
414
|
+
*
|
|
415
|
+
* @description
|
|
416
|
+
* Smart scheduling that calculates the optimal time for the next scheduler run based on:
|
|
417
|
+
* - When the next pending task becomes ready
|
|
418
|
+
* - When delay periods end
|
|
419
|
+
* - Whether there are tasks that can run immediately
|
|
420
|
+
*
|
|
421
|
+
* Uses setTimeout for precise timing instead of constant polling intervals.
|
|
422
|
+
*/
|
|
423
|
+
private scheduleSmartTimeout;
|
|
424
|
+
/**
|
|
425
|
+
* Runs the main scheduler logic and reschedules if needed.
|
|
426
|
+
* @private
|
|
427
|
+
* @returns {void}
|
|
428
|
+
*
|
|
429
|
+
* @description
|
|
430
|
+
* Executes the scheduler tick logic and then determines if more scheduling is needed.
|
|
431
|
+
* Tracks when scheduler last ran for healing mechanism.
|
|
432
|
+
*/
|
|
433
|
+
private runScheduler;
|
|
434
|
+
/**
|
|
435
|
+
* Starts the self-healing interval that ensures scheduler continues working.
|
|
436
|
+
* @private
|
|
437
|
+
* @returns {void}
|
|
438
|
+
*
|
|
439
|
+
* @description
|
|
440
|
+
* Healing mechanism that periodically checks if the scheduler should be running
|
|
441
|
+
* but isn't due to timeout failures or other issues. Runs every healingInterval milliseconds.
|
|
442
|
+
*/
|
|
443
|
+
private startHealingInterval;
|
|
444
|
+
/**
|
|
445
|
+
* Clears all scheduler-related timers.
|
|
446
|
+
* @private
|
|
447
|
+
* @returns {void}
|
|
448
|
+
*
|
|
449
|
+
* @description
|
|
450
|
+
* Cleans up both the main scheduler timeout and the healing interval timer.
|
|
451
|
+
*/
|
|
452
|
+
private clearSchedulerTimers;
|
|
453
|
+
/**
|
|
454
|
+
* Configure coalescing settings for specific keys dynamically.
|
|
455
|
+
* @param {string} coalescingKey - The coalescing key to configure
|
|
456
|
+
* @param {Object} config - Configuration for this key
|
|
457
|
+
* @param {number} [config.windowDuration] - Window duration in milliseconds for this key
|
|
458
|
+
* @param {number} [config.maxDelay] - Maximum delay in milliseconds for this key
|
|
459
|
+
* @param {number} [config.postDelay] - Post-completion delay in milliseconds for this key
|
|
460
|
+
* @param {number} [config.startDelay] - Pre-execution delay in milliseconds for this key
|
|
461
|
+
* @param {number} [config.delay] - DEPRECATED: Use postDelay instead
|
|
462
|
+
* @param {number} [config.start] - DEPRECATED: Use startDelay instead
|
|
463
|
+
* @param {boolean} [config.multipleCallbacks] - Whether to call multiple callbacks for this key
|
|
464
|
+
* @param {boolean} [config.resolveAllPromises] - Whether to resolve all promises for this key
|
|
465
|
+
* @returns {void}
|
|
466
|
+
*
|
|
467
|
+
* @example
|
|
468
|
+
* // Configure specific keys after queue creation
|
|
469
|
+
* queue.configureCoalescingKey('ui.update', {
|
|
470
|
+
* windowDuration: 100,
|
|
471
|
+
* maxDelay: 500,
|
|
472
|
+
* postDelay: 25,
|
|
473
|
+
* startDelay: 0
|
|
474
|
+
* });
|
|
475
|
+
*
|
|
476
|
+
* queue.configureCoalescingKey('api.batch', {
|
|
477
|
+
* windowDuration: 1000,
|
|
478
|
+
* maxDelay: 5000,
|
|
479
|
+
* postDelay: 100,
|
|
480
|
+
* startDelay: 200,
|
|
481
|
+
* resolveAllPromises: false
|
|
482
|
+
* });
|
|
483
|
+
*/
|
|
484
|
+
configureCoalescingKey(coalescingKey: string, config: {
|
|
485
|
+
windowDuration?: number;
|
|
486
|
+
maxDelay?: number;
|
|
487
|
+
postDelay?: number;
|
|
488
|
+
startDelay?: number;
|
|
489
|
+
delay?: number;
|
|
490
|
+
start?: number;
|
|
491
|
+
multipleCallbacks?: boolean;
|
|
492
|
+
resolveAllPromises?: boolean;
|
|
493
|
+
}): void;
|
|
494
|
+
/**
|
|
495
|
+
* Get the effective coalescing configuration for a specific key.
|
|
496
|
+
* @param {string} coalescingKey - The coalescing key to get configuration for
|
|
497
|
+
* @param {Object} [taskOptions={}] - Task-level options that may override key configuration
|
|
498
|
+
* @returns {Object} The effective configuration for this key
|
|
499
|
+
*
|
|
500
|
+
* @example
|
|
501
|
+
* // Get effective configuration for a key
|
|
502
|
+
* const config = queue.getCoalescingConfig('ui.update');
|
|
503
|
+
* console.log(`UI updates coalesce within ${config.windowDuration}ms with ${config.postDelay}ms post-completion delay`);
|
|
504
|
+
*
|
|
505
|
+
* // Check with task-level overrides
|
|
506
|
+
* const effectiveConfig = queue.getCoalescingConfig('ui.update', {
|
|
507
|
+
* coalescingWindowDuration: 50,
|
|
508
|
+
* delay: 30 // Still accepts old property names for backwards compatibility
|
|
509
|
+
* });
|
|
510
|
+
*/
|
|
511
|
+
getCoalescingConfig(coalescingKey: string, taskOptions?: any): any;
|
|
512
|
+
/**
|
|
513
|
+
* Get all configured coalescing keys and their configurations.
|
|
514
|
+
* @returns {Object} Map of coalescingKey to configuration
|
|
515
|
+
*
|
|
516
|
+
* @example
|
|
517
|
+
* // See all configured coalescing keys
|
|
518
|
+
* const allConfigs = queue.getCoalescingConfigurations();
|
|
519
|
+
* Object.entries(allConfigs).forEach(([key, config]) => {
|
|
520
|
+
* console.log(`${key}: ${config.windowDuration}ms window, ${config.maxDelay}ms max delay, ${config.postDelay}ms post-completion delay, ${config.startDelay}ms pre-execution delay`);
|
|
521
|
+
* });
|
|
522
|
+
*/
|
|
523
|
+
getCoalescingConfigurations(): any;
|
|
524
|
+
/**
|
|
525
|
+
* Configure default settings for specific priorities dynamically.
|
|
526
|
+
* @param {number} priority - The priority level to configure
|
|
527
|
+
* @param {Object} config - Configuration for this priority
|
|
528
|
+
* @param {number} [config.postDelay] - Default post-completion delay in milliseconds for this priority
|
|
529
|
+
* @param {number} [config.startDelay] - Default pre-execution delay in milliseconds for this priority
|
|
530
|
+
* @param {number} [config.delay] - DEPRECATED: Use postDelay instead
|
|
531
|
+
* @param {number} [config.start] - DEPRECATED: Use startDelay instead
|
|
532
|
+
* @returns {void}
|
|
533
|
+
*
|
|
534
|
+
* @example
|
|
535
|
+
* // Configure priority defaults after queue creation
|
|
536
|
+
* queue.configurePriority(1, {
|
|
537
|
+
* postDelay: 100, // High priority tasks have 100ms delay after completion
|
|
538
|
+
* startDelay: 0 // High priority tasks start immediately
|
|
539
|
+
* });
|
|
540
|
+
*
|
|
541
|
+
* queue.configurePriority(3, {
|
|
542
|
+
* postDelay: 0, // Low priority tasks have no delay after completion
|
|
543
|
+
* startDelay: 200 // Low priority tasks wait 200ms before starting
|
|
544
|
+
* });
|
|
545
|
+
*/
|
|
546
|
+
configurePriority(priority: number, config: {
|
|
547
|
+
postDelay?: number;
|
|
548
|
+
startDelay?: number;
|
|
549
|
+
delay?: number;
|
|
550
|
+
start?: number;
|
|
551
|
+
}): void;
|
|
552
|
+
/**
|
|
553
|
+
* Get the effective configuration for a specific priority.
|
|
554
|
+
* @param {number} priority - The priority level to get configuration for
|
|
555
|
+
* @param {Object} [taskOptions={}] - Task-level options that may override priority configuration
|
|
556
|
+
* @returns {Object} The effective configuration for this priority
|
|
557
|
+
*
|
|
558
|
+
* @example
|
|
559
|
+
* // Get effective configuration for a priority
|
|
560
|
+
* const config = queue.getPriorityConfig(1);
|
|
561
|
+
* console.log(`Priority 1 tasks: ${config.delay}ms delay, ${config.start}ms start delay`);
|
|
562
|
+
*
|
|
563
|
+
* // Check with task-level overrides
|
|
564
|
+
* const effectiveConfig = queue.getPriorityConfig(1, {
|
|
565
|
+
* delay: 50,
|
|
566
|
+
* start: 10
|
|
567
|
+
* });
|
|
568
|
+
*/
|
|
569
|
+
getPriorityConfig(priority: number, taskOptions?: any): any;
|
|
570
|
+
/**
|
|
571
|
+
* Get all configured priorities and their configurations.
|
|
572
|
+
* @returns {Object} Map of priority to configuration
|
|
573
|
+
*
|
|
574
|
+
* @example
|
|
575
|
+
* // See all configured priorities
|
|
576
|
+
* const allConfigs = queue.getPriorityConfigurations();
|
|
577
|
+
* Object.entries(allConfigs).forEach(([priority, config]) => {
|
|
578
|
+
* console.log(`Priority ${priority}: ${config.delay}ms delay, ${config.start}ms start delay`);
|
|
579
|
+
* });
|
|
580
|
+
*/
|
|
581
|
+
getPriorityConfigurations(): any;
|
|
582
|
+
/**
|
|
583
|
+
* Alias for destroy() method for common queue system naming.
|
|
584
|
+
* @returns {void}
|
|
585
|
+
*/
|
|
586
|
+
shutdown(): void;
|
|
587
|
+
/**
|
|
588
|
+
* Alias for enqueue() method for common queue system naming.
|
|
589
|
+
* @param {Function} task - The task function to execute
|
|
590
|
+
* @param {Function|Object} optionsOrCallback - Callback function or options object
|
|
591
|
+
* @param {Object} options - Task options (if callback provided as second parameter)
|
|
592
|
+
* @returns {Promise|TaskHandle} Promise if no callback provided, TaskHandle otherwise
|
|
593
|
+
*/
|
|
594
|
+
schedule(task: Function, optionsOrCallback: Function | any, options?: any): Promise<any> | TaskHandle;
|
|
595
|
+
/**
|
|
596
|
+
* Alias for enqueue() method for common queue system naming.
|
|
597
|
+
* @param {Function} task - The task function to execute
|
|
598
|
+
* @param {Function|Object} optionsOrCallback - Callback function or options object
|
|
599
|
+
* @param {Object} options - Task options (if callback provided as second parameter)
|
|
600
|
+
* @returns {Promise|TaskHandle} Promise if no callback provided, TaskHandle otherwise
|
|
601
|
+
*/
|
|
602
|
+
add(task: Function, optionsOrCallback: Function | any, options?: any): Promise<any> | TaskHandle;
|
|
603
|
+
/**
|
|
604
|
+
* Find a task by its ID.
|
|
605
|
+
* @param {string|number} id - The task ID to find
|
|
606
|
+
* @returns {Object|null} Task object if found, null otherwise
|
|
607
|
+
*/
|
|
608
|
+
get(id: string | number): any | null;
|
|
609
|
+
/**
|
|
610
|
+
* Check if a task with the given ID exists.
|
|
611
|
+
* @param {string|number} id - The task ID to check
|
|
612
|
+
* @returns {boolean} True if task exists, false otherwise
|
|
613
|
+
*/
|
|
614
|
+
has(id: string | number): boolean;
|
|
615
|
+
/**
|
|
616
|
+
* Alias for get() method for backward compatibility.
|
|
617
|
+
* @param {string|number} id - The task ID to find
|
|
618
|
+
* @returns {Object|null} Task object if found, null otherwise
|
|
619
|
+
*/
|
|
620
|
+
getTask(id: string | number): any | null;
|
|
621
|
+
/**
|
|
622
|
+
* Alias for has() method for backward compatibility.
|
|
623
|
+
* @param {string|number} id - The task ID to check
|
|
624
|
+
* @returns {boolean} True if task exists, false otherwise
|
|
625
|
+
*/
|
|
626
|
+
hasTask(id: string | number): boolean;
|
|
186
627
|
}
|
|
187
628
|
import { EventEmitter } from "events";
|
|
188
629
|
import { MinHeap } from "./utils.mjs";
|
|
@@ -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":"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"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export * from "@cldmv/holdmytask/main";
|
|
2
|
+
export { HoldMyTask as default, HoldMyTask as queue, HoldMyTask as Queue, HoldMyTask as TaskManager, HoldMyTask as TaskQueue, HoldMyTask as QueueManager, HoldMyTask as TaskProcessor } from "@cldmv/holdmytask/main";
|
|
3
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../index.mjs"],"names":[],"mappings":""}
|