From 2d101b658428b26239bbec3bc538e5871019c649 Mon Sep 17 00:00:00 2001 From: owenpearson Date: Fri, 26 Sep 2025 10:46:46 +0100 Subject: [PATCH 1/2] refactor: allow multiple events in `EventEmitter.listeners` --- src/common/lib/util/eventemitter.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/common/lib/util/eventemitter.ts b/src/common/lib/util/eventemitter.ts index 4c7fef0c9b..d3067fb1b0 100644 --- a/src/common/lib/util/eventemitter.ts +++ b/src/common/lib/util/eventemitter.ts @@ -178,10 +178,21 @@ class EventEmitter { /** * Get the array of listeners for a given event; excludes once events - * @param event (optional) the name of the event, or none for 'any' + * @param event (optional) the name of the event, array of event names, or none for 'any' * @return array of events, or null if none */ - listeners(event: string) { + listeners(event?: string | string[]) { + if (Array.isArray(event)) { + const allListeners: Function[] = []; + event.forEach((eventName) => { + const listeners = this.listeners(eventName); + if (listeners) { + allListeners.push(...listeners); + } + }); + return allListeners.length ? allListeners : null; + } + if (event) { const listeners = this.events[event] || []; if (this.eventsOnce[event]) Array.prototype.push.apply(listeners, this.eventsOnce[event]); From 5dfecfab355be9fb844cafeedbb9fb86bc6b8b70 Mon Sep 17 00:00:00 2001 From: owenpearson Date: Fri, 26 Sep 2025 10:47:51 +0100 Subject: [PATCH 2/2] feat: add `RealtimeChannel.listeners` and `RealtimePresence.listeners` APIs --- ably.d.ts | 42 +++++++++++++++++++++++ src/common/lib/client/realtimechannel.ts | 5 +++ src/common/lib/client/realtimepresence.ts | 5 +++ 3 files changed, 52 insertions(+) diff --git a/ably.d.ts b/ably.d.ts index 4513f94c3e..d26a4807fc 100644 --- a/ably.d.ts +++ b/ably.d.ts @@ -2040,6 +2040,27 @@ export declare interface RealtimePresence { */ unsubscribe(): void; + /** + * Returns all listeners currently registered on this presence object. + * + * @returns An array of listener functions for all events. Returns an empty array if no listeners are found. + */ + listeners(): Function[]; + /** + * Returns the listeners for a specified presence action on this presence object. + * + * @param event - The presence action name to retrieve the listeners for. + * @returns An array of listener functions for the specified event. Returns an empty array if no listeners are found. + */ + listeners(event: string): Function[]; + /** + * Returns the listeners for multiple specified presence actions on this presence object. + * + * @param events - An array of presence action names to retrieve the listeners for. + * @returns An array of listener functions for all the specified events combined. Returns an empty array if no listeners are found. + */ + listeners(events: string[]): Function[]; + /** * Retrieves the current members present on the channel and the metadata for each member, such as their {@link PresenceAction} and ID. Returns an array of {@link PresenceMessage} objects. * @@ -2934,6 +2955,27 @@ export declare interface RealtimeChannel extends EventEmitter