@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.
package/dist/esm/index.js CHANGED
@@ -955,54 +955,47 @@ var ChannelInstance = class extends EventEmitter2 {
955
955
  * Adiciona um listener para eventos de canal.
956
956
  */
957
957
  on(event, callback) {
958
- super.on(event, callback);
958
+ const wsClient = this.client.getWebSocketClients().get(this.app);
959
+ if (!wsClient) {
960
+ throw new Error(
961
+ `Nenhum WebSocket conectado dispon\xEDvel para '${this.app}'.`
962
+ );
963
+ }
964
+ const scopedEvent = `${this.app}:Channel:${this.id}:${event}`;
965
+ wsClient.on(scopedEvent, callback);
959
966
  return this;
960
967
  }
961
968
  /**
962
969
  * Adiciona um listener para ser chamado apenas uma vez.
963
970
  */
964
971
  once(event, callback) {
965
- console.log({
966
- type: "Adiciona um listener para ser chamado apenas uma vez",
967
- event
968
- });
969
- const wsClients = this.client.getWebSocketClients();
970
- const wsClient = Array.from(wsClients.values()).find(
971
- (client) => client.isConnected()
972
- );
972
+ const wsClient = this.client.getWebSocketClients().get(this.app);
973
973
  if (!wsClient) {
974
974
  throw new Error(
975
- `Nenhum WebSocket conectado dispon\xEDvel para '${this.id}'.`
975
+ `Nenhum WebSocket conectado dispon\xEDvel para '${this.app}'.`
976
976
  );
977
977
  }
978
- const globalListener = (webSocketEvent) => {
979
- if (isChannelEvent(webSocketEvent, this.id) && webSocketEvent.type === event) {
980
- console.log(`Recebido evento '${event}' para canal '${this.id}'`);
981
- this.emit(event, webSocketEvent);
982
- callback(webSocketEvent);
983
- wsClient.removeScopedMessageListeners(this.id, this.app);
984
- console.log(
985
- `Listeners escopados para canal '${this.id}' foram removidos.`
986
- );
987
- }
978
+ const scopedEvent = `${this.app}:Channel:${this.id}:${event}`;
979
+ console.log({ scopedEvent });
980
+ const listener = (data) => {
981
+ callback(data);
982
+ wsClient.off(scopedEvent, listener);
988
983
  };
989
- try {
990
- wsClient.addScopedMessageListener(this.id, this.app, globalListener);
991
- } catch (error) {
992
- console.error(
993
- `Erro ao adicionar listener escopado em 'once' para canal '${this.id}':`,
994
- error
995
- );
996
- throw error;
997
- }
998
- super.once(event, callback);
984
+ wsClient.on(scopedEvent, listener);
999
985
  return this;
1000
986
  }
1001
987
  /**
1002
988
  * Remove um listener específico.
1003
989
  */
1004
990
  off(event, callback) {
1005
- super.off(event, callback);
991
+ const wsClient = this.client.getWebSocketClients().get(this.app);
992
+ if (!wsClient) {
993
+ throw new Error(
994
+ `Nenhum WebSocket conectado dispon\xEDvel para '${this.app}'.`
995
+ );
996
+ }
997
+ const scopedEvent = `${this.app}:Channel:${this.id}:${event}`;
998
+ wsClient.off(scopedEvent, callback);
1006
999
  return this;
1007
1000
  }
1008
1001
  async answer() {
@@ -1478,55 +1471,51 @@ var PlaybackInstance = class extends EventEmitter3 {
1478
1471
  /**
1479
1472
  * Adiciona um listener para eventos de playback.
1480
1473
  */
1474
+ /**
1475
+ * Adiciona um listener para eventos de playback.
1476
+ */
1481
1477
  on(event, callback) {
1482
- super.on(event, callback);
1478
+ const wsClient = this.client.getWebSocketClients().get(this.app);
1479
+ if (!wsClient) {
1480
+ throw new Error(
1481
+ `Nenhum WebSocket conectado dispon\xEDvel para '${this.app}'.`
1482
+ );
1483
+ }
1484
+ const scopedEvent = `${this.app}:Playback:${this.id}:${event}`;
1485
+ wsClient.on(scopedEvent, callback);
1483
1486
  return this;
1484
1487
  }
1485
1488
  /**
1486
1489
  * Adiciona um listener para ser chamado apenas uma vez.
1487
1490
  */
1488
1491
  once(event, callback) {
1489
- console.log({
1490
- type: "Adiciona um listener para ser chamado apenas uma vez",
1491
- event
1492
- });
1493
- const wsClients = this.client.getWebSocketClients();
1494
- const wsClient = Array.from(wsClients.values()).find(
1495
- (client) => client.isConnected()
1496
- );
1492
+ const wsClient = this.client.getWebSocketClients().get(this.app);
1497
1493
  if (!wsClient) {
1498
1494
  throw new Error(
1499
- `Nenhum WebSocket conectado dispon\xEDvel para '${this.id}'.`
1495
+ `Nenhum WebSocket conectado dispon\xEDvel para '${this.app}'.`
1500
1496
  );
1501
1497
  }
1502
- const globalListener = (webSocketEvent) => {
1503
- if (isPlaybackEvent(webSocketEvent, this.id) && webSocketEvent.type === event) {
1504
- console.log(`Recebido evento '${event}' para playback '${this.id}'`);
1505
- this.emit(event, webSocketEvent);
1506
- callback(webSocketEvent);
1507
- wsClient.removeScopedMessageListeners(this.id, this.app);
1508
- console.log(
1509
- `Listeners escopados para playback '${this.id}' foram removidos.`
1510
- );
1511
- }
1498
+ const scopedEvent = `${this.app}:Playback:${this.id}:${event}`;
1499
+ console.log({ scopedEvent });
1500
+ const listener = (data) => {
1501
+ callback(data);
1502
+ wsClient.off(scopedEvent, listener);
1512
1503
  };
1513
- try {
1514
- wsClient.addScopedMessageListener(this.id, this.app, globalListener);
1515
- } catch (error) {
1516
- console.error(
1517
- `Erro ao adicionar listener escopado em 'once' para playback '${this.id}':`,
1518
- error
1519
- );
1520
- throw error;
1521
- }
1522
- super.once(event, callback);
1504
+ wsClient.on(scopedEvent, listener);
1523
1505
  return this;
1524
1506
  }
1525
1507
  /**
1526
1508
  * Remove um listener específico.
1527
1509
  */
1528
1510
  off(event, callback) {
1529
- super.off(event, callback);
1511
+ const wsClient = this.client.getWebSocketClients().get(this.app);
1512
+ if (!wsClient) {
1513
+ throw new Error(
1514
+ `Nenhum WebSocket conectado dispon\xEDvel para '${this.app}'.`
1515
+ );
1516
+ }
1517
+ const scopedEvent = `${this.app}:Playback:${this.id}:${event}`;
1518
+ wsClient.off(scopedEvent, callback);
1530
1519
  return this;
1531
1520
  }
1532
1521
  /**
@@ -1925,10 +1914,17 @@ var WebSocketClient = class extends EventEmitter4 {
1925
1914
  try {
1926
1915
  const decodedData = JSON.parse(rawData.toString());
1927
1916
  if (decodedData?.type && decodedData?.application) {
1928
- console.log({ handleMessage: decodedData });
1929
1917
  const scopedEvent = `${decodedData.application}:${decodedData.type}`;
1918
+ if ("channel" in decodedData && decodedData.channel?.id) {
1919
+ const channelScopedEvent = `${decodedData.application}:Channel:${decodedData.channel.id}:${decodedData.type}`;
1920
+ console.log({ handleMessage: decodedData });
1921
+ this.emit(channelScopedEvent, decodedData);
1922
+ } else if ("playback" in decodedData && decodedData.playback?.id) {
1923
+ const playbackScopedEvent = `${decodedData.application}:Playback:${decodedData.playback.id}:${decodedData.type}`;
1924
+ console.log({ handleMessage: decodedData });
1925
+ this.emit(playbackScopedEvent, decodedData);
1926
+ }
1930
1927
  this.emit(scopedEvent, decodedData);
1931
- this.emit(decodedData.application, decodedData);
1932
1928
  } else {
1933
1929
  console.warn(
1934
1930
  "Mensagem recebida sem tipo ou aplica\xE7\xE3o v\xE1lida:",