Skip to content

Commit 7c7ed2f

Browse files
EventEmitter
1 parent 67116cd commit 7c7ed2f

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

src/TinySQL.mjs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,38 @@ class PuddySqlInstance {
3636
*/
3737
#events = new EventEmitter();
3838

39+
/**
40+
* Important instance used to make system event emitter.
41+
* @type {EventEmitter}
42+
*/
43+
#sysEvents = new EventEmitter();
44+
#sysEventsUsed = false;
45+
46+
/**
47+
* Provides access to a secure internal EventEmitter for subclass use only.
48+
*
49+
* This method exposes a dedicated EventEmitter instance intended specifically for subclasses
50+
* that extend the main class. It prevents subclasses from accidentally or intentionally using
51+
* the primary class's public event system (`emit`), which could lead to unpredictable behavior
52+
* or interference in the base class's event flow.
53+
*
54+
* For security and consistency, this method is designed to be accessed only once.
55+
* Multiple accesses are blocked to avoid leaks or misuse of the internal event bus.
56+
*
57+
* @returns {EventEmitter} A special internal EventEmitter instance for subclass use.
58+
* @throws {Error} If the method is called more than once.
59+
*/
60+
getSysEvents() {
61+
if (this.#sysEventsUsed)
62+
throw new Error(
63+
'Access denied: getSysEvents() can only be called once. ' +
64+
'This restriction ensures subclass event isolation and prevents accidental interference ' +
65+
'with the main class event emitter.',
66+
);
67+
this.#sysEventsUsed = true;
68+
return this.#sysEvents;
69+
}
70+
3971
/**
4072
* Enables or disables console color output for debug messages.
4173
*
@@ -224,6 +256,86 @@ class PuddySqlInstance {
224256
return this;
225257
}
226258

259+
/**
260+
* Removes all listeners for a specific event, or all events if no event is specified.
261+
* @param {string | symbol} [event] - The name of the event. If omitted, all listeners from all events will be removed.
262+
* @returns {this} The current class instance (for chaining).
263+
*/
264+
removeAllListeners(event) {
265+
this.#events.removeAllListeners(event);
266+
return this;
267+
}
268+
269+
/**
270+
* Returns the number of times the given `listener` is registered for the specified `event`.
271+
* If no `listener` is passed, returns how many listeners are registered for the `event`.
272+
* @param {string | symbol} eventName - The name of the event.
273+
* @param {Function} [listener] - Optional listener function to count.
274+
* @returns {number} Number of matching listeners.
275+
*/
276+
listenerCount(eventName, listener) {
277+
return this.#events.listenerCount(eventName, listener);
278+
}
279+
280+
/**
281+
* Adds a listener function to the **beginning** of the listeners array for the specified event.
282+
* The listener is called every time the event is emitted.
283+
* @param {string | symbol} eventName - The event name.
284+
* @param {ListenerCallback} listener - The callback function.
285+
* @returns {this} The current class instance (for chaining).
286+
*/
287+
prependListener(eventName, listener) {
288+
this.#events.prependListener(eventName, listener);
289+
return this;
290+
}
291+
292+
/**
293+
* Adds a **one-time** listener function to the **beginning** of the listeners array.
294+
* The next time the event is triggered, this listener is removed and then invoked.
295+
* @param {string | symbol} eventName - The event name.
296+
* @param {ListenerCallback} listener - The callback function.
297+
* @returns {this} The current class instance (for chaining).
298+
*/
299+
prependOnceListener(eventName, listener) {
300+
this.#events.prependOnceListener(eventName, listener);
301+
return this;
302+
}
303+
304+
/**
305+
* Returns an array of event names for which listeners are currently registered.
306+
* @returns {(string | symbol)[]} Array of event names.
307+
*/
308+
eventNames() {
309+
return this.#events.eventNames();
310+
}
311+
312+
/**
313+
* Gets the current maximum number of listeners allowed for any single event.
314+
* @returns {number} The max listener count.
315+
*/
316+
getMaxListeners() {
317+
return this.#events.getMaxListeners();
318+
}
319+
320+
/**
321+
* Returns a copy of the listeners array for the specified event.
322+
* @param {string | symbol} eventName - The event name.
323+
* @returns {Function[]} An array of listener functions.
324+
*/
325+
listeners(eventName) {
326+
return this.#events.listeners(eventName);
327+
}
328+
329+
/**
330+
* Returns a copy of the internal listeners array for the specified event,
331+
* including wrapper functions like those used by `.once()`.
332+
* @param {string | symbol} eventName - The event name.
333+
* @returns {Function[]} An array of raw listener functions.
334+
*/
335+
rawListeners(eventName) {
336+
return this.#events.rawListeners(eventName);
337+
}
338+
227339
/**
228340
* Checks if the given error message indicates a connection error based on the SQL engine in use.
229341
*

0 commit comments

Comments
 (0)