@cityssm/unique-timed-entry-queue
Advanced tools
+16
-9
@@ -25,9 +25,11 @@ export declare const eventTypes: readonly ["enqueue"]; | ||
| * Clears all entries from the queue. | ||
| * @returns The number of entries that were cleared. | ||
| */ | ||
| clear(): void; | ||
| clear(): number; | ||
| /** | ||
| * Clears all entries from the queue and all pending entries. | ||
| * This is the same as calling both `clearPending` and `clear`. | ||
| * @returns The total number of entries that were cleared. | ||
| */ | ||
| clearAll(): void; | ||
| clearAll(): number; | ||
| /** | ||
@@ -37,4 +39,5 @@ * Clears all pending entries. | ||
| * This is useful for stopping all pending enqueues, and should be called before destroying the queue. | ||
| * @returns The number of pending entries that were cleared. | ||
| */ | ||
| clearPending(): void; | ||
| clearPending(): number; | ||
| /** | ||
@@ -52,3 +55,5 @@ * Clears a specific pending entry. | ||
| /** | ||
| * Enqueues an entry after the specified delay. If the entry is already pending, the delay is reset. | ||
| * Enqueues an entry **after the specified delay**. | ||
| * If the entry is already pending, the delay is reset. | ||
| * If the entry is already in the queue, it will not be added again. | ||
| * @param entry - The entry to enqueue. | ||
@@ -59,3 +64,5 @@ * @param entryDelayMilliseconds - Optional delay in milliseconds for this specific entry. If not provided, the default delay is used. | ||
| /** | ||
| * Enqueues a list of entries after the specified delay. If an entry is already pending, the delay is reset. | ||
| * Enqueues a list of entries after the specified delay. | ||
| * If an entry is already pending, the delay is reset. | ||
| * If an entry is already in the queue, it will not be added again. | ||
| * @param entries - The entries to enqueue. | ||
@@ -71,3 +78,3 @@ * @param entryDelayMilliseconds - Optional delay in milliseconds for these specific entries. If not provided, the default delay is used. | ||
| /** | ||
| * Enqueues all pending entries immediately, bypassing the delay. | ||
| * Enqueues all pending entries, bypassing the delay. | ||
| */ | ||
@@ -77,3 +84,3 @@ enqueuePending(): void; | ||
| * Checks if there are pending entries. | ||
| * @returns True if there are pending entries, false otherwise. | ||
| * @returns `true` if there are pending entries, `false` otherwise. | ||
| */ | ||
@@ -84,3 +91,3 @@ hasPending(): boolean; | ||
| * @param entry - The entry to check. | ||
| * @returns True if the entry is pending, false otherwise. | ||
| * @returns `true` if the entry is pending, `false` otherwise. | ||
| */ | ||
@@ -90,3 +97,3 @@ hasPendingEntry(entry: T): boolean; | ||
| * Checks if the queue is empty. | ||
| * @returns True if the queue is empty, false otherwise. | ||
| * @returns `true` if the queue is empty, `false` otherwise. | ||
| */ | ||
@@ -93,0 +100,0 @@ isEmpty(): boolean; |
+33
-9
@@ -31,3 +31,3 @@ import Debug from 'debug'; | ||
| exitHook(() => { | ||
| debug('Process exiting, clearing pending entry timeouts.'); | ||
| debug(`Process exiting, clearing ${this.pendingEntries.size} pending entry timeouts.`); | ||
| this.clearPending(); | ||
@@ -50,5 +50,8 @@ }); | ||
| * Clears all entries from the queue. | ||
| * @returns The number of entries that were cleared. | ||
| */ | ||
| clear() { | ||
| const clearedCount = this.queue.length; | ||
| this.queue.length = 0; | ||
| return clearedCount; | ||
| } | ||
@@ -58,6 +61,8 @@ /** | ||
| * This is the same as calling both `clearPending` and `clear`. | ||
| * @returns The total number of entries that were cleared. | ||
| */ | ||
| clearAll() { | ||
| this.clearPending(); | ||
| this.clear(); | ||
| const clearedPending = this.clearPending(); | ||
| const clearedQueue = this.clear(); | ||
| return clearedPending + clearedQueue; | ||
| } | ||
@@ -68,2 +73,3 @@ /** | ||
| * This is useful for stopping all pending enqueues, and should be called before destroying the queue. | ||
| * @returns The number of pending entries that were cleared. | ||
| */ | ||
@@ -74,3 +80,5 @@ clearPending() { | ||
| } | ||
| const pendingCount = this.pendingEntries.size; | ||
| this.pendingEntries.clear(); | ||
| return pendingCount; | ||
| } | ||
@@ -100,3 +108,5 @@ /** | ||
| /** | ||
| * Enqueues an entry after the specified delay. If the entry is already pending, the delay is reset. | ||
| * Enqueues an entry **after the specified delay**. | ||
| * If the entry is already pending, the delay is reset. | ||
| * If the entry is already in the queue, it will not be added again. | ||
| * @param entry - The entry to enqueue. | ||
@@ -109,2 +119,6 @@ * @param entryDelayMilliseconds - Optional delay in milliseconds for this specific entry. If not provided, the default delay is used. | ||
| if (delay <= 0) { | ||
| if (this.queue.includes(entry)) { | ||
| debug(`Entry already in queue, not enqueuing: ${valueToString(entry)}`); | ||
| return; | ||
| } | ||
| this.queue.push(entry); | ||
@@ -118,2 +132,6 @@ this.triggerEvents('enqueue', entry); | ||
| this.pendingEntries.delete(stringEntry); | ||
| if (this.queue.includes(entry)) { | ||
| debug(`Entry already in queue, not enqueuing: ${stringEntry}`); | ||
| return; | ||
| } | ||
| this.queue.push(entry); | ||
@@ -126,3 +144,5 @@ this.triggerEvents('enqueue', entry); | ||
| /** | ||
| * Enqueues a list of entries after the specified delay. If an entry is already pending, the delay is reset. | ||
| * Enqueues a list of entries after the specified delay. | ||
| * If an entry is already pending, the delay is reset. | ||
| * If an entry is already in the queue, it will not be added again. | ||
| * @param entries - The entries to enqueue. | ||
@@ -144,3 +164,3 @@ * @param entryDelayMilliseconds - Optional delay in milliseconds for these specific entries. If not provided, the default delay is used. | ||
| /** | ||
| * Enqueues all pending entries immediately, bypassing the delay. | ||
| * Enqueues all pending entries, bypassing the delay. | ||
| */ | ||
@@ -153,2 +173,6 @@ enqueuePending() { | ||
| clearTimeout(pendingEntry.timeout); | ||
| if (this.queue.includes(pendingEntry.value)) { | ||
| debug(`Entry already in queue, not enqueuing pending entry: ${stringValue}`); | ||
| continue; | ||
| } | ||
| this.queue.push(pendingEntry.value); | ||
@@ -161,3 +185,3 @@ this.triggerEvents('enqueue', pendingEntry.value); | ||
| * Checks if there are pending entries. | ||
| * @returns True if there are pending entries, false otherwise. | ||
| * @returns `true` if there are pending entries, `false` otherwise. | ||
| */ | ||
@@ -170,3 +194,3 @@ hasPending() { | ||
| * @param entry - The entry to check. | ||
| * @returns True if the entry is pending, false otherwise. | ||
| * @returns `true` if the entry is pending, `false` otherwise. | ||
| */ | ||
@@ -179,3 +203,3 @@ hasPendingEntry(entry) { | ||
| * Checks if the queue is empty. | ||
| * @returns True if the queue is empty, false otherwise. | ||
| * @returns `true` if the queue is empty, `false` otherwise. | ||
| */ | ||
@@ -182,0 +206,0 @@ isEmpty() { |
+51
-13
@@ -57,3 +57,5 @@ import Debug from 'debug' | ||
| exitHook(() => { | ||
| debug('Process exiting, clearing pending entry timeouts.') | ||
| debug( | ||
| `Process exiting, clearing ${this.pendingEntries.size} pending entry timeouts.` | ||
| ) | ||
| this.clearPending() | ||
@@ -69,3 +71,6 @@ }) | ||
| */ | ||
| public addEventListener(eventType: EventType, listener: (entry: T) => void): string { | ||
| public addEventListener( | ||
| eventType: EventType, | ||
| listener: (entry: T) => void | ||
| ): string { | ||
| const listenerId = generateUniqueListenerId() | ||
@@ -81,5 +86,8 @@ | ||
| * Clears all entries from the queue. | ||
| * @returns The number of entries that were cleared. | ||
| */ | ||
| public clear(): void { | ||
| public clear(): number { | ||
| const clearedCount = this.queue.length | ||
| this.queue.length = 0 | ||
| return clearedCount | ||
| } | ||
@@ -90,6 +98,9 @@ | ||
| * This is the same as calling both `clearPending` and `clear`. | ||
| * @returns The total number of entries that were cleared. | ||
| */ | ||
| public clearAll(): void { | ||
| this.clearPending() | ||
| this.clear() | ||
| public clearAll(): number { | ||
| const clearedPending = this.clearPending() | ||
| const clearedQueue = this.clear() | ||
| return clearedPending + clearedQueue | ||
| } | ||
@@ -101,4 +112,5 @@ | ||
| * This is useful for stopping all pending enqueues, and should be called before destroying the queue. | ||
| * @returns The number of pending entries that were cleared. | ||
| */ | ||
| public clearPending(): void { | ||
| public clearPending(): number { | ||
| for (const timeout of this.pendingEntries.values()) { | ||
@@ -108,3 +120,7 @@ clearTimeout(timeout.timeout) | ||
| const pendingCount = this.pendingEntries.size | ||
| this.pendingEntries.clear() | ||
| return pendingCount | ||
| } | ||
@@ -139,3 +155,5 @@ | ||
| /** | ||
| * Enqueues an entry after the specified delay. If the entry is already pending, the delay is reset. | ||
| * Enqueues an entry **after the specified delay**. | ||
| * If the entry is already pending, the delay is reset. | ||
| * If the entry is already in the queue, it will not be added again. | ||
| * @param entry - The entry to enqueue. | ||
@@ -150,2 +168,7 @@ * @param entryDelayMilliseconds - Optional delay in milliseconds for this specific entry. If not provided, the default delay is used. | ||
| if (delay <= 0) { | ||
| if (this.queue.includes(entry)) { | ||
| debug(`Entry already in queue, not enqueuing: ${valueToString(entry)}`) | ||
| return | ||
| } | ||
| this.queue.push(entry) | ||
@@ -163,2 +186,7 @@ this.triggerEvents('enqueue', entry) | ||
| if (this.queue.includes(entry)) { | ||
| debug(`Entry already in queue, not enqueuing: ${stringEntry}`) | ||
| return | ||
| } | ||
| this.queue.push(entry) | ||
@@ -174,3 +202,5 @@ this.triggerEvents('enqueue', entry) | ||
| /** | ||
| * Enqueues a list of entries after the specified delay. If an entry is already pending, the delay is reset. | ||
| * Enqueues a list of entries after the specified delay. | ||
| * If an entry is already pending, the delay is reset. | ||
| * If an entry is already in the queue, it will not be added again. | ||
| * @param entries - The entries to enqueue. | ||
@@ -194,3 +224,3 @@ * @param entryDelayMilliseconds - Optional delay in milliseconds for these specific entries. If not provided, the default delay is used. | ||
| /** | ||
| * Enqueues all pending entries immediately, bypassing the delay. | ||
| * Enqueues all pending entries, bypassing the delay. | ||
| */ | ||
@@ -208,2 +238,10 @@ public enqueuePending(): void { | ||
| clearTimeout(pendingEntry.timeout) | ||
| if (this.queue.includes(pendingEntry.value)) { | ||
| debug( | ||
| `Entry already in queue, not enqueuing pending entry: ${stringValue}` | ||
| ) | ||
| continue | ||
| } | ||
| this.queue.push(pendingEntry.value) | ||
@@ -218,3 +256,3 @@ this.triggerEvents('enqueue', pendingEntry.value) | ||
| * Checks if there are pending entries. | ||
| * @returns True if there are pending entries, false otherwise. | ||
| * @returns `true` if there are pending entries, `false` otherwise. | ||
| */ | ||
@@ -228,3 +266,3 @@ public hasPending(): boolean { | ||
| * @param entry - The entry to check. | ||
| * @returns True if the entry is pending, false otherwise. | ||
| * @returns `true` if the entry is pending, `false` otherwise. | ||
| */ | ||
@@ -238,3 +276,3 @@ public hasPendingEntry(entry: T): boolean { | ||
| * Checks if the queue is empty. | ||
| * @returns True if the queue is empty, false otherwise. | ||
| * @returns `true` if the queue is empty, `false` otherwise. | ||
| */ | ||
@@ -241,0 +279,0 @@ public isEmpty(): boolean { |
+2
-2
| { | ||
| "name": "@cityssm/unique-timed-entry-queue", | ||
| "version": "0.3.0", | ||
| "version": "0.4.0", | ||
| "description": "A queue with delayed enqueue of unique entries, perfect for queuing update notifications.", | ||
@@ -34,3 +34,3 @@ "keywords": [ | ||
| "@types/debug": "^4.1.12", | ||
| "@types/node": "^25.0.9", | ||
| "@types/node": "^25.0.10", | ||
| "eslint-config-cityssm": "^36.1.0", | ||
@@ -37,0 +37,0 @@ "prettier-config-cityssm": "^1.0.0" |
+14
-6
@@ -12,2 +12,5 @@ # Unique, Timed-Entry Queue for Node | ||
| 💡 **For best results, use numbers or strings for queue entries** | ||
| as uniqueness is easier to determine. | ||
| ## Installation | ||
@@ -51,4 +54,4 @@ | ||
| The enqueue delay can optionally be overridden for the specific entry. | ||
| If the entry is still waiting to be added to the queue, | ||
| the delay will be reset. | ||
| If the entry is still waiting to be added to the queue, the delay will be reset. | ||
| If the entry exists in the queue, it is discarded. | ||
@@ -64,6 +67,6 @@ `enqueuePending()`<br /> | ||
| `size()`<br /> | ||
| Returns the size of the queue. | ||
| Returns the size of the queue, not including any pending entries. | ||
| `pendingSize()`<br /> | ||
| Returns the number of entries waiting to be added to the queue. | ||
| Returns the number of pending entries waiting to be added to the queue. | ||
@@ -83,11 +86,15 @@ `isEmpty()`<br /> | ||
| Clears all queue entries. | ||
| Returns the number of cleared entries. | ||
| `clearPending()`<br /> | ||
| Clears all pending queue entries. | ||
| Returns the number of cleared pending entries. | ||
| `clearPendingEntry(entry)`<br /> | ||
| Clears a specific pending entry. | ||
| Returns `true` if the pending entry was found and cleared. | ||
| `clearAll()`<br /> | ||
| Clears all queue entries, and all pending entries. | ||
| Returns the total number of cleared entries. | ||
@@ -125,3 +132,4 @@ ### Event Listeners | ||
| [**ShiftLog**](https://github.com/cityssm/shiftlog/)<br /> | ||
| A work management system with work order recording, shift activity logging, and timesheet tracking. | ||
| Uses a `UniqueTimedEntryQueue` to dispatch notifications when work orders have been updated. | ||
| A work management system with work order recording, shift activity logging, | ||
| and timesheet tracking. Uses a `UniqueTimedEntryQueue` to dispatch notifications | ||
| when work orders have been updated. |
40296
8.15%736
9.2%131
6.5%