Comparing version 1.1.0 to 1.2.0
@@ -6,2 +6,3 @@ type ActualTask<Task> = Task extends object ? Readonly<Task> : Task; | ||
delayPromise: Promise<void> | null; | ||
retried: boolean; | ||
resolve: (result: TaskResult) => void; | ||
@@ -15,2 +16,3 @@ reject: (error: any) => void; | ||
onDrain: () => void; | ||
taskDelay: number; | ||
retryOnFailure: boolean; | ||
@@ -17,0 +19,0 @@ retryBeforeOtherTasks: boolean; |
@@ -25,2 +25,3 @@ "use strict"; | ||
onDrain: options.onDrain ?? (() => { }), | ||
taskDelay: options.taskDelay ?? 0, | ||
retryOnFailure: Boolean(options.retryOnFailure), | ||
@@ -43,3 +44,3 @@ retryBeforeOtherTasks: Boolean(options.retryBeforeOtherTasks), | ||
const key = this._options.key(incomingTask); | ||
const incomingItem = { task: incomingTask, key, delayPromise: sleep(0), resolve, reject }; | ||
const incomingItem = { task: incomingTask, key, delayPromise: sleep(0), retried: false, resolve, reject }; | ||
const existingKeyItemIndex = this._queue.findIndex((item) => item.key === key); | ||
@@ -68,4 +69,4 @@ const existingKeyItem = existingKeyItemIndex > -1 ? this._queue[existingKeyItemIndex] : null; | ||
this._running = true; | ||
// NOTE: We want to run the tasks asynchronusly, with a tiny delay, to allow synchronous | ||
// queuing (and possibly merging) of multiple tasks. | ||
// NOTE: We want to run the tasks asynchronusly to allow synchronous queuing | ||
// (and possibly merging) of multiple tasks. This needs to be at least 0ms. | ||
await sleep(0); | ||
@@ -82,2 +83,6 @@ if (this._paused) { | ||
} | ||
// NOTE: We do not want to wait for both the retry and task delays on retries. | ||
if (!currentItem.retried && this._options.taskDelay > 0) { | ||
await sleep(this._options.taskDelay); | ||
} | ||
try { | ||
@@ -123,3 +128,3 @@ await currentItem.delayPromise; | ||
_queueItemAsRetried(currentItem) { | ||
const retriedItem = { ...currentItem, delayPromise: sleep(this._options.retryDelay) }; | ||
const retriedItem = { ...currentItem, retried: true, delayPromise: sleep(this._options.retryDelay) }; | ||
if (this._options.retryBeforeOtherTasks) { | ||
@@ -155,2 +160,3 @@ const queuedKeyItemIndex = this._queue.findIndex((queuedItem) => queuedItem.key === currentItem.key); | ||
delayPromise: Promise.all([existingItem.delayPromise, incomingItem.delayPromise]).then(() => { }), | ||
retried: existingItem.retried, | ||
resolve: (result) => { | ||
@@ -157,0 +163,0 @@ existingItem.resolve(result); |
{ | ||
"name": "suprqueue", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Simple in-memory promise-based task queue with support for pausing, merging tasks, or retrying failed tasks", | ||
@@ -5,0 +5,0 @@ "main": "dist/Suprqueue.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
183144
3970