1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-11 05:53:42 +00:00

Refactor NotificationsService to encapsulate SignalR connection setup in a separate method

This commit is contained in:
Rui Tome
2025-01-27 16:07:00 +00:00
parent 1398c49f9a
commit cfe0ea46bb

View File

@@ -80,27 +80,7 @@ export class NotificationsService implements NotificationsServiceAbstraction {
this.signalrConnection = null;
}
this.signalrConnection = new signalR.HubConnectionBuilder()
.withUrl(this.url + "/hub", {
accessTokenFactory: () => this.apiService.getActiveBearerToken(),
skipNegotiation: true,
transport: signalR.HttpTransportType.WebSockets,
})
.withHubProtocol(new signalRMsgPack.MessagePackHubProtocol() as signalR.IHubProtocol)
// .configureLogging(signalR.LogLevel.Trace)
.build();
this.signalrConnection.on("ReceiveMessage", (data: any) =>
this.processNotification(new NotificationResponse(data)),
);
// eslint-disable-next-line
this.signalrConnection.on("Heartbeat", (data: any) => {
/*console.log('Heartbeat!');*/
});
this.signalrConnection.onclose(async () => {
this.connected = false;
await this.reconnect(true);
});
this.signalrConnection = this.setupConnection();
this.inited = true;
if (await this.isAuthedAndUnlocked()) {
await this.reconnect(false);
@@ -276,4 +256,28 @@ export class NotificationsService implements NotificationsServiceAbstraction {
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
private setupConnection(): signalR.HubConnection {
const connection = new signalR.HubConnectionBuilder()
.withUrl(this.url + "/hub", {
accessTokenFactory: () => this.apiService.getActiveBearerToken(),
skipNegotiation: true,
transport: signalR.HttpTransportType.WebSockets,
})
.withHubProtocol(new signalRMsgPack.MessagePackHubProtocol() as signalR.IHubProtocol)
.build();
connection.on("ReceiveMessage", (data: any) =>
this.processNotification(new NotificationResponse(data)),
);
connection.on("Heartbeat", (data: any) => {
/*console.log('Heartbeat!');*/
});
connection.onclose(async () => {
this.connected = false;
await this.reconnect(true);
});
return connection;
}
}