@ipcom/asterisk-ari 0.0.71 → 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.
package/dist/esm/index.js CHANGED
@@ -874,8 +874,17 @@ var ChannelInstance = class extends EventEmitter2 {
874
874
  this.baseClient = baseClient;
875
875
  this.channelId = channelId;
876
876
  this.id = channelId || `channel-${Date.now()}`;
877
- this.baseClient.onWebSocketEvent((event) => {
878
- if (this.isChannelEvent(event) && event.channel?.id === this.channelId) {
877
+ const wsClients = this.client.getWebSocketClients();
878
+ const wsClient = Array.from(wsClients.values()).find(
879
+ (client2) => client2.isConnected()
880
+ );
881
+ if (!wsClient) {
882
+ throw new Error(
883
+ `Nenhum WebSocket conectado dispon\xEDvel para o canal: ${this.channelId}`
884
+ );
885
+ }
886
+ wsClient.on("message", (event) => {
887
+ if (this.isChannelEvent(event)) {
879
888
  console.log(`Evento recebido no ChannelInstance: ${event.type}`, event);
880
889
  this.emit(event.type, event);
881
890
  }
@@ -1701,22 +1710,14 @@ var AriClient = class {
1701
1710
  channelInstances = /* @__PURE__ */ new Map();
1702
1711
  pendingListeners = [];
1703
1712
  processPendingListeners() {
1704
- if (this.wsClients.size === 0) {
1705
- console.warn(
1706
- "Nenhuma conex\xE3o WebSocket est\xE1 pronta. N\xE3o \xE9 poss\xEDvel processar a fila."
1707
- );
1708
- return;
1709
- }
1710
- console.log(
1711
- `Processando ${this.pendingListeners.length} listeners pendentes...`
1712
- );
1713
- while (this.pendingListeners.length > 0) {
1714
- const { event, callback } = this.pendingListeners.shift();
1715
- console.log(`Registrando listener pendente para evento: ${event}`);
1716
- for (const [app, wsClient] of this.wsClients.entries()) {
1717
- console.log(
1718
- `Registrando listener no app '${app}' para evento: ${event}`
1719
- );
1713
+ for (const [app, wsClient] of this.wsClients.entries()) {
1714
+ if (!wsClient.isConnected()) {
1715
+ console.warn(`WebSocket para o app '${app}' ainda n\xE3o est\xE1 conectado.`);
1716
+ continue;
1717
+ }
1718
+ while (this.pendingListeners.length > 0) {
1719
+ const { event, callback } = this.pendingListeners.shift();
1720
+ console.log(`Registrando listener para '${app}' no evento: ${event}`);
1720
1721
  wsClient.on(event, callback);
1721
1722
  }
1722
1723
  }
@@ -1728,6 +1729,10 @@ var AriClient = class {
1728
1729
  sounds;
1729
1730
  asterisk;
1730
1731
  bridges;
1732
+ // Getter para wsClients
1733
+ getWebSocketClients() {
1734
+ return this.wsClients;
1735
+ }
1731
1736
  /**
1732
1737
  * Registra um listener para eventos de WebSocket relacionados a canais.
1733
1738
  * @param eventType Tipo de evento.
@@ -1747,8 +1752,7 @@ var AriClient = class {
1747
1752
  }
1748
1753
  }
1749
1754
  /**
1750
- * Registra um listener para eventos globais de WebSocket.
1751
- * O channel no evento será automaticamente transformado em uma instância de ChannelInstance.
1755
+ * Registra listeners globais para eventos de WebSocket.
1752
1756
  */
1753
1757
  on(eventType, callback) {
1754
1758
  console.log(`Registrando listener global para evento: ${eventType}`);
@@ -1765,9 +1769,10 @@ var AriClient = class {
1765
1769
  }
1766
1770
  callback(event);
1767
1771
  };
1772
+ for (const [_app, wsClient] of this.wsClients.entries()) {
1773
+ wsClient.on(eventType, wrappedCallback);
1774
+ }
1768
1775
  this.eventListeners.set(eventType, wrappedCallback);
1769
- this.pendingListeners.push({ event: eventType, callback: wrappedCallback });
1770
- this.processPendingListeners();
1771
1776
  }
1772
1777
  /**
1773
1778
  * Type guard to check if the given data object contains a valid channel property.
@@ -1830,7 +1835,13 @@ var AriClient = class {
1830
1835
  throw new Error("\xC9 necess\xE1rio fornecer pelo menos um aplicativo.");
1831
1836
  }
1832
1837
  return Promise.all(
1833
- apps.map((app) => this.connectSingleWebSocket(app, subscribedEvents))
1838
+ apps.map(async (app) => {
1839
+ if (this.wsClients.has(app)) {
1840
+ console.log(`Conex\xE3o WebSocket para '${app}' j\xE1 existe.`);
1841
+ return;
1842
+ }
1843
+ await this.connectSingleWebSocket(app, subscribedEvents);
1844
+ })
1834
1845
  );
1835
1846
  }
1836
1847
  /**