1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-11-03 02:14:22 +01:00
ntfy/web/src/app/ConnectionManager.js

43 lines
1.5 KiB
JavaScript
Raw Normal View History

import Connection from "./Connection";
class ConnectionManager {
constructor() {
this.connections = new Map();
}
refresh(subscriptions, users, onNotification) {
if (!subscriptions || !users) {
return;
}
console.log(`[ConnectionManager] Refreshing connections`);
const subscriptionIds = subscriptions.ids();
const deletedIds = Array.from(this.connections.keys()).filter(id => !subscriptionIds.includes(id));
// Create and add new connections
subscriptions.forEach((id, subscription) => {
const added = !this.connections.get(id)
if (added) {
const baseUrl = subscription.baseUrl;
const topic = subscription.topic;
const [user] = users.filter(user => user.baseUrl === baseUrl);
2022-02-28 01:29:17 +01:00
const since = subscription.last;
const connection = new Connection(id, baseUrl, topic, user, since, onNotification);
this.connections.set(id, connection);
console.log(`[ConnectionManager] Starting new connection ${id}`);
connection.start();
}
});
// Delete old connections
deletedIds.forEach(id => {
console.log(`[ConnectionManager] Closing connection ${id}`);
const connection = this.connections.get(id);
this.connections.delete(id);
connection.close();
});
}
}
const connectionManager = new ConnectionManager();
export default connectionManager;