@ipcom/asterisk-ari 0.0.105 → 0.0.107

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.
@@ -977,54 +977,47 @@ var ChannelInstance = class extends import_events2.EventEmitter {
977
977
  * Adiciona um listener para eventos de canal.
978
978
  */
979
979
  on(event, callback) {
980
- super.on(event, callback);
980
+ const wsClient = this.client.getWebSocketClients().get(this.app);
981
+ if (!wsClient) {
982
+ throw new Error(
983
+ `Nenhum WebSocket conectado dispon\xEDvel para '${this.app}'.`
984
+ );
985
+ }
986
+ const scopedEvent = `${this.app}:Channel:${this.id}:${event}`;
987
+ wsClient.on(scopedEvent, callback);
981
988
  return this;
982
989
  }
983
990
  /**
984
991
  * Adiciona um listener para ser chamado apenas uma vez.
985
992
  */
986
993
  once(event, callback) {
987
- console.log({
988
- type: "Adiciona um listener para ser chamado apenas uma vez",
989
- event
990
- });
991
- const wsClients = this.client.getWebSocketClients();
992
- const wsClient = Array.from(wsClients.values()).find(
993
- (client) => client.isConnected()
994
- );
994
+ const wsClient = this.client.getWebSocketClients().get(this.app);
995
995
  if (!wsClient) {
996
996
  throw new Error(
997
- `Nenhum WebSocket conectado dispon\xEDvel para '${this.id}'.`
997
+ `Nenhum WebSocket conectado dispon\xEDvel para '${this.app}'.`
998
998
  );
999
999
  }
1000
- const globalListener = (webSocketEvent) => {
1001
- if (isChannelEvent(webSocketEvent, this.id) && webSocketEvent.type === event) {
1002
- console.log(`Recebido evento '${event}' para canal '${this.id}'`);
1003
- this.emit(event, webSocketEvent);
1004
- callback(webSocketEvent);
1005
- wsClient.removeScopedMessageListeners(this.id, this.app);
1006
- console.log(
1007
- `Listeners escopados para canal '${this.id}' foram removidos.`
1008
- );
1009
- }
1000
+ const scopedEvent = `${this.app}:Channel:${this.id}:${event}`;
1001
+ console.log({ scopedEvent });
1002
+ const listener = (data) => {
1003
+ callback(data);
1004
+ wsClient.off(scopedEvent, listener);
1010
1005
  };
1011
- try {
1012
- wsClient.addScopedMessageListener(this.id, this.app, globalListener);
1013
- } catch (error) {
1014
- console.error(
1015
- `Erro ao adicionar listener escopado em 'once' para canal '${this.id}':`,
1016
- error
1017
- );
1018
- throw error;
1019
- }
1020
- super.once(event, callback);
1006
+ wsClient.on(scopedEvent, listener);
1021
1007
  return this;
1022
1008
  }
1023
1009
  /**
1024
1010
  * Remove um listener específico.
1025
1011
  */
1026
1012
  off(event, callback) {
1027
- super.off(event, callback);
1013
+ const wsClient = this.client.getWebSocketClients().get(this.app);
1014
+ if (!wsClient) {
1015
+ throw new Error(
1016
+ `Nenhum WebSocket conectado dispon\xEDvel para '${this.app}'.`
1017
+ );
1018
+ }
1019
+ const scopedEvent = `${this.app}:Channel:${this.id}:${event}`;
1020
+ wsClient.off(scopedEvent, callback);
1028
1021
  return this;
1029
1022
  }
1030
1023
  async answer() {
@@ -1500,55 +1493,51 @@ var PlaybackInstance = class extends import_events3.EventEmitter {
1500
1493
  /**
1501
1494
  * Adiciona um listener para eventos de playback.
1502
1495
  */
1496
+ /**
1497
+ * Adiciona um listener para eventos de playback.
1498
+ */
1503
1499
  on(event, callback) {
1504
- super.on(event, callback);
1500
+ const wsClient = this.client.getWebSocketClients().get(this.app);
1501
+ if (!wsClient) {
1502
+ throw new Error(
1503
+ `Nenhum WebSocket conectado dispon\xEDvel para '${this.app}'.`
1504
+ );
1505
+ }
1506
+ const scopedEvent = `${this.app}:Playback:${this.id}:${event}`;
1507
+ wsClient.on(scopedEvent, callback);
1505
1508
  return this;
1506
1509
  }
1507
1510
  /**
1508
1511
  * Adiciona um listener para ser chamado apenas uma vez.
1509
1512
  */
1510
1513
  once(event, callback) {
1511
- console.log({
1512
- type: "Adiciona um listener para ser chamado apenas uma vez",
1513
- event
1514
- });
1515
- const wsClients = this.client.getWebSocketClients();
1516
- const wsClient = Array.from(wsClients.values()).find(
1517
- (client) => client.isConnected()
1518
- );
1514
+ const wsClient = this.client.getWebSocketClients().get(this.app);
1519
1515
  if (!wsClient) {
1520
1516
  throw new Error(
1521
- `Nenhum WebSocket conectado dispon\xEDvel para '${this.id}'.`
1517
+ `Nenhum WebSocket conectado dispon\xEDvel para '${this.app}'.`
1522
1518
  );
1523
1519
  }
1524
- const globalListener = (webSocketEvent) => {
1525
- if (isPlaybackEvent(webSocketEvent, this.id) && webSocketEvent.type === event) {
1526
- console.log(`Recebido evento '${event}' para playback '${this.id}'`);
1527
- this.emit(event, webSocketEvent);
1528
- callback(webSocketEvent);
1529
- wsClient.removeScopedMessageListeners(this.id, this.app);
1530
- console.log(
1531
- `Listeners escopados para playback '${this.id}' foram removidos.`
1532
- );
1533
- }
1520
+ const scopedEvent = `${this.app}:Playback:${this.id}:${event}`;
1521
+ console.log({ scopedEvent });
1522
+ const listener = (data) => {
1523
+ callback(data);
1524
+ wsClient.off(scopedEvent, listener);
1534
1525
  };
1535
- try {
1536
- wsClient.addScopedMessageListener(this.id, this.app, globalListener);
1537
- } catch (error) {
1538
- console.error(
1539
- `Erro ao adicionar listener escopado em 'once' para playback '${this.id}':`,
1540
- error
1541
- );
1542
- throw error;
1543
- }
1544
- super.once(event, callback);
1526
+ wsClient.on(scopedEvent, listener);
1545
1527
  return this;
1546
1528
  }
1547
1529
  /**
1548
1530
  * Remove um listener específico.
1549
1531
  */
1550
1532
  off(event, callback) {
1551
- super.off(event, callback);
1533
+ const wsClient = this.client.getWebSocketClients().get(this.app);
1534
+ if (!wsClient) {
1535
+ throw new Error(
1536
+ `Nenhum WebSocket conectado dispon\xEDvel para '${this.app}'.`
1537
+ );
1538
+ }
1539
+ const scopedEvent = `${this.app}:Playback:${this.id}:${event}`;
1540
+ wsClient.off(scopedEvent, callback);
1552
1541
  return this;
1553
1542
  }
1554
1543
  /**
@@ -1947,10 +1936,17 @@ var WebSocketClient = class extends import_events4.EventEmitter {
1947
1936
  try {
1948
1937
  const decodedData = JSON.parse(rawData.toString());
1949
1938
  if (decodedData?.type && decodedData?.application) {
1950
- console.log({ handleMessage: decodedData });
1951
1939
  const scopedEvent = `${decodedData.application}:${decodedData.type}`;
1940
+ if ("channel" in decodedData && decodedData.channel?.id) {
1941
+ const channelScopedEvent = `${decodedData.application}:Channel:${decodedData.channel.id}:${decodedData.type}`;
1942
+ console.log({ handleMessage: decodedData });
1943
+ this.emit(channelScopedEvent, decodedData);
1944
+ } else if ("playback" in decodedData && decodedData.playback?.id) {
1945
+ const playbackScopedEvent = `${decodedData.application}:Playback:${decodedData.playback.id}:${decodedData.type}`;
1946
+ console.log({ handleMessage: decodedData });
1947
+ this.emit(playbackScopedEvent, decodedData);
1948
+ }
1952
1949
  this.emit(scopedEvent, decodedData);
1953
- this.emit(decodedData.application, decodedData);
1954
1950
  } else {
1955
1951
  console.warn(
1956
1952
  "Mensagem recebida sem tipo ou aplica\xE7\xE3o v\xE1lida:",