@@ -1803,7 +1803,7 @@ var ChannelInstance = class {
1803
1803
* Otherwise, all listeners for the given event type are removed.
1804
1804
*
1805
1805
* @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
1807
1807
* @throws {Error } If no event type is provided
1808
1808
*/
1809
1809
off ( event , listener ) {
@@ -2765,7 +2765,7 @@ var PlaybackInstance = class {
2765
2765
* Registers an event listener for a specific WebSocket event type.
2766
2766
*
2767
2767
* @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
2769
2769
*/
2770
2770
on ( event , listener ) {
2771
2771
if ( ! event ) {
@@ -2793,7 +2793,7 @@ var PlaybackInstance = class {
2793
2793
* Registers a one-time event listener for a specific WebSocket event type.
2794
2794
*
2795
2795
* @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
2797
2797
*/
2798
2798
once ( event , listener ) {
2799
2799
if ( ! event ) {
@@ -2823,7 +2823,7 @@ var PlaybackInstance = class {
2823
2823
* Removes event listener(s) for a specific WebSocket event type.
2824
2824
*
2825
2825
* @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
2827
2827
*/
2828
2828
off ( event , listener ) {
2829
2829
if ( ! event ) {
@@ -3004,11 +3004,11 @@ var Playbacks = class {
3004
3004
* This method performs the following cleanup operations:
3005
3005
* 1. Clears all pending timeouts in the event queue.
3006
3006
* 2. Removes all playback instances.
3007
- *
3007
+ *
3008
3008
* @remarks
3009
3009
* This method should be called when the Playbacks instance is no longer needed
3010
3010
* to ensure proper resource management and prevent memory leaks.
3011
- *
3011
+ *
3012
3012
* @returns {void } This method doesn't return a value.
3013
3013
*/
3014
3014
cleanup ( ) {
@@ -3324,6 +3324,58 @@ var WebSocketClient = class extends import_events4.EventEmitter {
3324
3324
this . isConnecting = false ;
3325
3325
}
3326
3326
}
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
+ }
3327
3379
/**
3328
3380
* Initializes a WebSocket connection with exponential backoff retry mechanism.
3329
3381
*
@@ -3595,7 +3647,6 @@ var AriClient = class {
3595
3647
baseClient ;
3596
3648
webSocketClient ;
3597
3649
eventListeners = /* @__PURE__ */ new Map ( ) ;
3598
- // Armazena os listeners para limpeza
3599
3650
channels ;
3600
3651
endpoints ;
3601
3652
applications ;
@@ -3653,16 +3704,19 @@ var AriClient = class {
3653
3704
* @param {string[] } apps - List of application names to subscribe to
3654
3705
* @param {WebSocketEventType[] } [subscribedEvents] - Optional list of specific event types to subscribe to
3655
3706
* @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
3657
3708
*/
3658
3709
async connectWebSocket ( apps , subscribedEvents ) {
3659
3710
try {
3660
3711
if ( ! apps . length ) {
3661
3712
throw new Error ( "At least one application name is required." ) ;
3662
3713
}
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 ;
3666
3720
}
3667
3721
this . webSocketClient = new WebSocketClient (
3668
3722
this . baseClient ,
@@ -3673,10 +3727,25 @@ var AriClient = class {
3673
3727
await this . webSocketClient . connect ( ) ;
3674
3728
} catch ( error ) {
3675
3729
console . error ( "Failed to establish WebSocket connection:" , error ) ;
3676
- this . webSocketClient = void 0 ;
3677
3730
throw error ;
3678
3731
}
3679
3732
}
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
+ }
3680
3749
/**
3681
3750
* Destroys the ARI Client instance, cleaning up all resources and removing circular references.
3682
3751
* This method should be called when the ARI Client is no longer needed to ensure proper cleanup.
@@ -3702,6 +3771,13 @@ var AriClient = class {
3702
3771
throw error ;
3703
3772
}
3704
3773
}
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
+ */
3705
3781
/**
3706
3782
* Registers an event listener for WebSocket events.
3707
3783
*
@@ -3721,6 +3797,7 @@ var AriClient = class {
3721
3797
this . webSocketClient . on ( event , listener ) ;
3722
3798
existingListeners . push ( listener ) ;
3723
3799
this . eventListeners . set ( event , existingListeners ) ;
3800
+ console . log ( `Event listener successfully registered for ${ event } ` ) ;
3724
3801
}
3725
3802
/**
3726
3803
* Registers a one-time event listener for WebSocket events.
@@ -3745,7 +3822,10 @@ var AriClient = class {
3745
3822
this . off ( event , wrappedListener ) ;
3746
3823
} ;
3747
3824
this . webSocketClient . once ( event , wrappedListener ) ;
3748
- this . eventListeners . set ( event , [ ...existingListeners , wrappedListener ] ) ;
3825
+ this . eventListeners . set ( event , [
3826
+ ...existingListeners ,
3827
+ wrappedListener
3828
+ ] ) ;
3749
3829
console . log ( `One-time event listener registered for ${ event } ` ) ;
3750
3830
}
3751
3831
/**
@@ -3763,7 +3843,9 @@ var AriClient = class {
3763
3843
const existingListeners = this . eventListeners . get ( event ) || [ ] ;
3764
3844
this . eventListeners . set (
3765
3845
event ,
3766
- existingListeners . filter ( ( l ) => l !== listener )
3846
+ existingListeners . filter (
3847
+ ( l ) => l !== listener
3848
+ )
3767
3849
) ;
3768
3850
console . log ( `Event listener removed for ${ event } ` ) ;
3769
3851
}
0 commit comments