@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.
@@ -1961,7 +1961,6 @@ var WebSocketClient = class extends import_events3.EventEmitter {
1961
1961
  handleMessage(rawData) {
1962
1962
  try {
1963
1963
  const decodedData = JSON.parse(rawData.toString());
1964
- console.log({ tipo: decodedData?.type });
1965
1964
  if (decodedData?.type && decodedData?.application) {
1966
1965
  const scopedEvent = `${decodedData.application}:${decodedData.type}`;
1967
1966
  if ("channel" in decodedData && decodedData.channel?.id) {
@@ -2033,6 +2032,7 @@ var AriClient = class {
2033
2032
  this.asterisk = new Asterisk(this.baseClient);
2034
2033
  this.bridges = new Bridges(this.baseClient);
2035
2034
  }
2035
+ scopedListeners = /* @__PURE__ */ new Map();
2036
2036
  eventListeners = /* @__PURE__ */ new Map();
2037
2037
  wsClients = /* @__PURE__ */ new Map();
2038
2038
  // Map para armazenar conexões por app
@@ -2083,49 +2083,53 @@ var AriClient = class {
2083
2083
  getWebSocketClients() {
2084
2084
  return this.wsClients;
2085
2085
  }
2086
+ addScopedListener(app, eventType, instanceId, callback) {
2087
+ const key = `${app}:${instanceId}:${eventType}`;
2088
+ if (this.scopedListeners.has(key)) {
2089
+ console.warn(`Listener escopado j\xE1 registrado para '${key}'.`);
2090
+ return;
2091
+ }
2092
+ const scopedListener = (data) => {
2093
+ 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)) {
2094
+ callback(data);
2095
+ }
2096
+ };
2097
+ this.scopedListeners.set(key, scopedListener);
2098
+ const wsClient = this.wsClients.get(app);
2099
+ wsClient?.on(eventType, scopedListener);
2100
+ console.log(`Listener escopado adicionado para '${key}'.`);
2101
+ }
2102
+ removeScopedListener(app, eventType, instanceId) {
2103
+ const key = `${app}:${instanceId}:${eventType}`;
2104
+ const scopedListener = this.scopedListeners.get(key);
2105
+ if (scopedListener) {
2106
+ const wsClient = this.wsClients.get(app);
2107
+ wsClient?.off(eventType, scopedListener);
2108
+ this.scopedListeners.delete(key);
2109
+ console.log(`Listener escopado removido para '${key}'.`);
2110
+ }
2111
+ }
2086
2112
  /**
2087
2113
  * Registra listeners globais para eventos de WebSocket.
2088
2114
  */
2089
2115
  on(eventType, callback, app) {
2090
2116
  if (!app) {
2091
2117
  throw new Error(
2092
- "O nome do app \xE9 obrigat\xF3rio para registrar um listener de evento."
2118
+ "O nome do app \xE9 obrigat\xF3rio para registrar um listener."
2093
2119
  );
2094
2120
  }
2095
- console.log(
2096
- `Registrando listener para evento '${eventType}' no app '${app}'.`
2097
- );
2098
2121
  const callbackKey = `${app}:${eventType}`;
2099
2122
  if (this.eventListeners.has(callbackKey)) {
2100
- console.log(
2101
- `Listener para evento '${eventType}' j\xE1 est\xE1 registrado no app '${app}'. Ignorando duplicata.`
2102
- );
2123
+ console.log(`Listener j\xE1 registrado para '${callbackKey}'. Ignorando.`);
2103
2124
  return;
2104
2125
  }
2105
- const wrappedCallback = (event) => {
2106
- if (isChannelEvent(event)) {
2107
- const channelId = event.channel.id;
2108
- if (channelId) {
2109
- if (!this.channelInstances.has(channelId)) {
2110
- const channelInstance = this.createChannelInstance(channelId, app);
2111
- this.channelInstances.set(channelId, channelInstance);
2112
- }
2113
- event.instanceChannel = this.channelInstances.get(channelId);
2114
- event.instanceChannel?.emit(event.type, event);
2115
- }
2116
- }
2117
- callback(event);
2118
- };
2119
2126
  const wsClient = this.wsClients.get(app);
2120
2127
  if (wsClient) {
2121
- const scopedEvent = `${app}:${eventType}`;
2122
- wsClient.on(scopedEvent, wrappedCallback);
2123
- this.eventListeners.set(callbackKey, wrappedCallback);
2124
- console.log(
2125
- `Listener para evento '${eventType}' registrado com sucesso no app '${app}'.`
2126
- );
2128
+ wsClient.on(eventType, callback);
2129
+ this.eventListeners.set(callbackKey, callback);
2130
+ console.log(`Listener registrado para '${callbackKey}'.`);
2127
2131
  } else {
2128
- console.warn(`WebSocket para o app '${app}' n\xE3o est\xE1 conectado.`);
2132
+ console.warn(`WebSocket para '${app}' n\xE3o est\xE1 conectado.`);
2129
2133
  }
2130
2134
  }
2131
2135
  removeListener(eventType, app) {