@@ -50,13 +50,46 @@ class TinyOlmInstance {
5050 */
5151 #events = new EventEmitter ( ) ;
5252
53+ /**
54+ * Important instance used to make system event emitter.
55+ * @type {EventEmitter }
56+ */
57+ #sysEvents = new EventEmitter ( ) ;
58+ #sysEventsUsed = false ;
59+
5360 /**
5461 * Emits an event with optional arguments to all system emit.
5562 * @param {string | symbol } event - The name of the event to emit.
5663 * @param {...any } args - Arguments passed to event listeners.
5764 */
5865 #emit( event , ...args ) {
5966 this . #events. emit ( event , ...args ) ;
67+ if ( this . #sysEventsUsed) this . #sysEvents. emit ( event , ...args ) ;
68+ }
69+
70+ /**
71+ * Provides access to a secure internal EventEmitter for subclass use only.
72+ *
73+ * This method exposes a dedicated EventEmitter instance intended specifically for subclasses
74+ * that extend the main class. It prevents subclasses from accidentally or intentionally using
75+ * the primary class's public event system (`emit`), which could lead to unpredictable behavior
76+ * or interference in the base class's event flow.
77+ *
78+ * For security and consistency, this method is designed to be accessed only once.
79+ * Multiple accesses are blocked to avoid leaks or misuse of the internal event bus.
80+ *
81+ * @returns {EventEmitter } A special internal EventEmitter instance for subclass use.
82+ * @throws {Error } If the method is called more than once.
83+ */
84+ getSysEvents ( ) {
85+ if ( this . #sysEventsUsed)
86+ throw new Error (
87+ 'Access denied: getSysEvents() can only be called once. ' +
88+ 'This restriction ensures subclass event isolation and prevents accidental interference ' +
89+ 'with the main class event emitter.' ,
90+ ) ;
91+ this . #sysEventsUsed = true ;
92+ return this . #sysEvents;
6093 }
6194
6295 /**
@@ -138,6 +171,86 @@ class TinyOlmInstance {
138171 return this ;
139172 }
140173
174+ /**
175+ * Removes all listeners for a specific event, or all events if no event is specified.
176+ * @param {string | symbol } [event] - The name of the event. If omitted, all listeners from all events will be removed.
177+ * @returns {this } The current class instance (for chaining).
178+ */
179+ removeAllListeners ( event ) {
180+ this . #events. removeAllListeners ( event ) ;
181+ return this ;
182+ }
183+
184+ /**
185+ * Returns the number of times the given `listener` is registered for the specified `event`.
186+ * If no `listener` is passed, returns how many listeners are registered for the `event`.
187+ * @param {string | symbol } eventName - The name of the event.
188+ * @param {Function } [listener] - Optional listener function to count.
189+ * @returns {number } Number of matching listeners.
190+ */
191+ listenerCount ( eventName , listener ) {
192+ return this . #events. listenerCount ( eventName , listener ) ;
193+ }
194+
195+ /**
196+ * Adds a listener function to the **beginning** of the listeners array for the specified event.
197+ * The listener is called every time the event is emitted.
198+ * @param {string | symbol } eventName - The event name.
199+ * @param {ListenerCallback } listener - The callback function.
200+ * @returns {this } The current class instance (for chaining).
201+ */
202+ prependListener ( eventName , listener ) {
203+ this . #events. prependListener ( eventName , listener ) ;
204+ return this ;
205+ }
206+
207+ /**
208+ * Adds a **one-time** listener function to the **beginning** of the listeners array.
209+ * The next time the event is triggered, this listener is removed and then invoked.
210+ * @param {string | symbol } eventName - The event name.
211+ * @param {ListenerCallback } listener - The callback function.
212+ * @returns {this } The current class instance (for chaining).
213+ */
214+ prependOnceListener ( eventName , listener ) {
215+ this . #events. prependOnceListener ( eventName , listener ) ;
216+ return this ;
217+ }
218+
219+ /**
220+ * Returns an array of event names for which listeners are currently registered.
221+ * @returns {(string | symbol)[] } Array of event names.
222+ */
223+ eventNames ( ) {
224+ return this . #events. eventNames ( ) ;
225+ }
226+
227+ /**
228+ * Gets the current maximum number of listeners allowed for any single event.
229+ * @returns {number } The max listener count.
230+ */
231+ getMaxListeners ( ) {
232+ return this . #events. getMaxListeners ( ) ;
233+ }
234+
235+ /**
236+ * Returns a copy of the listeners array for the specified event.
237+ * @param {string | symbol } eventName - The event name.
238+ * @returns {Function[] } An array of listener functions.
239+ */
240+ listeners ( eventName ) {
241+ return this . #events. listeners ( eventName ) ;
242+ }
243+
244+ /**
245+ * Returns a copy of the internal listeners array for the specified event,
246+ * including wrapper functions like those used by `.once()`.
247+ * @param {string | symbol } eventName - The event name.
248+ * @returns {Function[] } An array of raw listener functions.
249+ */
250+ rawListeners ( eventName ) {
251+ return this . #events. rawListeners ( eventName ) ;
252+ }
253+
141254 /**
142255 * Important instance used to validate values.
143256 * @type {TinyCryptoParser }
@@ -1730,6 +1843,23 @@ class TinyOlmInstance {
17301843 }
17311844 } ) ;
17321845 }
1846+
1847+ /**
1848+ * Destroys the instance by disposing internal resources and removing all event listeners.
1849+ *
1850+ * This method ensures a clean shutdown of the instance by first calling `dispose()`—which is expected
1851+ * to release external or internal resources (such as timers or memory references)—and then
1852+ * removes all listeners from both `#events` and `#sysEvents` to prevent memory leaks or unintended behavior.
1853+ *
1854+ * It should be called when the instance is no longer needed.
1855+ *
1856+ * @returns {Promise<void> } Resolves when cleanup is complete.
1857+ */
1858+ async destroy ( ) {
1859+ await this . dispose ( ) ;
1860+ this . #events. removeAllListeners ( ) ;
1861+ this . #sysEvents. removeAllListeners ( ) ;
1862+ }
17331863}
17341864
17351865export default TinyOlmInstance ;
0 commit comments