@ipcom/asterisk-ari 0.0.70 → 0.0.72

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.
@@ -896,8 +896,17 @@ var ChannelInstance = class extends import_events2.EventEmitter {
896
896
  this.baseClient = baseClient;
897
897
  this.channelId = channelId;
898
898
  this.id = channelId || `channel-${Date.now()}`;
899
- this.baseClient.onWebSocketEvent((event) => {
900
- if (this.isChannelEvent(event) && event.channel?.id === this.channelId) {
899
+ const wsClients = this.client.getWebSocketClients();
900
+ const wsClient = Array.from(wsClients.values()).find(
901
+ (client2) => client2.isConnected()
902
+ );
903
+ if (!wsClient) {
904
+ throw new Error(
905
+ `Nenhum WebSocket conectado dispon\xEDvel para o canal: ${this.channelId}`
906
+ );
907
+ }
908
+ wsClient.on("message", (event) => {
909
+ if (this.isChannelEvent(event)) {
901
910
  console.log(`Evento recebido no ChannelInstance: ${event.type}`, event);
902
911
  this.emit(event.type, event);
903
912
  }
@@ -1723,22 +1732,14 @@ var AriClient = class {
1723
1732
  channelInstances = /* @__PURE__ */ new Map();
1724
1733
  pendingListeners = [];
1725
1734
  processPendingListeners() {
1726
- if (this.wsClients.size === 0) {
1727
- console.warn(
1728
- "Nenhuma conex\xE3o WebSocket est\xE1 pronta. N\xE3o \xE9 poss\xEDvel processar a fila."
1729
- );
1730
- return;
1731
- }
1732
- console.log(
1733
- `Processando ${this.pendingListeners.length} listeners pendentes...`
1734
- );
1735
- while (this.pendingListeners.length > 0) {
1736
- const { event, callback } = this.pendingListeners.shift();
1737
- console.log(`Registrando listener pendente para evento: ${event}`);
1738
- for (const [app, wsClient] of this.wsClients.entries()) {
1739
- console.log(
1740
- `Registrando listener no app '${app}' para evento: ${event}`
1741
- );
1735
+ for (const [app, wsClient] of this.wsClients.entries()) {
1736
+ if (!wsClient.isConnected()) {
1737
+ console.warn(`WebSocket para o app '${app}' ainda n\xE3o est\xE1 conectado.`);
1738
+ continue;
1739
+ }
1740
+ while (this.pendingListeners.length > 0) {
1741
+ const { event, callback } = this.pendingListeners.shift();
1742
+ console.log(`Registrando listener para '${app}' no evento: ${event}`);
1742
1743
  wsClient.on(event, callback);
1743
1744
  }
1744
1745
  }
@@ -1750,6 +1751,10 @@ var AriClient = class {
1750
1751
  sounds;
1751
1752
  asterisk;
1752
1753
  bridges;
1754
+ // Getter para wsClients
1755
+ getWebSocketClients() {
1756
+ return this.wsClients;
1757
+ }
1753
1758
  /**
1754
1759
  * Registra um listener para eventos de WebSocket relacionados a canais.
1755
1760
  * @param eventType Tipo de evento.
@@ -1769,8 +1774,7 @@ var AriClient = class {
1769
1774
  }
1770
1775
  }
1771
1776
  /**
1772
- * Registra um listener para eventos globais de WebSocket.
1773
- * O channel no evento será automaticamente transformado em uma instância de ChannelInstance.
1777
+ * Registra listeners globais para eventos de WebSocket.
1774
1778
  */
1775
1779
  on(eventType, callback) {
1776
1780
  console.log(`Registrando listener global para evento: ${eventType}`);
@@ -1787,9 +1791,10 @@ var AriClient = class {
1787
1791
  }
1788
1792
  callback(event);
1789
1793
  };
1794
+ for (const [_app, wsClient] of this.wsClients.entries()) {
1795
+ wsClient.on(eventType, wrappedCallback);
1796
+ }
1790
1797
  this.eventListeners.set(eventType, wrappedCallback);
1791
- this.pendingListeners.push({ event: eventType, callback: wrappedCallback });
1792
- this.processPendingListeners();
1793
1798
  }
1794
1799
  /**
1795
1800
  * Type guard to check if the given data object contains a valid channel property.
@@ -1852,7 +1857,13 @@ var AriClient = class {
1852
1857
  throw new Error("\xC9 necess\xE1rio fornecer pelo menos um aplicativo.");
1853
1858
  }
1854
1859
  return Promise.all(
1855
- apps.map((app) => this.connectSingleWebSocket(app, subscribedEvents))
1860
+ apps.map(async (app) => {
1861
+ if (this.wsClients.has(app)) {
1862
+ console.log(`Conex\xE3o WebSocket para '${app}' j\xE1 existe.`);
1863
+ return;
1864
+ }
1865
+ await this.connectSingleWebSocket(app, subscribedEvents);
1866
+ })
1856
1867
  );
1857
1868
  }
1858
1869
  /**