@ipcom/asterisk-ari 0.0.97 → 0.0.98

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.
@@ -947,12 +947,20 @@ var ChannelInstance = class extends import_events2.EventEmitter {
947
947
  `Nenhum WebSocket conectado dispon\xEDvel para o canal: ${this.id}`
948
948
  );
949
949
  }
950
- wsClient.addScopedMessageListener(this.id, (event) => {
951
- if (isChannelEvent(event, this.id)) {
952
- const channelEventType = event.type;
953
- this.emit(channelEventType, event);
954
- }
955
- });
950
+ try {
951
+ wsClient.addScopedMessageListener(this.id, (event) => {
952
+ if (isChannelEvent(event, this.id)) {
953
+ const channelEventType = event.type;
954
+ this.emit(channelEventType, event);
955
+ }
956
+ });
957
+ } catch (error) {
958
+ console.error(
959
+ `Erro ao adicionar listener escopado para canal '${this.id}':`,
960
+ error
961
+ );
962
+ throw error;
963
+ }
956
964
  this.on("removeListener", (eventName) => {
957
965
  if (this.eventNames().length === 0 || this.listenerCount(eventName) === 0) {
958
966
  wsClient.removeScopedMessageListeners(this.id);
@@ -991,11 +999,21 @@ var ChannelInstance = class extends import_events2.EventEmitter {
991
999
  console.log(`Recebido evento '${event}' para canal '${this.id}'`);
992
1000
  this.emit(event, webSocketEvent);
993
1001
  callback(webSocketEvent);
994
- wsClient.off("message", globalListener);
995
- console.log(`Listener global para canal '${this.id}' foi removido.`);
1002
+ wsClient.removeScopedMessageListeners(this.id);
1003
+ console.log(
1004
+ `Listeners escopados para canal '${this.id}' foram removidos.`
1005
+ );
996
1006
  }
997
1007
  };
998
- wsClient.on("message", globalListener);
1008
+ try {
1009
+ wsClient.addScopedMessageListener(this.id, globalListener);
1010
+ } catch (error) {
1011
+ console.error(
1012
+ `Erro ao adicionar listener escopado em 'once' para canal '${this.id}':`,
1013
+ error
1014
+ );
1015
+ throw error;
1016
+ }
999
1017
  super.once(event, callback);
1000
1018
  return this;
1001
1019
  }
@@ -1438,12 +1456,20 @@ var PlaybackInstance = class extends import_events3.EventEmitter {
1438
1456
  `Nenhum WebSocket conectado dispon\xEDvel para o playback: ${this.id}`
1439
1457
  );
1440
1458
  }
1441
- wsClient.addScopedMessageListener(this.id, (event) => {
1442
- if (isPlaybackEvent(event, this.id)) {
1443
- const playbackEventType = event.type;
1444
- this.emit(playbackEventType, event);
1445
- }
1446
- });
1459
+ try {
1460
+ wsClient.addScopedMessageListener(this.id, (event) => {
1461
+ if (isPlaybackEvent(event, this.id)) {
1462
+ const playbackEventType = event.type;
1463
+ this.emit(playbackEventType, event);
1464
+ }
1465
+ });
1466
+ } catch (error) {
1467
+ console.error(
1468
+ `Erro ao adicionar listener escopado para playback '${this.id}':`,
1469
+ error
1470
+ );
1471
+ throw error;
1472
+ }
1447
1473
  this.on("removeListener", (eventName) => {
1448
1474
  if (this.eventNames().length === 0 || this.listenerCount(eventName) === 0) {
1449
1475
  wsClient.removeScopedMessageListeners(this.id);
@@ -1493,7 +1519,15 @@ var PlaybackInstance = class extends import_events3.EventEmitter {
1493
1519
  );
1494
1520
  }
1495
1521
  };
1496
- wsClient.addScopedMessageListener(this.id, globalListener);
1522
+ try {
1523
+ wsClient.addScopedMessageListener(this.id, globalListener);
1524
+ } catch (error) {
1525
+ console.error(
1526
+ `Erro ao adicionar listener escopado em 'once' para playback '${this.id}':`,
1527
+ error
1528
+ );
1529
+ throw error;
1530
+ }
1497
1531
  super.once(event, callback);
1498
1532
  return this;
1499
1533
  }
@@ -1664,7 +1698,6 @@ var import_events4 = require("events");
1664
1698
  var import_exponential_backoff = __toESM(require_backoff(), 1);
1665
1699
  var import_ws = __toESM(require("ws"), 1);
1666
1700
  var WebSocketClient = class extends import_events4.EventEmitter {
1667
- // Para gerenciar tentativas de reconexão
1668
1701
  /**
1669
1702
  * Creates a new WebSocketClient instance.
1670
1703
  * @param url - The WebSocket server URL to connect to.
@@ -1679,6 +1712,8 @@ var WebSocketClient = class extends import_events4.EventEmitter {
1679
1712
  messageListeners = [];
1680
1713
  maxReconnectAttempts = 30;
1681
1714
  reconnectAttempts = 0;
1715
+ // Para gerenciar tentativas de reconexão
1716
+ scopedListeners = /* @__PURE__ */ new Map();
1682
1717
  async reconnect() {
1683
1718
  console.log("Iniciando processo de reconex\xE3o...");
1684
1719
  const backoffOptions = {
@@ -1802,25 +1837,31 @@ var WebSocketClient = class extends import_events4.EventEmitter {
1802
1837
  */
1803
1838
  addScopedMessageListener(instanceId, callback) {
1804
1839
  const scopedListener = (data) => {
1805
- if (data.instanceId === instanceId) {
1840
+ if (data.type.startsWith("Channel") && "channel" in data && data.channel?.id === instanceId || // Verifica se o evento é de playback e possui a propriedade `playbackId`
1841
+ data.type.startsWith("Playback") && "playbackId" in data && data.playbackId === instanceId) {
1806
1842
  callback(data);
1807
1843
  }
1808
1844
  };
1845
+ this.scopedListeners.set(instanceId, scopedListener);
1809
1846
  this.on("message", scopedListener);
1810
- this.messageListeners.push(scopedListener);
1811
1847
  }
1812
1848
  /**
1813
1849
  * Remove todos os listeners associados a uma instância específica.
1814
1850
  * @param instanceId - Identificador da instância cujos listeners serão removidos.
1815
1851
  */
1816
1852
  removeScopedMessageListeners(instanceId) {
1817
- this.messageListeners = this.messageListeners.filter((listener) => {
1818
- const shouldRemove = listener.name === instanceId;
1819
- if (shouldRemove) {
1820
- this.off("message", listener);
1821
- }
1822
- return !shouldRemove;
1823
- });
1853
+ if (!this.scopedListeners.has(instanceId)) {
1854
+ console.warn(
1855
+ `Nenhum listener encontrado para a inst\xE2ncia '${instanceId}'.`
1856
+ );
1857
+ return;
1858
+ }
1859
+ const scopedListener = this.scopedListeners.get(instanceId);
1860
+ if (scopedListener) {
1861
+ this.off("message", scopedListener);
1862
+ this.scopedListeners.delete(instanceId);
1863
+ console.log(`Listeners removidos para a inst\xE2ncia '${instanceId}'.`);
1864
+ }
1824
1865
  }
1825
1866
  /**
1826
1867
  * Handles incoming WebSocket messages.