@ipcom/asterisk-ari 0.0.158 → 0.0.159

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm/index.js CHANGED
@@ -1782,7 +1782,7 @@ var ChannelInstance = class {
1782
1782
  * Otherwise, all listeners for the given event type are removed.
1783
1783
  *
1784
1784
  * @param {T} event - The type of WebSocket event to remove listener(s) for
1785
- * @param {Function} [listener] - Optional specific listener to remove
1785
+ * @param {(data: WebSocketEvent) => void} [listener] - Optional specific listener to remove
1786
1786
  * @throws {Error} If no event type is provided
1787
1787
  */
1788
1788
  off(event, listener) {
@@ -2744,7 +2744,7 @@ var PlaybackInstance = class {
2744
2744
  * Registers an event listener for a specific WebSocket event type.
2745
2745
  *
2746
2746
  * @param {T} event - Event type to listen for
2747
- * @param {Function} listener - Callback function for the event
2747
+ * @param {(data: WebSocketEvent) => void} listener - Callback function for the event
2748
2748
  */
2749
2749
  on(event, listener) {
2750
2750
  if (!event) {
@@ -2772,7 +2772,7 @@ var PlaybackInstance = class {
2772
2772
  * Registers a one-time event listener for a specific WebSocket event type.
2773
2773
  *
2774
2774
  * @param {T} event - Event type to listen for
2775
- * @param {Function} listener - Callback function for the event
2775
+ * @param {(data: WebSocketEvent) => void} listener - Callback function for the event
2776
2776
  */
2777
2777
  once(event, listener) {
2778
2778
  if (!event) {
@@ -2802,7 +2802,7 @@ var PlaybackInstance = class {
2802
2802
  * Removes event listener(s) for a specific WebSocket event type.
2803
2803
  *
2804
2804
  * @param {T} event - Event type to remove listener(s) for
2805
- * @param {Function} [listener] - Optional specific listener to remove
2805
+ * @param {(data: WebSocketEvent) => void} [listener] - Optional specific listener to remove
2806
2806
  */
2807
2807
  off(event, listener) {
2808
2808
  if (!event) {
@@ -2983,11 +2983,11 @@ var Playbacks = class {
2983
2983
  * This method performs the following cleanup operations:
2984
2984
  * 1. Clears all pending timeouts in the event queue.
2985
2985
  * 2. Removes all playback instances.
2986
- *
2986
+ *
2987
2987
  * @remarks
2988
2988
  * This method should be called when the Playbacks instance is no longer needed
2989
2989
  * to ensure proper resource management and prevent memory leaks.
2990
- *
2990
+ *
2991
2991
  * @returns {void} This method doesn't return a value.
2992
2992
  */
2993
2993
  cleanup() {
@@ -3303,6 +3303,58 @@ var WebSocketClient = class extends EventEmitter4 {
3303
3303
  this.isConnecting = false;
3304
3304
  }
3305
3305
  }
3306
+ /**
3307
+ * Reconecta o WebSocket com uma lista atualizada de aplicações.
3308
+ *
3309
+ * @param {string[]} newApps - Lista de aplicações para reconectar
3310
+ * @param {WebSocketEventType[]} [subscribedEvents] - Tipos de eventos para se inscrever (opcional)
3311
+ * @returns {Promise<void>} Promise resolvida quando reconectado com sucesso
3312
+ */
3313
+ async reconnectWithApps(newApps, subscribedEvents) {
3314
+ if (!newApps.length) {
3315
+ throw new Error("At least one application name is required");
3316
+ }
3317
+ const uniqueApps = Array.from(/* @__PURE__ */ new Set([...this.apps, ...newApps]));
3318
+ if (uniqueApps.length === this.apps.length && uniqueApps.every((app) => this.apps.includes(app))) {
3319
+ console.log(
3320
+ "No changes in applications list, maintaining current connection"
3321
+ );
3322
+ return;
3323
+ }
3324
+ console.log(
3325
+ `Reconnecting WebSocket with updated applications: ${uniqueApps.join(", ")}`
3326
+ );
3327
+ this.apps = uniqueApps;
3328
+ if (subscribedEvents) {
3329
+ this.subscribedEvents = subscribedEvents;
3330
+ }
3331
+ if (this.ws) {
3332
+ await new Promise((resolve) => {
3333
+ this.once("disconnected", () => resolve());
3334
+ this.close();
3335
+ });
3336
+ }
3337
+ await this.connect();
3338
+ console.log("WebSocket reconnected successfully with updated applications");
3339
+ }
3340
+ /**
3341
+ * Adiciona novas aplicações à conexão WebSocket existente.
3342
+ *
3343
+ * @param {string[]} newApps - Lista de novas aplicações para adicionar
3344
+ * @param {WebSocketEventType[]} [subscribedEvents] - Tipos de eventos para se inscrever (opcional)
3345
+ * @returns {Promise<void>} Promise resolvida quando as aplicações são adicionadas com sucesso
3346
+ */
3347
+ async addApps(newApps, subscribedEvents) {
3348
+ if (!newApps.length) {
3349
+ throw new Error("At least one application name is required");
3350
+ }
3351
+ const appsToAdd = newApps.filter((app) => !this.apps.includes(app));
3352
+ if (appsToAdd.length === 0) {
3353
+ console.log("All applications are already registered");
3354
+ return;
3355
+ }
3356
+ await this.reconnectWithApps(appsToAdd, subscribedEvents);
3357
+ }
3306
3358
  /**
3307
3359
  * Initializes a WebSocket connection with exponential backoff retry mechanism.
3308
3360
  *
@@ -3574,7 +3626,6 @@ var AriClient = class {
3574
3626
  baseClient;
3575
3627
  webSocketClient;
3576
3628
  eventListeners = /* @__PURE__ */ new Map();
3577
- // Armazena os listeners para limpeza
3578
3629
  channels;
3579
3630
  endpoints;
3580
3631
  applications;
@@ -3632,16 +3683,19 @@ var AriClient = class {
3632
3683
  * @param {string[]} apps - List of application names to subscribe to
3633
3684
  * @param {WebSocketEventType[]} [subscribedEvents] - Optional list of specific event types to subscribe to
3634
3685
  * @returns {Promise<void>} Resolves when connection is established
3635
- * @throws {Error} If connection fails or if WebSocket is already connected
3686
+ * @throws {Error} If connection fails
3636
3687
  */
3637
3688
  async connectWebSocket(apps, subscribedEvents) {
3638
3689
  try {
3639
3690
  if (!apps.length) {
3640
3691
  throw new Error("At least one application name is required.");
3641
3692
  }
3642
- if (this.webSocketClient) {
3643
- await this.closeWebSocket();
3644
- await new Promise((resolve) => setTimeout(resolve, 1e3));
3693
+ if (this.webSocketClient && this.webSocketClient.isConnected()) {
3694
+ console.log(
3695
+ "WebSocket already connected. Reconnecting with updated apps..."
3696
+ );
3697
+ await this.webSocketClient.reconnectWithApps(apps, subscribedEvents);
3698
+ return;
3645
3699
  }
3646
3700
  this.webSocketClient = new WebSocketClient(
3647
3701
  this.baseClient,
@@ -3652,10 +3706,25 @@ var AriClient = class {
3652
3706
  await this.webSocketClient.connect();
3653
3707
  } catch (error) {
3654
3708
  console.error("Failed to establish WebSocket connection:", error);
3655
- this.webSocketClient = void 0;
3656
3709
  throw error;
3657
3710
  }
3658
3711
  }
3712
+ /**
3713
+ * Adds applications to the existing WebSocket connection.
3714
+ *
3715
+ * @param {string[]} apps - Additional applications to subscribe to
3716
+ * @param {WebSocketEventType[]} [subscribedEvents] - Optional list of specific event types to subscribe to
3717
+ * @returns {Promise<void>} Resolves when applications are added successfully
3718
+ * @throws {Error} If no WebSocket connection exists or if the operation fails
3719
+ */
3720
+ async addApplicationsToWebSocket(apps, subscribedEvents) {
3721
+ if (!this.webSocketClient || !this.webSocketClient.isConnected()) {
3722
+ throw new Error(
3723
+ "No active WebSocket connection. Create one first with connectWebSocket()."
3724
+ );
3725
+ }
3726
+ await this.webSocketClient.addApps(apps, subscribedEvents);
3727
+ }
3659
3728
  /**
3660
3729
  * Destroys the ARI Client instance, cleaning up all resources and removing circular references.
3661
3730
  * This method should be called when the ARI Client is no longer needed to ensure proper cleanup.
@@ -3681,6 +3750,13 @@ var AriClient = class {
3681
3750
  throw error;
3682
3751
  }
3683
3752
  }
3753
+ /**
3754
+ * Registers an event listener for WebSocket events.
3755
+ *
3756
+ * @param {T} event - The event type to listen for
3757
+ * @param {Function} listener - Callback function for handling the event
3758
+ * @throws {Error} If WebSocket is not connected
3759
+ */
3684
3760
  /**
3685
3761
  * Registers an event listener for WebSocket events.
3686
3762
  *
@@ -3700,6 +3776,7 @@ var AriClient = class {
3700
3776
  this.webSocketClient.on(event, listener);
3701
3777
  existingListeners.push(listener);
3702
3778
  this.eventListeners.set(event, existingListeners);
3779
+ console.log(`Event listener successfully registered for ${event}`);
3703
3780
  }
3704
3781
  /**
3705
3782
  * Registers a one-time event listener for WebSocket events.
@@ -3724,7 +3801,10 @@ var AriClient = class {
3724
3801
  this.off(event, wrappedListener);
3725
3802
  };
3726
3803
  this.webSocketClient.once(event, wrappedListener);
3727
- this.eventListeners.set(event, [...existingListeners, wrappedListener]);
3804
+ this.eventListeners.set(event, [
3805
+ ...existingListeners,
3806
+ wrappedListener
3807
+ ]);
3728
3808
  console.log(`One-time event listener registered for ${event}`);
3729
3809
  }
3730
3810
  /**
@@ -3742,7 +3822,9 @@ var AriClient = class {
3742
3822
  const existingListeners = this.eventListeners.get(event) || [];
3743
3823
  this.eventListeners.set(
3744
3824
  event,
3745
- existingListeners.filter((l) => l !== listener)
3825
+ existingListeners.filter(
3826
+ (l) => l !== listener
3827
+ )
3746
3828
  );
3747
3829
  console.log(`Event listener removed for ${event}`);
3748
3830
  }