@cityssm/unique-timed-entry-queue
Advanced tools
+14
-0
@@ -57,2 +57,6 @@ /** | ||
| /** | ||
| * Enqueues all pending entries immediately, bypassing the delay. | ||
| */ | ||
| enqueuePending(): void; | ||
| /** | ||
| * Checks if there are pending entries. | ||
@@ -79,2 +83,7 @@ * @returns True if there are pending entries, false otherwise. | ||
| /** | ||
| * Converts the pending entries to an array. | ||
| * @returns An array containing the pending entries. | ||
| */ | ||
| pendingToArray(): T[]; | ||
| /** | ||
| * Gets the size of the queue. | ||
@@ -84,2 +93,7 @@ * @returns The number of entries in the queue. | ||
| size(): number; | ||
| /** | ||
| * Converts the queue to an array. | ||
| * @returns An array containing the entries in the queue. | ||
| */ | ||
| toArray(): T[]; | ||
| } |
+34
-3
@@ -51,3 +51,3 @@ import Debug from 'debug'; | ||
| for (const timeout of this.pendingEntries.values()) { | ||
| clearTimeout(timeout); | ||
| clearTimeout(timeout.timeout); | ||
| } | ||
@@ -65,3 +65,3 @@ this.pendingEntries.clear(); | ||
| debug(`Clearing pending entry timeout: ${stringEntry}`); | ||
| clearTimeout(this.pendingEntries.get(stringEntry)); | ||
| clearTimeout(this.pendingEntries.get(stringEntry)?.timeout); | ||
| this.pendingEntries.delete(stringEntry); | ||
@@ -98,3 +98,3 @@ return true; | ||
| }, delay); | ||
| this.pendingEntries.set(stringEntry, timeout); | ||
| this.pendingEntries.set(stringEntry, { timeout, value: entry }); | ||
| } | ||
@@ -119,2 +119,15 @@ /** | ||
| /** | ||
| * Enqueues all pending entries immediately, bypassing the delay. | ||
| */ | ||
| enqueuePending() { | ||
| for (const stringValue of this.pendingEntries.keys()) { | ||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion | ||
| const pendingEntry = this.pendingEntries.get(stringValue); | ||
| this.pendingEntries.delete(stringValue); | ||
| clearTimeout(pendingEntry.timeout); | ||
| this.queue.push(pendingEntry.value); | ||
| debug(`Enqueued pending entry immediately: ${stringValue}`); | ||
| } | ||
| } | ||
| /** | ||
| * Checks if there are pending entries. | ||
@@ -150,2 +163,13 @@ * @returns True if there are pending entries, false otherwise. | ||
| /** | ||
| * Converts the pending entries to an array. | ||
| * @returns An array containing the pending entries. | ||
| */ | ||
| pendingToArray() { | ||
| const pendingValues = []; | ||
| for (const pendingEntry of this.pendingEntries.values()) { | ||
| pendingValues.push(pendingEntry.value); | ||
| } | ||
| return pendingValues; | ||
| } | ||
| /** | ||
| * Gets the size of the queue. | ||
@@ -157,2 +181,9 @@ * @returns The number of entries in the queue. | ||
| } | ||
| /** | ||
| * Converts the queue to an array. | ||
| * @returns An array containing the entries in the queue. | ||
| */ | ||
| toArray() { | ||
| return [...this.queue]; | ||
| } | ||
| } |
+52
-4
@@ -14,3 +14,9 @@ import Debug from 'debug' | ||
| private readonly enqueueDelayMilliseconds: number | ||
| private readonly pendingEntries: Map<string, NodeJS.Timeout> | ||
| private readonly pendingEntries: Map< | ||
| string, | ||
| { | ||
| timeout: NodeJS.Timeout | ||
| value: T | ||
| } | ||
| > | ||
| private readonly queue: T[] | ||
@@ -65,3 +71,3 @@ | ||
| for (const timeout of this.pendingEntries.values()) { | ||
| clearTimeout(timeout) | ||
| clearTimeout(timeout.timeout) | ||
| } | ||
@@ -82,3 +88,3 @@ | ||
| debug(`Clearing pending entry timeout: ${stringEntry}`) | ||
| clearTimeout(this.pendingEntries.get(stringEntry)) | ||
| clearTimeout(this.pendingEntries.get(stringEntry)?.timeout) | ||
| this.pendingEntries.delete(stringEntry) | ||
@@ -123,3 +129,3 @@ return true | ||
| this.pendingEntries.set(stringEntry, timeout) | ||
| this.pendingEntries.set(stringEntry, { timeout, value: entry }) | ||
| } | ||
@@ -147,2 +153,22 @@ | ||
| /** | ||
| * Enqueues all pending entries immediately, bypassing the delay. | ||
| */ | ||
| public enqueuePending(): void { | ||
| for (const stringValue of this.pendingEntries.keys()) { | ||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion | ||
| const pendingEntry = this.pendingEntries.get(stringValue) as { | ||
| timeout: NodeJS.Timeout | ||
| value: T | ||
| } | ||
| this.pendingEntries.delete(stringValue) | ||
| clearTimeout(pendingEntry.timeout) | ||
| this.queue.push(pendingEntry.value) | ||
| debug(`Enqueued pending entry immediately: ${stringValue}`) | ||
| } | ||
| } | ||
| /** | ||
| * Checks if there are pending entries. | ||
@@ -182,2 +208,16 @@ * @returns True if there are pending entries, false otherwise. | ||
| /** | ||
| * Converts the pending entries to an array. | ||
| * @returns An array containing the pending entries. | ||
| */ | ||
| public pendingToArray(): T[] { | ||
| const pendingValues: T[] = [] | ||
| for (const pendingEntry of this.pendingEntries.values()) { | ||
| pendingValues.push(pendingEntry.value) | ||
| } | ||
| return pendingValues | ||
| } | ||
| /** | ||
| * Gets the size of the queue. | ||
@@ -189,2 +229,10 @@ * @returns The number of entries in the queue. | ||
| } | ||
| /** | ||
| * Converts the queue to an array. | ||
| * @returns An array containing the entries in the queue. | ||
| */ | ||
| public toArray(): T[] { | ||
| return [...this.queue] | ||
| } | ||
| } |
+1
-1
| { | ||
| "name": "@cityssm/unique-timed-entry-queue", | ||
| "version": "0.1.0", | ||
| "version": "0.2.0", | ||
| "description": "A queue with delayed enqueue of unique entries, perfect for queuing update notifications.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
+12
-3
@@ -53,6 +53,9 @@ # Unique, Timed-Entry Queue for Node | ||
| `enqueuePending()`<br /> | ||
| Immediately add all pending entries to the queue. | ||
| `dequeue()`<br /> | ||
| Dequeues an entry from the queue. | ||
| ### Size Checks | ||
| ### Queue Checks | ||
@@ -68,4 +71,2 @@ `size()`<br /> | ||
| ### Pending Checks | ||
| `hasPending()`<br /> | ||
@@ -91,2 +92,10 @@ Returns `true` if there are pending entries. | ||
| ### Export Functions | ||
| `toArray()`<br /> | ||
| Exports all queue entries to an array. | ||
| `pendingToArray()`<br /> | ||
| Exports all pending entries to an array. | ||
| ## Note Regarding Shutdown | ||
@@ -93,0 +102,0 @@ |
31552
10.29%544
18.52%112
8.74%