@ipcom/asterisk-ari 0.0.40 → 0.0.42

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.
@@ -864,6 +864,37 @@ function toQueryParams2(options) {
864
864
  Object.entries(options).filter(([, value]) => value !== void 0).map(([key, value]) => [key, value])
865
865
  ).toString();
866
866
  }
867
+ var ChannelEventEmitter = class {
868
+ eventEmitter = new import_events.EventEmitter();
869
+ channel;
870
+ constructor(channel) {
871
+ this.channel = channel;
872
+ }
873
+ /**
874
+ * Registra um listener para um evento no canal.
875
+ */
876
+ on(eventType, callback) {
877
+ this.eventEmitter.on(eventType, callback);
878
+ }
879
+ /**
880
+ * Emite um evento no canal.
881
+ */
882
+ emit(eventType, data) {
883
+ this.eventEmitter.emit(eventType, data);
884
+ }
885
+ /**
886
+ * Remove um listener de evento do canal.
887
+ */
888
+ off(eventType, callback) {
889
+ this.eventEmitter.off(eventType, callback);
890
+ }
891
+ /**
892
+ * Remove todos os listeners de um evento no canal.
893
+ */
894
+ removeAllListeners(eventType) {
895
+ this.eventEmitter.removeAllListeners(eventType);
896
+ }
897
+ };
867
898
  var ChannelInstance = class extends import_events.EventEmitter {
868
899
  constructor(client, baseClient, channelId = `channel-${Date.now()}`) {
869
900
  super();
@@ -1695,6 +1726,7 @@ var AriClient = class {
1695
1726
  this.bridges = new Bridges(this.baseClient);
1696
1727
  }
1697
1728
  wsClient = null;
1729
+ channelEmitters = /* @__PURE__ */ new Map();
1698
1730
  baseClient;
1699
1731
  isReconnecting = false;
1700
1732
  eventEmitter = new import_events4.EventEmitter();
@@ -1811,6 +1843,18 @@ var AriClient = class {
1811
1843
  `WebSocket client para o app '${app}' n\xE3o est\xE1 conectado.`
1812
1844
  );
1813
1845
  }
1846
+ wsClient.on("ChannelDestroyed", (data) => {
1847
+ if (data.type === "ChannelDestroyed" && "channel" in data) {
1848
+ console.log(`[${app}] Canal destru\xEDdo: ${data.channel.id}`);
1849
+ this.removeChannel(data.channel.id);
1850
+ }
1851
+ });
1852
+ wsClient.on("StasisEnd", (data) => {
1853
+ if (data.type === "StasisEnd" && "channel" in data) {
1854
+ console.log(`[${app}] StasisEnd para o canal: ${data.channel.id}`);
1855
+ this.removeChannel(data.channel.id);
1856
+ }
1857
+ });
1814
1858
  const eventHandlers = {
1815
1859
  PlaybackStarted: (data) => {
1816
1860
  if ("playbackId" in data) {
@@ -1847,10 +1891,11 @@ var AriClient = class {
1847
1891
  }
1848
1892
  /**
1849
1893
  * Registra um listener para eventos globais.
1894
+ * @param eventType
1850
1895
  * @param callback - A função a ser executada quando um evento global for recebido.
1851
1896
  */
1852
- onGlobalEvent(callback) {
1853
- this.eventEmitter.on("globalEvent", callback);
1897
+ onGlobalEvent(eventType, callback) {
1898
+ this.eventEmitter.on(eventType, callback);
1854
1899
  }
1855
1900
  /**
1856
1901
  * Remove um listener para eventos globais.
@@ -2047,6 +2092,69 @@ var AriClient = class {
2047
2092
  this.wsClient = null;
2048
2093
  }
2049
2094
  }
2095
+ /**
2096
+ * Obtém ou cria um emissor de eventos para o canal especificado.
2097
+ */
2098
+ getOrCreateChannelEmitter(channel) {
2099
+ if (!this.channelEmitters.has(channel.id)) {
2100
+ this.channelEmitters.set(channel.id, new ChannelEventEmitter(channel));
2101
+ }
2102
+ return this.channelEmitters.get(channel.id);
2103
+ }
2104
+ handleChannelEvent(event) {
2105
+ if ("channel" in event && event.channel) {
2106
+ const channelEmitter = this.channelEmitters.get(event.channel.id);
2107
+ if (channelEmitter) {
2108
+ channelEmitter.emit(event.type, event);
2109
+ } else {
2110
+ console.warn(
2111
+ `Nenhum listener registrado para o canal ${event.channel.id}`
2112
+ );
2113
+ }
2114
+ }
2115
+ }
2116
+ registerGlobalWebSocketListener() {
2117
+ this.wsClient?.on("message", (event) => {
2118
+ if ("channel" in event && event.channel) {
2119
+ this.handleChannelEvent(event);
2120
+ } else {
2121
+ this.eventEmitter.emit(event.type, event);
2122
+ }
2123
+ });
2124
+ }
2125
+ /**
2126
+ * Adiciona um listener para um evento específico de canal.
2127
+ */
2128
+ onChannelEvent(channelId, eventType, callback) {
2129
+ const channelEmitter = this.channelEmitters.get(channelId);
2130
+ if (!channelEmitter) {
2131
+ throw new Error(`Canal com ID ${channelId} n\xE3o encontrado.`);
2132
+ }
2133
+ channelEmitter.on(eventType, callback);
2134
+ console.log(
2135
+ `Listener registrado para o canal ${channelId} e evento ${eventType}`
2136
+ );
2137
+ }
2138
+ removeChannel(channelId) {
2139
+ const channelEmitter = this.channelEmitters.get(channelId);
2140
+ if (channelEmitter) {
2141
+ channelEmitter.removeAllListeners();
2142
+ this.channelEmitters.delete(channelId);
2143
+ console.log(`Listeners para o canal ${channelId} foram removidos.`);
2144
+ } else {
2145
+ console.warn(`Nenhum listener encontrado para o canal ${channelId}.`);
2146
+ }
2147
+ }
2148
+ /**
2149
+ * Remove um listener para um evento específico de canal.
2150
+ */
2151
+ offChannelEvent(channelId, eventType, callback) {
2152
+ const channelEmitter = this.channelEmitters.get(channelId);
2153
+ if (!channelEmitter) {
2154
+ throw new Error(`Canal com ID ${channelId} n\xE3o encontrado.`);
2155
+ }
2156
+ channelEmitter.off(eventType, callback);
2157
+ }
2050
2158
  /**
2051
2159
  * Retrieves a list of active channels from the Asterisk ARI.
2052
2160
  *