Skip to content

Commit 62fa7f8

Browse files
more old stuff.
1 parent d24ec5c commit 62fa7f8

File tree

3 files changed

+260
-0
lines changed

3 files changed

+260
-0
lines changed

src/old/old/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 🗂️ Welcome to `old/old/` – The Backup's Backup 📦📦
2+
3+
Yes... you read that right. This is the `old` folder **inside another `old` folder** 😅
4+
We’re officially in **archive inception** 🌀
5+
6+
This folder contains files so outdated 🧓, so forgotten 🕸️, they’ve been double-wrapped for safety.
7+
They're **not used**, **not maintained**, and **absolutely not pretty** 💀
8+
9+
🍮 **Tiny pudding-quality code alert!**
10+
This is peak legacy chaos, preserved only for historical curiosity or emotional damage control.
11+
12+
> "Why is it still here?"
13+
> We don’t know either. But deleting it feels... dangerous. 😬
14+
15+
So yeah — look, but don’t touch. And definitely don’t judge us 😶❤️

src/old/old/TimedMap.js

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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;

src/old/old/examples/TimedMap.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import TimedMap from '../TimedMap';
2+
3+
// Example usage:
4+
const myTimedMap = new TimedMap();
5+
6+
// Listener for modification events
7+
const modifyListener = (event) => {
8+
console.log(
9+
`Modify event: ${event.action} on key "${event.key}" at ${new Date(event.timestamp)}`,
10+
);
11+
};
12+
myTimedMap.onModify(modifyListener);
13+
14+
// Listener for queue updates
15+
const queueUpdateListener = (queueSize) => {
16+
console.log(`Queue updated! Current size: ${queueSize}`);
17+
};
18+
myTimedMap.onQueueUpdate(queueUpdateListener);
19+
20+
// Listener for event processing
21+
const processEventListener = (event) => {
22+
console.log(
23+
`Processing event: ${event.action} on key "${event.key}" at ${new Date(event.timestamp)}`,
24+
);
25+
};
26+
myTimedMap.onProcessEvent(processEventListener);
27+
28+
// Adding values
29+
myTimedMap.set('item1', { name: 'Example Item' });
30+
myTimedMap.set('item2', { name: 'Another Item' });
31+
32+
// Removing specific listener
33+
myTimedMap.offModify(modifyListener);
34+
myTimedMap.set('item3', { name: 'Should not trigger modifyListener' });
35+
36+
// Removing all listeners from 'queueUpdated'
37+
myTimedMap.removeAllListeners('queueUpdated');
38+
myTimedMap.set('item4', { name: 'Queue update should not trigger' });
39+
40+
// Processing events
41+
myTimedMap._processNextEvent();

0 commit comments

Comments
 (0)