|
| 1 | +import EventEmitter from 'events'; |
| 2 | + |
| 3 | +/** |
| 4 | + * A class that extends Map and emits events whenever an item is set or deleted. |
| 5 | + * It also maintains a queue of modification events to ensure sequential processing. |
| 6 | + */ |
| 7 | +class TimedMap extends Map { |
| 8 | + #eventQueue = []; |
| 9 | + #eventEmitter = new EventEmitter(); |
| 10 | + |
| 11 | + /** |
| 12 | + * Emits a modification event and adds it to the event queue. |
| 13 | + * This method is private and cannot be accessed externally. |
| 14 | + * @param {string} key - The key of the modified item. |
| 15 | + * @param {'set' | 'delete'} action - The type of modification performed. |
| 16 | + * @param {any} [value] - The new value of the item (if applicable). |
| 17 | + * @private |
| 18 | + */ |
| 19 | + #emitChangeEvent(key, action, value = undefined) { |
| 20 | + const timestamp = Date.now(); |
| 21 | + const event = { key, action, value, timestamp }; |
| 22 | + |
| 23 | + this.#eventEmitter.emit('modify', event); |
| 24 | + this._addToQueue(event); |
| 25 | + } |
| 26 | + |
| 27 | + constructor() { |
| 28 | + super(); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Adds an event to the end of the queue for sequential processing. |
| 33 | + * Emits an event indicating that the queue has new content. |
| 34 | + * @param {Object} event - The event object containing modification details. |
| 35 | + * @private |
| 36 | + */ |
| 37 | + _addToQueue(event) { |
| 38 | + this.#eventQueue.push(event); |
| 39 | + this.#eventEmitter.emit('queueUpdated', this.#eventQueue.length); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Adds an event back to the front of the queue. |
| 44 | + * Emits an event indicating that the queue has new content. |
| 45 | + * @param {Object} event - The event object to be reprocessed. |
| 46 | + * @private |
| 47 | + */ |
| 48 | + _addToQueueFront(event) { |
| 49 | + this.#eventQueue.unshift(event); |
| 50 | + this.#eventEmitter.emit('queueUpdated', this.#eventQueue.length); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Retrieves and removes the first event in the queue. |
| 55 | + * Emits an event notifying that an event should be processed. |
| 56 | + * Also emits 'queueUpdated' to indicate that the queue has changed. |
| 57 | + * @returns {Object | undefined} - The event object if available, otherwise undefined. |
| 58 | + */ |
| 59 | + _processNextEvent() { |
| 60 | + if (this.#eventQueue.length === 0) return undefined; |
| 61 | + |
| 62 | + const event = this.#eventQueue.shift(); |
| 63 | + this.#eventEmitter.emit('processEvent', event); |
| 64 | + this.#eventEmitter.emit('queueUpdated', this.#eventQueue.length); |
| 65 | + return event; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Sets a value in the Map and emits an event. |
| 70 | + * @param {string} key - The key of the item. |
| 71 | + * @param {any} value - The value to be stored. |
| 72 | + * @returns {TimedMap} - Returns the instance for method chaining. |
| 73 | + */ |
| 74 | + set(key, value) { |
| 75 | + super.set(key, value); |
| 76 | + this.#emitChangeEvent(key, 'set', value); |
| 77 | + return this; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Retrieves a value from the Map. |
| 82 | + * @param {string} key - The key of the item. |
| 83 | + * @returns {any} - The value associated with the key, or undefined if not found. |
| 84 | + */ |
| 85 | + get(key) { |
| 86 | + return super.get(key); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Deletes a value from the Map and emits an event. |
| 91 | + * @param {string} key - The key of the item to delete. |
| 92 | + * @returns {boolean} - Returns true if the key existed and was removed. |
| 93 | + */ |
| 94 | + delete(key) { |
| 95 | + const value = super.get(key); |
| 96 | + super.delete(key); |
| 97 | + this.#emitChangeEvent(key, 'delete', value); |
| 98 | + return true; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Get all values from the map. |
| 103 | + * @returns {Array} An array containing all values in the map. |
| 104 | + */ |
| 105 | + getAll() { |
| 106 | + return Array.from(super.values()); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Get the last `count` added values from the map. |
| 111 | + * @param {number} count - The number of last added values to retrieve. |
| 112 | + * @returns {Array} An array with the last `count` values. |
| 113 | + */ |
| 114 | + getAmount(count) { |
| 115 | + if (count <= 0) return []; |
| 116 | + const values = Array.from(super.values()); |
| 117 | + return values.slice(-count); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Returns a copy of the current event queue to avoid data corruption. |
| 122 | + * @returns {Object[]} - A new array containing the event queue. |
| 123 | + */ |
| 124 | + getEventQueue() { |
| 125 | + return [...this.#eventQueue]; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Retrieves an event at a specific index without removing it from the queue. |
| 130 | + * @param {number} index - The index of the event to retrieve. |
| 131 | + * @returns {Object | undefined} - The event object if found, otherwise undefined. |
| 132 | + */ |
| 133 | + peekEventAt(index) { |
| 134 | + return this.#eventQueue[index]; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Sorts the event queue using a custom comparison function. |
| 139 | + * @param {Function} compareFn - The comparison function to determine sorting order. |
| 140 | + */ |
| 141 | + sortEventQueue(compareFn) { |
| 142 | + this.#eventQueue.sort(compareFn); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Subscribes a callback function to listen for modification events. |
| 147 | + * @param {Function} callback - The function to execute when an event occurs. |
| 148 | + */ |
| 149 | + onModify(callback) { |
| 150 | + this.#eventEmitter.on('modify', callback); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Unsubscribes a callback function from modification events. |
| 155 | + * @param {Function} callback - The function to remove from the listener. |
| 156 | + */ |
| 157 | + offModify(callback) { |
| 158 | + this.#eventEmitter.off('modify', callback); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Subscribes a callback function to listen for queue updates. |
| 163 | + * @param {Function} callback - The function to execute when the queue is updated. |
| 164 | + */ |
| 165 | + onQueueUpdate(callback) { |
| 166 | + this.#eventEmitter.on('queueUpdated', callback); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Unsubscribes a callback function from queue updates. |
| 171 | + * @param {Function} callback - The function to remove from the listener. |
| 172 | + */ |
| 173 | + offQueueUpdate(callback) { |
| 174 | + this.#eventEmitter.off('queueUpdated', callback); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Subscribes a callback function to listen for event processing. |
| 179 | + * @param {Function} callback - The function to execute when an event is ready to be processed. |
| 180 | + */ |
| 181 | + onProcessEvent(callback) { |
| 182 | + this.#eventEmitter.on('processEvent', callback); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Unsubscribes a callback function from event processing. |
| 187 | + * @param {Function} callback - The function to remove from the listener. |
| 188 | + */ |
| 189 | + offProcessEvent(callback) { |
| 190 | + this.#eventEmitter.off('processEvent', callback); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Removes all listeners for 'modify', 'queueUpdated', or 'processEvent' events. |
| 195 | + * @param {'modify' | 'queueUpdated' | 'processEvent'} eventType - The event type to remove all listeners from. |
| 196 | + */ |
| 197 | + removeAllListeners(eventType) { |
| 198 | + if (['modify', 'queueUpdated', 'processEvent'].includes(eventType)) { |
| 199 | + this.#eventEmitter.removeAllListeners(eventType); |
| 200 | + } |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +export default TimedMap; |
0 commit comments