From 5d41e8b627ba39e23b314c9be16b31dff0a3a587 Mon Sep 17 00:00:00 2001 From: Cesar Gonzalez Date: Mon, 1 Apr 2024 12:00:46 -0500 Subject: [PATCH] [PM-6426] Implementing reconnect timer timeout for NotificationService using the TaskSchedulerService --- .../src/services/notifications.service.ts | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/libs/common/src/services/notifications.service.ts b/libs/common/src/services/notifications.service.ts index 4dc8772d00..a08a27a545 100644 --- a/libs/common/src/services/notifications.service.ts +++ b/libs/common/src/services/notifications.service.ts @@ -18,6 +18,8 @@ import { EnvironmentService } from "../platform/abstractions/environment.service import { LogService } from "../platform/abstractions/log.service"; import { MessagingService } from "../platform/abstractions/messaging.service"; import { StateService } from "../platform/abstractions/state.service"; +import { TaskSchedulerService } from "../platform/abstractions/task-scheduler.service"; +import { ScheduledTaskNames } from "../platform/enums/scheduled-task-name.enum"; import { SyncService } from "../vault/abstractions/sync/sync.service.abstraction"; export class NotificationsService implements NotificationsServiceAbstraction { @@ -26,7 +28,7 @@ export class NotificationsService implements NotificationsServiceAbstraction { private connected = false; private inited = false; private inactive = false; - private reconnectTimer: any = null; + private reconnectTimer: number | NodeJS.Timeout = null; constructor( private logService: LogService, @@ -38,6 +40,7 @@ export class NotificationsService implements NotificationsServiceAbstraction { private stateService: StateService, private authService: AuthService, private messagingService: MessagingService, + private taskSchedulerService: TaskSchedulerService, ) { this.environmentService.environment$.subscribe(() => { if (!this.inited) { @@ -211,10 +214,11 @@ export class NotificationsService implements NotificationsServiceAbstraction { } private async reconnect(sync: boolean) { - if (this.reconnectTimer != null) { - clearTimeout(this.reconnectTimer); - this.reconnectTimer = null; - } + void this.taskSchedulerService.clearScheduledTask({ + taskName: ScheduledTaskNames.notificationsReconnectTimeout, + timeoutId: this.reconnectTimer, + }); + if (this.connected || !this.inited || this.inactive) { return; } @@ -234,7 +238,11 @@ export class NotificationsService implements NotificationsServiceAbstraction { } if (!this.connected) { - this.reconnectTimer = setTimeout(() => this.reconnect(sync), this.random(120000, 300000)); + this.reconnectTimer = await this.taskSchedulerService.setTimeout( + () => this.reconnect(sync), + this.random(120000, 300000), + ScheduledTaskNames.notificationsReconnectTimeout, + ); } }