1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-06 09:20:43 +01:00

[PM-6426] Refactoring smaller details of the implementation

This commit is contained in:
Cesar Gonzalez 2024-04-22 16:50:57 -05:00
parent aa6bc2b5ac
commit 9b2052726d
No known key found for this signature in database
GPG Key ID: 3381A5457F8CCECF
3 changed files with 34 additions and 29 deletions

View File

@ -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);
});

View File

@ -9,4 +9,5 @@ export type ActiveAlarm = {
export interface BrowserTaskSchedulerService extends TaskSchedulerService {
clearAllScheduledTasks(): Promise<void>;
verifyAlarmsState(): Promise<void>;
}

View File

@ -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<void> {
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<void> {
async verifyAlarmsState(): Promise<void> {
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<void> {
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<void> {
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];
}
}