@ipcom/asterisk-ari 0.0.132 → 0.0.134

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.
@@ -948,54 +948,21 @@ var ChannelInstance = class extends import_events.EventEmitter {
948
948
  }
949
949
  );
950
950
  this.once("ChannelDestroyed", () => {
951
- console.log(`Removendo listeners associados ao canal ${this.id}`);
952
- this.removeAllScopedListeners();
951
+ console.log(
952
+ `Canal '${this.id}' destru\xEDdo. Removendo listeners associados.`
953
+ );
954
+ const wsClient2 = this.client.getWebSocketClients().get(this.app);
955
+ if (wsClient2) {
956
+ wsClient2.removeScopedChannelListeners(this.id, this.app);
957
+ } else {
958
+ console.warn(
959
+ `WebSocketClient n\xE3o encontrado para o app '${this.app}'.`
960
+ );
961
+ }
953
962
  });
954
963
  }
955
- scopedListeners = /* @__PURE__ */ new Map();
956
964
  channelData = null;
957
965
  id;
958
- addScopedListener(event, callback) {
959
- const scopedEvent = `${this.app}:Channel:${this.id}:${event}`;
960
- const wsClient = this.client.getWebSocketClients().get(this.app);
961
- if (!wsClient) {
962
- throw new Error(
963
- `Nenhum WebSocket conectado dispon\xEDvel para '${this.app}'.`
964
- );
965
- }
966
- if (!this.scopedListeners.has(scopedEvent)) {
967
- this.scopedListeners.set(scopedEvent, callback);
968
- wsClient.on(scopedEvent, callback);
969
- } else {
970
- console.warn(`Listener j\xE1 registrado para o evento '${scopedEvent}'.`);
971
- }
972
- }
973
- removeScopedListener(event) {
974
- const scopedEvent = `${this.app}:Channel:${this.id}:${event}`;
975
- const wsClient = this.client.getWebSocketClients().get(this.app);
976
- if (!wsClient) {
977
- throw new Error(
978
- `Nenhum WebSocket conectado dispon\xEDvel para '${this.app}'.`
979
- );
980
- }
981
- const callback = this.scopedListeners.get(scopedEvent);
982
- if (callback) {
983
- wsClient.off(scopedEvent, callback);
984
- this.scopedListeners.delete(scopedEvent);
985
- }
986
- }
987
- removeAllScopedListeners() {
988
- const wsClient = this.client.getWebSocketClients().get(this.app);
989
- if (!wsClient) {
990
- throw new Error(
991
- `Nenhum WebSocket conectado dispon\xEDvel para '${this.app}'.`
992
- );
993
- }
994
- for (const [scopedEvent, callback] of this.scopedListeners.entries()) {
995
- wsClient.off(scopedEvent, callback);
996
- }
997
- this.scopedListeners.clear();
998
- }
999
966
  /**
1000
967
  * Adiciona um listener para eventos de canal.
1001
968
  */
@@ -1137,7 +1104,6 @@ var ChannelInstance = class extends import_events.EventEmitter {
1137
1104
  console.log(
1138
1105
  `Canal '${this.id}' foi destru\xEDdo. Removendo listeners associados.`
1139
1106
  );
1140
- wsClient.removeScopedChannelListeners(this.id, this.app);
1141
1107
  wsClient.removeScopedPlaybackListeners(playback.id, this.app);
1142
1108
  });
1143
1109
  return playback;
@@ -2021,25 +1987,22 @@ var WebSocketClient = class extends import_events3.EventEmitter {
2021
1987
  const key = `${app}:${instanceId}`;
2022
1988
  if (this.scopedListeners.has(key)) {
2023
1989
  console.warn(
2024
- `Listener escopado j\xE1 existe para '${key}'. Removendo antigo.`
1990
+ `Listener escopado j\xE1 existe para '${key}'. Removendo antigo antes de adicionar um novo.`
2025
1991
  );
2026
- const existingListener = this.scopedListeners.get(key);
2027
- this.off("message", existingListener);
2028
- this.scopedListeners.delete(key);
1992
+ this.removeScopedMessageListeners(instanceId, app);
2029
1993
  }
2030
1994
  const scopedListener = (data) => {
2031
1995
  if (data.application === app && (data.type.startsWith("Channel") && "channel" in data && data.channel?.id === instanceId || data.type.startsWith("Playback") && "playbackId" in data && data.playbackId === instanceId)) {
2032
1996
  callback(data);
2033
1997
  }
2034
1998
  };
2035
- this.on("message", scopedListener);
2036
1999
  this.scopedListeners.set(key, scopedListener);
2000
+ this.on("message", scopedListener);
2037
2001
  console.log(
2038
2002
  `Listener escopado adicionado para '${key}'. Total de listeners: ${this.listenerCount(
2039
2003
  "message"
2040
2004
  )}`
2041
2005
  );
2042
- this.listListeners("message");
2043
2006
  }
2044
2007
  /**
2045
2008
  * Remove todos os listeners associados a uma instância específica.
@@ -2049,19 +2012,20 @@ var WebSocketClient = class extends import_events3.EventEmitter {
2049
2012
  removeScopedMessageListeners(instanceId, app) {
2050
2013
  const key = `${app}:${instanceId}`;
2051
2014
  const scopedListener = this.scopedListeners.get(key);
2052
- if (!scopedListener) {
2053
- console.warn(`Nenhum listener encontrado para a inst\xE2ncia '${key}'.`);
2054
- return;
2015
+ if (scopedListener) {
2016
+ console.log(`Removendo listener escopado para '${key}'.`);
2017
+ this.off("message", scopedListener);
2018
+ this.scopedListeners.delete(key);
2019
+ } else {
2020
+ console.warn(`Nenhum listener encontrado para '${key}'.`);
2055
2021
  }
2056
- console.log(`Removendo listener escopado para '${key}'.`);
2057
- this.off("message", scopedListener);
2058
- this.scopedListeners.delete(key);
2059
- console.log(
2060
- `Listeners escopados removidos para '${key}'. Total de listeners restantes: ${this.listenerCount(
2061
- "message"
2062
- )}`
2063
- );
2064
- this.listListeners("message");
2022
+ }
2023
+ removeAllScopedListeners() {
2024
+ this.scopedListeners.forEach((_listener, key) => {
2025
+ console.log(`Removendo listener escopado para '${key}' durante cleanup.`);
2026
+ this.removeScopedMessageListeners(key.split(":")[1], key.split(":")[0]);
2027
+ });
2028
+ console.log("Todos os listeners escopados foram removidos.");
2065
2029
  }
2066
2030
  removeScopedChannelListeners(instanceId, app) {
2067
2031
  const key = `${app}:${instanceId}`;