@ipcom/asterisk-ari 0.0.40 → 0.0.41

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) {
@@ -2047,6 +2091,45 @@ var AriClient = class {
2047
2091
  this.wsClient = null;
2048
2092
  }
2049
2093
  }
2094
+ /**
2095
+ * Obtém ou cria um emissor de eventos para o canal especificado.
2096
+ */
2097
+ getOrCreateChannelEmitter(channel) {
2098
+ if (!this.channelEmitters.has(channel.id)) {
2099
+ this.channelEmitters.set(channel.id, new ChannelEventEmitter(channel));
2100
+ }
2101
+ return this.channelEmitters.get(channel.id);
2102
+ }
2103
+ /**
2104
+ * Adiciona um listener para um evento específico de canal.
2105
+ */
2106
+ onChannelEvent(channelId, eventType, callback) {
2107
+ const channelEmitter = this.channelEmitters.get(channelId);
2108
+ if (!channelEmitter) {
2109
+ throw new Error(`Canal com ID ${channelId} n\xE3o encontrado.`);
2110
+ }
2111
+ channelEmitter.on(eventType, callback);
2112
+ }
2113
+ removeChannel(channelId) {
2114
+ const channelEmitter = this.channelEmitters.get(channelId);
2115
+ if (channelEmitter) {
2116
+ channelEmitter.removeAllListeners();
2117
+ this.channelEmitters.delete(channelId);
2118
+ console.log(`Listeners para o canal ${channelId} foram removidos.`);
2119
+ } else {
2120
+ console.warn(`Nenhum listener encontrado para o canal ${channelId}.`);
2121
+ }
2122
+ }
2123
+ /**
2124
+ * Remove um listener para um evento específico de canal.
2125
+ */
2126
+ offChannelEvent(channelId, eventType, callback) {
2127
+ const channelEmitter = this.channelEmitters.get(channelId);
2128
+ if (!channelEmitter) {
2129
+ throw new Error(`Canal com ID ${channelId} n\xE3o encontrado.`);
2130
+ }
2131
+ channelEmitter.off(eventType, callback);
2132
+ }
2050
2133
  /**
2051
2134
  * Retrieves a list of active channels from the Asterisk ARI.
2052
2135
  *