From 9b2052726da9a3e15b041039ced3a63467f95452 Mon Sep 17 00:00:00 2001 From: Cesar Gonzalez Date: Mon, 22 Apr 2024 16:50:57 -0500 Subject: [PATCH] [PM-6426] Refactoring smaller details of the implementation --- .../browser/src/background/main.background.ts | 1 + .../browser-task-scheduler.service.ts | 1 + .../browser-task-scheduler.service.ts | 61 ++++++++++--------- 3 files changed, 34 insertions(+), 29 deletions(-) diff --git a/apps/browser/src/background/main.background.ts b/apps/browser/src/background/main.background.ts index 632e6a6133..d142a3edd4 100644 --- a/apps/browser/src/background/main.background.ts +++ b/apps/browser/src/background/main.background.ts @@ -1167,6 +1167,7 @@ export default class MainBackground { 5 * 60 * 1000, // check every 5 minutes ); setTimeout(() => this.notificationsService.init(), 2500); + await this.taskSchedulerService.verifyAlarmsState(); resolve(); }, 500); }); diff --git a/apps/browser/src/platform/services/abstractions/browser-task-scheduler.service.ts b/apps/browser/src/platform/services/abstractions/browser-task-scheduler.service.ts index 8a1b49f456..7eb5da1433 100644 --- a/apps/browser/src/platform/services/abstractions/browser-task-scheduler.service.ts +++ b/apps/browser/src/platform/services/abstractions/browser-task-scheduler.service.ts @@ -9,4 +9,5 @@ export type ActiveAlarm = { export interface BrowserTaskSchedulerService extends TaskSchedulerService { clearAllScheduledTasks(): Promise; + verifyAlarmsState(): Promise; } diff --git a/apps/browser/src/platform/services/browser-task-scheduler.service.ts b/apps/browser/src/platform/services/browser-task-scheduler.service.ts index baf7ba891e..1cac953010 100644 --- a/apps/browser/src/platform/services/browser-task-scheduler.service.ts +++ b/apps/browser/src/platform/services/browser-task-scheduler.service.ts @@ -39,7 +39,6 @@ export class BrowserTaskSchedulerService ); this.setupOnAlarmListener(); - this.verifyAlarmsState().catch((e) => this.logService.error(e)); } /** @@ -129,36 +128,11 @@ export class BrowserTaskSchedulerService this.recoveredAlarms.clear(); } - /** - * Creates a browser extension alarm with the given name and create info. - * - * @param taskName - The name of the alarm. - * @param createInfo - The alarm create info. - */ - private async scheduleAlarm( - taskName: ScheduledTaskName, - createInfo: chrome.alarms.AlarmCreateInfo, - ): Promise { - const existingAlarm = await this.getAlarm(taskName); - if (existingAlarm) { - this.logService.warning(`Alarm ${taskName} already exists. Skipping creation.`); - return; - } - - await this.createAlarm(taskName, createInfo); - - await this.setActiveAlarm({ - taskName, - startTime: Date.now(), - createInfo, - }); - } - /** * Verifies the state of the active alarms by checking if * any alarms have been missed or need to be created. */ - private async verifyAlarmsState(): Promise { + async verifyAlarmsState(): Promise { const currentTime = Date.now(); const activeAlarms = await firstValueFrom(this.activeAlarms$); @@ -186,6 +160,31 @@ export class BrowserTaskSchedulerService globalThis.setTimeout(() => this.recoveredAlarms.clear(), 10 * 1000); } + /** + * Creates a browser extension alarm with the given name and create info. + * + * @param taskName - The name of the alarm. + * @param createInfo - The alarm create info. + */ + private async scheduleAlarm( + taskName: ScheduledTaskName, + createInfo: chrome.alarms.AlarmCreateInfo, + ): Promise { + const existingAlarm = await this.getAlarm(taskName); + if (existingAlarm) { + this.logService.warning(`Alarm ${taskName} already exists. Skipping creation.`); + return; + } + + await this.createAlarm(taskName, createInfo); + + await this.setActiveAlarm({ + taskName, + startTime: Date.now(), + createInfo, + }); + } + /** * Sets an active alarm in state. * @@ -259,7 +258,7 @@ export class BrowserTaskSchedulerService alarmName: ScheduledTaskName, periodInMinutes?: number, ): Promise { - const activeUserAlarmName = await this.getActiveUserAlarmName(alarmName); + const activeUserAlarmName = this.getTaskFromAlarmName(alarmName); const handler = this.taskHandlers.get(activeUserAlarmName); if (!periodInMinutes) { await this.deleteActiveAlarm(alarmName); @@ -343,6 +342,10 @@ export class BrowserTaskSchedulerService return taskName; } - return `${activeUserId}_${taskName}`; + return `${activeUserId}__${taskName}`; + } + + private getTaskFromAlarmName(alarmName: string): string { + return alarmName.split("__")[1]; } }