| 1 |
import Connection from "./Connection"; |
| 2 |
import { hashCode } from "./utils"; |
| 3 |
|
| 4 |
const makeConnectionId = (subscription, user) => |
| 5 |
user ? hashCode(`${subscription.id}|${user.username}|${user.password ?? ""}|${user.token ?? ""}`) : hashCode(`${subscription.id}`); |
| 6 |
|
| 7 |
/** |
| 8 |
* The connection manager keeps track of active connections (WebSocket connections, see Connection). |
| 9 |
* |
| 10 |
* Its refresh() method reconciles state changes with the target state by closing/opening connections |
| 11 |
* as required. This is done pretty much exactly the same way as in the Android app. |
| 12 |
*/ |
| 13 |
class ConnectionManager { |
| 14 |
constructor() { |
| 15 |
this.connections = new Map(); // ConnectionId -> Connection (hash, see below) |
| 16 |
this.stateListener = null; // Fired when connection state changes |
| 17 |
this.messageListener = null; // Fired when new notifications arrive |
| 18 |
} |
| 19 |
|
| 20 |
registerStateListener(listener) { |
| 21 |
this.stateListener = listener; |
| 22 |
} |
| 23 |
|
| 24 |
resetStateListener() { |
| 25 |
this.stateListener = null; |
| 26 |
} |
| 27 |
|
| 28 |
registerMessageListener(listener) { |
| 29 |
this.messageListener = listener; |
| 30 |
} |
| 31 |
|
| 32 |
resetMessageListener() { |
| 33 |
this.messageListener = null; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* This function figures out which websocket connections should be running by comparing the |
| 38 |
* current state of the world (connections) with the target state (targetIds). |
| 39 |
* |
| 40 |
* It uses a "connectionId", which is sha256($subscriptionId|$username|$password) to identify |
| 41 |
* connections. If any of them change, the connection is closed/replaced. |
| 42 |
*/ |
| 43 |
async refresh(subscriptions, users) { |
| 44 |
if (!subscriptions || !users) { |
| 45 |
return; |
| 46 |
} |
| 47 |
console.log(`[ConnectionManager] Refreshing connections`); |
| 48 |
const subscriptionsWithUsersAndConnectionId = subscriptions.map((s) => { |
| 49 |
const [user] = users.filter((u) => u.baseUrl === s.baseUrl); |
| 50 |
const connectionId = makeConnectionId(s, user); |
| 51 |
return { ...s, user, connectionId }; |
| 52 |
}); |
| 53 |
|
| 54 |
const targetIds = subscriptionsWithUsersAndConnectionId.map((s) => s.connectionId); |
| 55 |
const deletedIds = Array.from(this.connections.keys()).filter((id) => !targetIds.includes(id)); |
| 56 |
|
| 57 |
// Create and add new connections |
| 58 |
subscriptionsWithUsersAndConnectionId.forEach((subscription) => { |
| 59 |
const subscriptionId = subscription.id; |
| 60 |
const { connectionId } = subscription; |
| 61 |
const added = !this.connections.get(connectionId); |
| 62 |
if (added) { |
| 63 |
const { baseUrl, topic, user } = subscription; |
| 64 |
const since = subscription.last; |
| 65 |
const connection = new Connection( |
| 66 |
connectionId, |
| 67 |
subscriptionId, |
| 68 |
baseUrl, |
| 69 |
topic, |
| 70 |
user, |
| 71 |
since, |
| 72 |
(subId, notification) => this.notificationReceived(subId, notification), |
| 73 |
(subId, state) => this.stateChanged(subId, state), |
| 74 |
); |
| 75 |
this.connections.set(connectionId, connection); |
| 76 |
console.log( |
| 77 |
`[ConnectionManager] Starting new connection ${connectionId} (subscription ${subscriptionId} with user ${ |
| 78 |
user ? user.username : "anonymous" |
| 79 |
})`, |
| 80 |
); |
| 81 |
connection.start(); |
| 82 |
} |
| 83 |
}); |
| 84 |
|
| 85 |
// Delete old connections |
| 86 |
deletedIds.forEach((id) => { |
| 87 |
console.log(`[ConnectionManager] Closing connection ${id}`); |
| 88 |
const connection = this.connections.get(id); |
| 89 |
this.connections.delete(id); |
| 90 |
connection.close(); |
| 91 |
}); |
| 92 |
} |
| 93 |
|
| 94 |
stateChanged(subscriptionId, state) { |
| 95 |
if (this.stateListener) { |
| 96 |
try { |
| 97 |
this.stateListener(subscriptionId, state); |
| 98 |
} catch (e) { |
| 99 |
console.error(`[ConnectionManager] Error updating state of ${subscriptionId} to ${state}`, e); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
notificationReceived(subscriptionId, notification) { |
| 105 |
if (this.messageListener) { |
| 106 |
try { |
| 107 |
this.messageListener(subscriptionId, notification); |
| 108 |
} catch (e) { |
| 109 |
console.error(`[ConnectionManager] Error handling notification for ${subscriptionId}`, e); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
const connectionManager = new ConnectionManager(); |
| 116 |
export default connectionManager; |