@ipcom/asterisk-ari 0.0.76 → 0.0.77

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
@@ -4,6 +4,8 @@ import axios from "axios";
4
4
  var BaseClient = class {
5
5
  client;
6
6
  eventEmitter = new EventEmitter();
7
+ wsClients = /* @__PURE__ */ new Map();
8
+ // Gerencia os WebSocket clients
7
9
  constructor(baseUrl, username, password) {
8
10
  this.client = axios.create({
9
11
  baseURL: baseUrl,
@@ -44,9 +46,46 @@ var BaseClient = class {
44
46
  const response = await this.client.delete(path);
45
47
  return response.data;
46
48
  }
49
+ // Gerenciamento de WebSocket clients
47
50
  /**
48
- * Escuta eventos do WebSocket.
51
+ * Adiciona um WebSocket client ao gerenciador.
52
+ * @param app - Nome do aplicativo ou contexto associado.
53
+ * @param wsClient - Instância do WebSocketClient.
49
54
  */
55
+ addWebSocketClient(app, wsClient) {
56
+ if (this.wsClients.has(app)) {
57
+ throw new Error(`J\xE1 existe um WebSocket client registrado para '${app}'`);
58
+ }
59
+ this.wsClients.set(app, wsClient);
60
+ console.log(`WebSocket client adicionado para '${app}'`);
61
+ }
62
+ /**
63
+ * Remove um WebSocket client do gerenciador.
64
+ * @param app - Nome do aplicativo ou contexto associado.
65
+ */
66
+ removeWebSocketClient(app) {
67
+ if (!this.wsClients.has(app)) {
68
+ throw new Error(`Nenhum WebSocket client encontrado para '${app}'`);
69
+ }
70
+ this.wsClients.delete(app);
71
+ console.log(`WebSocket client removido para '${app}'`);
72
+ }
73
+ /**
74
+ * Obtém todos os WebSocket clients.
75
+ * @returns Um Map contendo todos os WebSocket clients registrados.
76
+ */
77
+ getWebSocketClients() {
78
+ return this.wsClients;
79
+ }
80
+ /**
81
+ * Obtém um WebSocket client específico.
82
+ * @param app - Nome do aplicativo ou contexto associado.
83
+ * @returns O WebSocket client correspondente.
84
+ */
85
+ getWebSocketClient(app) {
86
+ return this.wsClients.get(app);
87
+ }
88
+ // Gerenciamento de eventos WebSocket
50
89
  onWebSocketEvent(callback) {
51
90
  console.log("Registrando callback para eventos do WebSocket");
52
91
  this.eventEmitter.on("websocketEvent", (event) => {
@@ -54,9 +93,6 @@ var BaseClient = class {
54
93
  callback(event);
55
94
  });
56
95
  }
57
- /**
58
- * Emite eventos do WebSocket.
59
- */
60
96
  emitWebSocketEvent(event) {
61
97
  console.log("Emitindo evento do WebSocket:", event);
62
98
  this.eventEmitter.emit("websocketEvent", event);
@@ -769,8 +805,21 @@ var PlaybackInstance = class extends EventEmitter3 {
769
805
  this.baseClient = baseClient;
770
806
  this.playbackId = playbackId;
771
807
  this.id = playbackId || `playback-${Date.now()}`;
772
- this.baseClient.onWebSocketEvent((event) => {
773
- if (this.isPlaybackEvent(event) && event.playbackId === this.playbackId) {
808
+ const wsClients = this.baseClient.getWebSocketClients();
809
+ const wsClient = Array.from(wsClients.values()).find(
810
+ (client) => client.isConnected()
811
+ );
812
+ if (!wsClient) {
813
+ throw new Error(
814
+ `Nenhum WebSocket conectado dispon\xEDvel para o playback: ${this.id}`
815
+ );
816
+ }
817
+ wsClient.on("message", (event) => {
818
+ if (this.isPlaybackEvent(event)) {
819
+ console.log(
820
+ `Evento recebido no PlaybackInstance: ${event.type}`,
821
+ event
822
+ );
774
823
  this.emit(event.type, event);
775
824
  }
776
825
  });
@@ -781,7 +830,15 @@ var PlaybackInstance = class extends EventEmitter3 {
781
830
  * Verifica se o evento é relacionado a um playback.
782
831
  */
783
832
  isPlaybackEvent(event) {
784
- return event && typeof event === "object" && "playbackId" in event;
833
+ const isPlaybackType = event?.type.startsWith("Playback");
834
+ const isPlaybackEvent = isPlaybackType && "playbackId" in event && event.playbackId === this.id;
835
+ if (!isPlaybackEvent) {
836
+ console.log(
837
+ `Evento ignorado no isPlaybackEvent: tipo=${event.type}, playback esperado=${this.id}, evento recebido=`,
838
+ event
839
+ );
840
+ }
841
+ return isPlaybackEvent;
785
842
  }
786
843
  /**
787
844
  * Obtém os detalhes do playback.
@@ -850,6 +907,12 @@ var Playbacks = class extends EventEmitter3 {
850
907
  }
851
908
  });
852
909
  }
910
+ /**
911
+ * Obtém os clientes WebSocket disponíveis.
912
+ */
913
+ getWebSocketClients() {
914
+ return this.client.getWebSocketClients();
915
+ }
853
916
  /**
854
917
  * Verifica se o evento é relacionado a um playback.
855
918
  */