@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.
package/dist/esm/index.js CHANGED
@@ -842,6 +842,37 @@ function toQueryParams2(options) {
842
842
  Object.entries(options).filter(([, value]) => value !== void 0).map(([key, value]) => [key, value])
843
843
  ).toString();
844
844
  }
845
+ var ChannelEventEmitter = class {
846
+ eventEmitter = new EventEmitter();
847
+ channel;
848
+ constructor(channel) {
849
+ this.channel = channel;
850
+ }
851
+ /**
852
+ * Registra um listener para um evento no canal.
853
+ */
854
+ on(eventType, callback) {
855
+ this.eventEmitter.on(eventType, callback);
856
+ }
857
+ /**
858
+ * Emite um evento no canal.
859
+ */
860
+ emit(eventType, data) {
861
+ this.eventEmitter.emit(eventType, data);
862
+ }
863
+ /**
864
+ * Remove um listener de evento do canal.
865
+ */
866
+ off(eventType, callback) {
867
+ this.eventEmitter.off(eventType, callback);
868
+ }
869
+ /**
870
+ * Remove todos os listeners de um evento no canal.
871
+ */
872
+ removeAllListeners(eventType) {
873
+ this.eventEmitter.removeAllListeners(eventType);
874
+ }
875
+ };
845
876
  var ChannelInstance = class extends EventEmitter {
846
877
  constructor(client, baseClient, channelId = `channel-${Date.now()}`) {
847
878
  super();
@@ -1673,6 +1704,7 @@ var AriClient = class {
1673
1704
  this.bridges = new Bridges(this.baseClient);
1674
1705
  }
1675
1706
  wsClient = null;
1707
+ channelEmitters = /* @__PURE__ */ new Map();
1676
1708
  baseClient;
1677
1709
  isReconnecting = false;
1678
1710
  eventEmitter = new EventEmitter4();
@@ -1789,6 +1821,18 @@ var AriClient = class {
1789
1821
  `WebSocket client para o app '${app}' n\xE3o est\xE1 conectado.`
1790
1822
  );
1791
1823
  }
1824
+ wsClient.on("ChannelDestroyed", (data) => {
1825
+ if (data.type === "ChannelDestroyed" && "channel" in data) {
1826
+ console.log(`[${app}] Canal destru\xEDdo: ${data.channel.id}`);
1827
+ this.removeChannel(data.channel.id);
1828
+ }
1829
+ });
1830
+ wsClient.on("StasisEnd", (data) => {
1831
+ if (data.type === "StasisEnd" && "channel" in data) {
1832
+ console.log(`[${app}] StasisEnd para o canal: ${data.channel.id}`);
1833
+ this.removeChannel(data.channel.id);
1834
+ }
1835
+ });
1792
1836
  const eventHandlers = {
1793
1837
  PlaybackStarted: (data) => {
1794
1838
  if ("playbackId" in data) {
@@ -1825,10 +1869,11 @@ var AriClient = class {
1825
1869
  }
1826
1870
  /**
1827
1871
  * Registra um listener para eventos globais.
1872
+ * @param eventType
1828
1873
  * @param callback - A função a ser executada quando um evento global for recebido.
1829
1874
  */
1830
- onGlobalEvent(callback) {
1831
- this.eventEmitter.on("globalEvent", callback);
1875
+ onGlobalEvent(eventType, callback) {
1876
+ this.eventEmitter.on(eventType, callback);
1832
1877
  }
1833
1878
  /**
1834
1879
  * Remove um listener para eventos globais.
@@ -2025,6 +2070,69 @@ var AriClient = class {
2025
2070
  this.wsClient = null;
2026
2071
  }
2027
2072
  }
2073
+ /**
2074
+ * Obtém ou cria um emissor de eventos para o canal especificado.
2075
+ */
2076
+ getOrCreateChannelEmitter(channel) {
2077
+ if (!this.channelEmitters.has(channel.id)) {
2078
+ this.channelEmitters.set(channel.id, new ChannelEventEmitter(channel));
2079
+ }
2080
+ return this.channelEmitters.get(channel.id);
2081
+ }
2082
+ handleChannelEvent(event) {
2083
+ if ("channel" in event && event.channel) {
2084
+ const channelEmitter = this.channelEmitters.get(event.channel.id);
2085
+ if (channelEmitter) {
2086
+ channelEmitter.emit(event.type, event);
2087
+ } else {
2088
+ console.warn(
2089
+ `Nenhum listener registrado para o canal ${event.channel.id}`
2090
+ );
2091
+ }
2092
+ }
2093
+ }
2094
+ registerGlobalWebSocketListener() {
2095
+ this.wsClient?.on("message", (event) => {
2096
+ if ("channel" in event && event.channel) {
2097
+ this.handleChannelEvent(event);
2098
+ } else {
2099
+ this.eventEmitter.emit(event.type, event);
2100
+ }
2101
+ });
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
+ console.log(
2113
+ `Listener registrado para o canal ${channelId} e evento ${eventType}`
2114
+ );
2115
+ }
2116
+ removeChannel(channelId) {
2117
+ const channelEmitter = this.channelEmitters.get(channelId);
2118
+ if (channelEmitter) {
2119
+ channelEmitter.removeAllListeners();
2120
+ this.channelEmitters.delete(channelId);
2121
+ console.log(`Listeners para o canal ${channelId} foram removidos.`);
2122
+ } else {
2123
+ console.warn(`Nenhum listener encontrado para o canal ${channelId}.`);
2124
+ }
2125
+ }
2126
+ /**
2127
+ * Remove um listener para um evento específico de canal.
2128
+ */
2129
+ offChannelEvent(channelId, eventType, callback) {
2130
+ const channelEmitter = this.channelEmitters.get(channelId);
2131
+ if (!channelEmitter) {
2132
+ throw new Error(`Canal com ID ${channelId} n\xE3o encontrado.`);
2133
+ }
2134
+ channelEmitter.off(eventType, callback);
2135
+ }
2028
2136
  /**
2029
2137
  * Retrieves a list of active channels from the Asterisk ARI.
2030
2138
  *