Skip to content

Commit 5cc6619

Browse files
committed
version: 0.0.159
1 parent a72ab09 commit 5cc6619

23 files changed

+443
-95
lines changed

dist/cjs/index.cjs

Lines changed: 96 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,7 +1803,7 @@ var ChannelInstance = class {
18031803
* Otherwise, all listeners for the given event type are removed.
18041804
*
18051805
* @param {T} event - The type of WebSocket event to remove listener(s) for
1806-
* @param {Function} [listener] - Optional specific listener to remove
1806+
* @param {(data: WebSocketEvent) => void} [listener] - Optional specific listener to remove
18071807
* @throws {Error} If no event type is provided
18081808
*/
18091809
off(event, listener) {
@@ -2765,7 +2765,7 @@ var PlaybackInstance = class {
27652765
* Registers an event listener for a specific WebSocket event type.
27662766
*
27672767
* @param {T} event - Event type to listen for
2768-
* @param {Function} listener - Callback function for the event
2768+
* @param {(data: WebSocketEvent) => void} listener - Callback function for the event
27692769
*/
27702770
on(event, listener) {
27712771
if (!event) {
@@ -2793,7 +2793,7 @@ var PlaybackInstance = class {
27932793
* Registers a one-time event listener for a specific WebSocket event type.
27942794
*
27952795
* @param {T} event - Event type to listen for
2796-
* @param {Function} listener - Callback function for the event
2796+
* @param {(data: WebSocketEvent) => void} listener - Callback function for the event
27972797
*/
27982798
once(event, listener) {
27992799
if (!event) {
@@ -2823,7 +2823,7 @@ var PlaybackInstance = class {
28232823
* Removes event listener(s) for a specific WebSocket event type.
28242824
*
28252825
* @param {T} event - Event type to remove listener(s) for
2826-
* @param {Function} [listener] - Optional specific listener to remove
2826+
* @param {(data: WebSocketEvent) => void} [listener] - Optional specific listener to remove
28272827
*/
28282828
off(event, listener) {
28292829
if (!event) {
@@ -3004,11 +3004,11 @@ var Playbacks = class {
30043004
* This method performs the following cleanup operations:
30053005
* 1. Clears all pending timeouts in the event queue.
30063006
* 2. Removes all playback instances.
3007-
*
3007+
*
30083008
* @remarks
30093009
* This method should be called when the Playbacks instance is no longer needed
30103010
* to ensure proper resource management and prevent memory leaks.
3011-
*
3011+
*
30123012
* @returns {void} This method doesn't return a value.
30133013
*/
30143014
cleanup() {
@@ -3324,6 +3324,58 @@ var WebSocketClient = class extends import_events4.EventEmitter {
33243324
this.isConnecting = false;
33253325
}
33263326
}
3327+
/**
3328+
* Reconecta o WebSocket com uma lista atualizada de aplicações.
3329+
*
3330+
* @param {string[]} newApps - Lista de aplicações para reconectar
3331+
* @param {WebSocketEventType[]} [subscribedEvents] - Tipos de eventos para se inscrever (opcional)
3332+
* @returns {Promise<void>} Promise resolvida quando reconectado com sucesso
3333+
*/
3334+
async reconnectWithApps(newApps, subscribedEvents) {
3335+
if (!newApps.length) {
3336+
throw new Error("At least one application name is required");
3337+
}
3338+
const uniqueApps = Array.from(/* @__PURE__ */ new Set([...this.apps, ...newApps]));
3339+
if (uniqueApps.length === this.apps.length && uniqueApps.every((app) => this.apps.includes(app))) {
3340+
console.log(
3341+
"No changes in applications list, maintaining current connection"
3342+
);
3343+
return;
3344+
}
3345+
console.log(
3346+
`Reconnecting WebSocket with updated applications: ${uniqueApps.join(", ")}`
3347+
);
3348+
this.apps = uniqueApps;
3349+
if (subscribedEvents) {
3350+
this.subscribedEvents = subscribedEvents;
3351+
}
3352+
if (this.ws) {
3353+
await new Promise((resolve) => {
3354+
this.once("disconnected", () => resolve());
3355+
this.close();
3356+
});
3357+
}
3358+
await this.connect();
3359+
console.log("WebSocket reconnected successfully with updated applications");
3360+
}
3361+
/**
3362+
* Adiciona novas aplicações à conexão WebSocket existente.
3363+
*
3364+
* @param {string[]} newApps - Lista de novas aplicações para adicionar
3365+
* @param {WebSocketEventType[]} [subscribedEvents] - Tipos de eventos para se inscrever (opcional)
3366+
* @returns {Promise<void>} Promise resolvida quando as aplicações são adicionadas com sucesso
3367+
*/
3368+
async addApps(newApps, subscribedEvents) {
3369+
if (!newApps.length) {
3370+
throw new Error("At least one application name is required");
3371+
}
3372+
const appsToAdd = newApps.filter((app) => !this.apps.includes(app));
3373+
if (appsToAdd.length === 0) {
3374+
console.log("All applications are already registered");
3375+
return;
3376+
}
3377+
await this.reconnectWithApps(appsToAdd, subscribedEvents);
3378+
}
33273379
/**
33283380
* Initializes a WebSocket connection with exponential backoff retry mechanism.
33293381
*
@@ -3595,7 +3647,6 @@ var AriClient = class {
35953647
baseClient;
35963648
webSocketClient;
35973649
eventListeners = /* @__PURE__ */ new Map();
3598-
// Armazena os listeners para limpeza
35993650
channels;
36003651
endpoints;
36013652
applications;
@@ -3653,16 +3704,19 @@ var AriClient = class {
36533704
* @param {string[]} apps - List of application names to subscribe to
36543705
* @param {WebSocketEventType[]} [subscribedEvents] - Optional list of specific event types to subscribe to
36553706
* @returns {Promise<void>} Resolves when connection is established
3656-
* @throws {Error} If connection fails or if WebSocket is already connected
3707+
* @throws {Error} If connection fails
36573708
*/
36583709
async connectWebSocket(apps, subscribedEvents) {
36593710
try {
36603711
if (!apps.length) {
36613712
throw new Error("At least one application name is required.");
36623713
}
3663-
if (this.webSocketClient) {
3664-
await this.closeWebSocket();
3665-
await new Promise((resolve) => setTimeout(resolve, 1e3));
3714+
if (this.webSocketClient && this.webSocketClient.isConnected()) {
3715+
console.log(
3716+
"WebSocket already connected. Reconnecting with updated apps..."
3717+
);
3718+
await this.webSocketClient.reconnectWithApps(apps, subscribedEvents);
3719+
return;
36663720
}
36673721
this.webSocketClient = new WebSocketClient(
36683722
this.baseClient,
@@ -3673,10 +3727,25 @@ var AriClient = class {
36733727
await this.webSocketClient.connect();
36743728
} catch (error) {
36753729
console.error("Failed to establish WebSocket connection:", error);
3676-
this.webSocketClient = void 0;
36773730
throw error;
36783731
}
36793732
}
3733+
/**
3734+
* Adds applications to the existing WebSocket connection.
3735+
*
3736+
* @param {string[]} apps - Additional applications to subscribe to
3737+
* @param {WebSocketEventType[]} [subscribedEvents] - Optional list of specific event types to subscribe to
3738+
* @returns {Promise<void>} Resolves when applications are added successfully
3739+
* @throws {Error} If no WebSocket connection exists or if the operation fails
3740+
*/
3741+
async addApplicationsToWebSocket(apps, subscribedEvents) {
3742+
if (!this.webSocketClient || !this.webSocketClient.isConnected()) {
3743+
throw new Error(
3744+
"No active WebSocket connection. Create one first with connectWebSocket()."
3745+
);
3746+
}
3747+
await this.webSocketClient.addApps(apps, subscribedEvents);
3748+
}
36803749
/**
36813750
* Destroys the ARI Client instance, cleaning up all resources and removing circular references.
36823751
* This method should be called when the ARI Client is no longer needed to ensure proper cleanup.
@@ -3702,6 +3771,13 @@ var AriClient = class {
37023771
throw error;
37033772
}
37043773
}
3774+
/**
3775+
* Registers an event listener for WebSocket events.
3776+
*
3777+
* @param {T} event - The event type to listen for
3778+
* @param {Function} listener - Callback function for handling the event
3779+
* @throws {Error} If WebSocket is not connected
3780+
*/
37053781
/**
37063782
* Registers an event listener for WebSocket events.
37073783
*
@@ -3721,6 +3797,7 @@ var AriClient = class {
37213797
this.webSocketClient.on(event, listener);
37223798
existingListeners.push(listener);
37233799
this.eventListeners.set(event, existingListeners);
3800+
console.log(`Event listener successfully registered for ${event}`);
37243801
}
37253802
/**
37263803
* Registers a one-time event listener for WebSocket events.
@@ -3745,7 +3822,10 @@ var AriClient = class {
37453822
this.off(event, wrappedListener);
37463823
};
37473824
this.webSocketClient.once(event, wrappedListener);
3748-
this.eventListeners.set(event, [...existingListeners, wrappedListener]);
3825+
this.eventListeners.set(event, [
3826+
...existingListeners,
3827+
wrappedListener
3828+
]);
37493829
console.log(`One-time event listener registered for ${event}`);
37503830
}
37513831
/**
@@ -3763,7 +3843,9 @@ var AriClient = class {
37633843
const existingListeners = this.eventListeners.get(event) || [];
37643844
this.eventListeners.set(
37653845
event,
3766-
existingListeners.filter((l) => l !== listener)
3846+
existingListeners.filter(
3847+
(l) => l !== listener
3848+
)
37673849
);
37683850
console.log(`Event listener removed for ${event}`);
37693851
}

dist/cjs/index.cjs.map

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)