@ipcom/asterisk-ari 0.0.119 → 0.0.120

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
@@ -1939,7 +1939,6 @@ var WebSocketClient = class extends EventEmitter3 {
1939
1939
  handleMessage(rawData) {
1940
1940
  try {
1941
1941
  const decodedData = JSON.parse(rawData.toString());
1942
- console.log({ tipo: decodedData?.type });
1943
1942
  if (decodedData?.type && decodedData?.application) {
1944
1943
  const scopedEvent = `${decodedData.application}:${decodedData.type}`;
1945
1944
  if ("channel" in decodedData && decodedData.channel?.id) {
@@ -2011,6 +2010,7 @@ var AriClient = class {
2011
2010
  this.asterisk = new Asterisk(this.baseClient);
2012
2011
  this.bridges = new Bridges(this.baseClient);
2013
2012
  }
2013
+ scopedListeners = /* @__PURE__ */ new Map();
2014
2014
  eventListeners = /* @__PURE__ */ new Map();
2015
2015
  wsClients = /* @__PURE__ */ new Map();
2016
2016
  // Map para armazenar conexões por app
@@ -2061,49 +2061,53 @@ var AriClient = class {
2061
2061
  getWebSocketClients() {
2062
2062
  return this.wsClients;
2063
2063
  }
2064
+ addScopedListener(app, eventType, instanceId, callback) {
2065
+ const key = `${app}:${instanceId}:${eventType}`;
2066
+ if (this.scopedListeners.has(key)) {
2067
+ console.warn(`Listener escopado j\xE1 registrado para '${key}'.`);
2068
+ return;
2069
+ }
2070
+ const scopedListener = (data) => {
2071
+ 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)) {
2072
+ callback(data);
2073
+ }
2074
+ };
2075
+ this.scopedListeners.set(key, scopedListener);
2076
+ const wsClient = this.wsClients.get(app);
2077
+ wsClient?.on(eventType, scopedListener);
2078
+ console.log(`Listener escopado adicionado para '${key}'.`);
2079
+ }
2080
+ removeScopedListener(app, eventType, instanceId) {
2081
+ const key = `${app}:${instanceId}:${eventType}`;
2082
+ const scopedListener = this.scopedListeners.get(key);
2083
+ if (scopedListener) {
2084
+ const wsClient = this.wsClients.get(app);
2085
+ wsClient?.off(eventType, scopedListener);
2086
+ this.scopedListeners.delete(key);
2087
+ console.log(`Listener escopado removido para '${key}'.`);
2088
+ }
2089
+ }
2064
2090
  /**
2065
2091
  * Registra listeners globais para eventos de WebSocket.
2066
2092
  */
2067
2093
  on(eventType, callback, app) {
2068
2094
  if (!app) {
2069
2095
  throw new Error(
2070
- "O nome do app \xE9 obrigat\xF3rio para registrar um listener de evento."
2096
+ "O nome do app \xE9 obrigat\xF3rio para registrar um listener."
2071
2097
  );
2072
2098
  }
2073
- console.log(
2074
- `Registrando listener para evento '${eventType}' no app '${app}'.`
2075
- );
2076
2099
  const callbackKey = `${app}:${eventType}`;
2077
2100
  if (this.eventListeners.has(callbackKey)) {
2078
- console.log(
2079
- `Listener para evento '${eventType}' j\xE1 est\xE1 registrado no app '${app}'. Ignorando duplicata.`
2080
- );
2101
+ console.log(`Listener j\xE1 registrado para '${callbackKey}'. Ignorando.`);
2081
2102
  return;
2082
2103
  }
2083
- const wrappedCallback = (event) => {
2084
- if (isChannelEvent(event)) {
2085
- const channelId = event.channel.id;
2086
- if (channelId) {
2087
- if (!this.channelInstances.has(channelId)) {
2088
- const channelInstance = this.createChannelInstance(channelId, app);
2089
- this.channelInstances.set(channelId, channelInstance);
2090
- }
2091
- event.instanceChannel = this.channelInstances.get(channelId);
2092
- event.instanceChannel?.emit(event.type, event);
2093
- }
2094
- }
2095
- callback(event);
2096
- };
2097
2104
  const wsClient = this.wsClients.get(app);
2098
2105
  if (wsClient) {
2099
- const scopedEvent = `${app}:${eventType}`;
2100
- wsClient.on(scopedEvent, wrappedCallback);
2101
- this.eventListeners.set(callbackKey, wrappedCallback);
2102
- console.log(
2103
- `Listener para evento '${eventType}' registrado com sucesso no app '${app}'.`
2104
- );
2106
+ wsClient.on(eventType, callback);
2107
+ this.eventListeners.set(callbackKey, callback);
2108
+ console.log(`Listener registrado para '${callbackKey}'.`);
2105
2109
  } else {
2106
- console.warn(`WebSocket para o app '${app}' n\xE3o est\xE1 conectado.`);
2110
+ console.warn(`WebSocket para '${app}' n\xE3o est\xE1 conectado.`);
2107
2111
  }
2108
2112
  }
2109
2113
  removeListener(eventType, app) {