websocket fix
test-org/trust-module-frontend/pipeline/pr-rc This commit looks good
Details
test-org/trust-module-frontend/pipeline/pr-rc This commit looks good
Details
parent
cb7c22929a
commit
09a6082917
|
|
@ -6,6 +6,8 @@ class MetricsService {
|
||||||
this.socket = null;
|
this.socket = null;
|
||||||
this.subscriptions = new Map();
|
this.subscriptions = new Map();
|
||||||
this.pendingRequests = new Map();
|
this.pendingRequests = new Map();
|
||||||
|
window.addEventListener('beforeunload', this.cleanupAll.bind(this));
|
||||||
|
window.addEventListener('pagehide', this.cleanupAll.bind(this));
|
||||||
|
|
||||||
window.addEventListener('beforeunload', () => {
|
window.addEventListener('beforeunload', () => {
|
||||||
this.cleanupAll();
|
this.cleanupAll();
|
||||||
|
|
@ -89,14 +91,9 @@ class MetricsService {
|
||||||
|
|
||||||
subscribeToMetric(metricKey, callback, interval = 5000, filters = {}) {
|
subscribeToMetric(metricKey, callback, interval = 5000, filters = {}) {
|
||||||
this.connectWebSocket();
|
this.connectWebSocket();
|
||||||
|
|
||||||
const alreadySubscribed = this.subscriptions.has(metricKey);
|
if (!this.subscriptions.has(metricKey)) {
|
||||||
const callbacks = this.subscriptions.get(metricKey) || [];
|
this.subscriptions.set(metricKey, []);
|
||||||
callbacks.push(callback);
|
|
||||||
this.subscriptions.set(metricKey, callbacks);
|
|
||||||
|
|
||||||
if (!alreadySubscribed) {
|
|
||||||
// Разделяем metricKey на метрику и фильтры
|
|
||||||
const [metric] = metricKey.split('?');
|
const [metric] = metricKey.split('?');
|
||||||
this.socket.emit('subscribe-metric', {
|
this.socket.emit('subscribe-metric', {
|
||||||
metric,
|
metric,
|
||||||
|
|
@ -104,8 +101,13 @@ class MetricsService {
|
||||||
filters
|
filters
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return () => this.unsubscribeFromMetric(metricKey, callback);
|
const callbacks = this.subscriptions.get(metricKey);
|
||||||
|
callbacks.push(callback);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
this.unsubscribeFromMetric(metricKey, callback);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
unsubscribeFromMetric(metricKey, callback) {
|
unsubscribeFromMetric(metricKey, callback) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue