@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.
package/dist/esm/index.js CHANGED
@@ -926,54 +926,21 @@ var ChannelInstance = class extends EventEmitter {
926
926
  }
927
927
  );
928
928
  this.once("ChannelDestroyed", () => {
929
- console.log(`Removendo listeners associados ao canal ${this.id}`);
930
- this.removeAllScopedListeners();
929
+ console.log(
930
+ `Canal '${this.id}' destru\xEDdo. Removendo listeners associados.`
931
+ );
932
+ const wsClient2 = this.client.getWebSocketClients().get(this.app);
933
+ if (wsClient2) {
934
+ wsClient2.removeScopedChannelListeners(this.id, this.app);
935
+ } else {
936
+ console.warn(
937
+ `WebSocketClient n\xE3o encontrado para o app '${this.app}'.`
938
+ );
939
+ }
931
940
  });
932
941
  }
933
- scopedListeners = /* @__PURE__ */ new Map();
934
942
  channelData = null;
935
943
  id;
936
- addScopedListener(event, callback) {
937
- const scopedEvent = `${this.app}:Channel:${this.id}:${event}`;
938
- const wsClient = this.client.getWebSocketClients().get(this.app);
939
- if (!wsClient) {
940
- throw new Error(
941
- `Nenhum WebSocket conectado dispon\xEDvel para '${this.app}'.`
942
- );
943
- }
944
- if (!this.scopedListeners.has(scopedEvent)) {
945
- this.scopedListeners.set(scopedEvent, callback);
946
- wsClient.on(scopedEvent, callback);
947
- } else {
948
- console.warn(`Listener j\xE1 registrado para o evento '${scopedEvent}'.`);
949
- }
950
- }
951
- removeScopedListener(event) {
952
- const scopedEvent = `${this.app}:Channel:${this.id}:${event}`;
953
- const wsClient = this.client.getWebSocketClients().get(this.app);
954
- if (!wsClient) {
955
- throw new Error(
956
- `Nenhum WebSocket conectado dispon\xEDvel para '${this.app}'.`
957
- );
958
- }
959
- const callback = this.scopedListeners.get(scopedEvent);
960
- if (callback) {
961
- wsClient.off(scopedEvent, callback);
962
- this.scopedListeners.delete(scopedEvent);
963
- }
964
- }
965
- removeAllScopedListeners() {
966
- const wsClient = this.client.getWebSocketClients().get(this.app);
967
- if (!wsClient) {
968
- throw new Error(
969
- `Nenhum WebSocket conectado dispon\xEDvel para '${this.app}'.`
970
- );
971
- }
972
- for (const [scopedEvent, callback] of this.scopedListeners.entries()) {
973
- wsClient.off(scopedEvent, callback);
974
- }
975
- this.scopedListeners.clear();
976
- }
977
944
  /**
978
945
  * Adiciona um listener para eventos de canal.
979
946
  */
@@ -1115,7 +1082,6 @@ var ChannelInstance = class extends EventEmitter {
1115
1082
  console.log(
1116
1083
  `Canal '${this.id}' foi destru\xEDdo. Removendo listeners associados.`
1117
1084
  );
1118
- wsClient.removeScopedChannelListeners(this.id, this.app);
1119
1085
  wsClient.removeScopedPlaybackListeners(playback.id, this.app);
1120
1086
  });
1121
1087
  return playback;
@@ -1999,25 +1965,22 @@ var WebSocketClient = class extends EventEmitter3 {
1999
1965
  const key = `${app}:${instanceId}`;
2000
1966
  if (this.scopedListeners.has(key)) {
2001
1967
  console.warn(
2002
- `Listener escopado j\xE1 existe para '${key}'. Removendo antigo.`
1968
+ `Listener escopado j\xE1 existe para '${key}'. Removendo antigo antes de adicionar um novo.`
2003
1969
  );
2004
- const existingListener = this.scopedListeners.get(key);
2005
- this.off("message", existingListener);
2006
- this.scopedListeners.delete(key);
1970
+ this.removeScopedMessageListeners(instanceId, app);
2007
1971
  }
2008
1972
  const scopedListener = (data) => {
2009
1973
  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)) {
2010
1974
  callback(data);
2011
1975
  }
2012
1976
  };
2013
- this.on("message", scopedListener);
2014
1977
  this.scopedListeners.set(key, scopedListener);
1978
+ this.on("message", scopedListener);
2015
1979
  console.log(
2016
1980
  `Listener escopado adicionado para '${key}'. Total de listeners: ${this.listenerCount(
2017
1981
  "message"
2018
1982
  )}`
2019
1983
  );
2020
- this.listListeners("message");
2021
1984
  }
2022
1985
  /**
2023
1986
  * Remove todos os listeners associados a uma instância específica.
@@ -2027,19 +1990,20 @@ var WebSocketClient = class extends EventEmitter3 {
2027
1990
  removeScopedMessageListeners(instanceId, app) {
2028
1991
  const key = `${app}:${instanceId}`;
2029
1992
  const scopedListener = this.scopedListeners.get(key);
2030
- if (!scopedListener) {
2031
- console.warn(`Nenhum listener encontrado para a inst\xE2ncia '${key}'.`);
2032
- return;
1993
+ if (scopedListener) {
1994
+ console.log(`Removendo listener escopado para '${key}'.`);
1995
+ this.off("message", scopedListener);
1996
+ this.scopedListeners.delete(key);
1997
+ } else {
1998
+ console.warn(`Nenhum listener encontrado para '${key}'.`);
2033
1999
  }
2034
- console.log(`Removendo listener escopado para '${key}'.`);
2035
- this.off("message", scopedListener);
2036
- this.scopedListeners.delete(key);
2037
- console.log(
2038
- `Listeners escopados removidos para '${key}'. Total de listeners restantes: ${this.listenerCount(
2039
- "message"
2040
- )}`
2041
- );
2042
- this.listListeners("message");
2000
+ }
2001
+ removeAllScopedListeners() {
2002
+ this.scopedListeners.forEach((_listener, key) => {
2003
+ console.log(`Removendo listener escopado para '${key}' durante cleanup.`);
2004
+ this.removeScopedMessageListeners(key.split(":")[1], key.split(":")[0]);
2005
+ });
2006
+ console.log("Todos os listeners escopados foram removidos.");
2043
2007
  }
2044
2008
  removeScopedChannelListeners(instanceId, app) {
2045
2009
  const key = `${app}:${instanceId}`;