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