@ipcom/asterisk-ari 0.0.132 → 0.0.133

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
  */
@@ -2021,25 +1988,22 @@ var WebSocketClient = class extends import_events3.EventEmitter {
2021
1988
  const key = `${app}:${instanceId}`;
2022
1989
  if (this.scopedListeners.has(key)) {
2023
1990
  console.warn(
2024
- `Listener escopado j\xE1 existe para '${key}'. Removendo antigo.`
1991
+ `Listener escopado j\xE1 existe para '${key}'. Removendo antigo antes de adicionar um novo.`
2025
1992
  );
2026
- const existingListener = this.scopedListeners.get(key);
2027
- this.off("message", existingListener);
2028
- this.scopedListeners.delete(key);
1993
+ this.removeScopedMessageListeners(instanceId, app);
2029
1994
  }
2030
1995
  const scopedListener = (data) => {
2031
1996
  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
1997
  callback(data);
2033
1998
  }
2034
1999
  };
2035
- this.on("message", scopedListener);
2036
2000
  this.scopedListeners.set(key, scopedListener);
2001
+ this.on("message", scopedListener);
2037
2002
  console.log(
2038
2003
  `Listener escopado adicionado para '${key}'. Total de listeners: ${this.listenerCount(
2039
2004
  "message"
2040
2005
  )}`
2041
2006
  );
2042
- this.listListeners("message");
2043
2007
  }
2044
2008
  /**
2045
2009
  * Remove todos os listeners associados a uma instância específica.
@@ -2049,19 +2013,20 @@ var WebSocketClient = class extends import_events3.EventEmitter {
2049
2013
  removeScopedMessageListeners(instanceId, app) {
2050
2014
  const key = `${app}:${instanceId}`;
2051
2015
  const scopedListener = this.scopedListeners.get(key);
2052
- if (!scopedListener) {
2053
- console.warn(`Nenhum listener encontrado para a inst\xE2ncia '${key}'.`);
2054
- return;
2016
+ if (scopedListener) {
2017
+ console.log(`Removendo listener escopado para '${key}'.`);
2018
+ this.off("message", scopedListener);
2019
+ this.scopedListeners.delete(key);
2020
+ } else {
2021
+ console.warn(`Nenhum listener encontrado para '${key}'.`);
2055
2022
  }
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");
2023
+ }
2024
+ removeAllScopedListeners() {
2025
+ this.scopedListeners.forEach((_listener, key) => {
2026
+ console.log(`Removendo listener escopado para '${key}' durante cleanup.`);
2027
+ this.removeScopedMessageListeners(key.split(":")[1], key.split(":")[0]);
2028
+ });
2029
+ console.log("Todos os listeners escopados foram removidos.");
2065
2030
  }
2066
2031
  removeScopedChannelListeners(instanceId, app) {
2067
2032
  const key = `${app}:${instanceId}`;