1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-14 07:13:32 +00:00
Files
browser/libs/platform/src/background-sync/background-sync.service.ts
Addison Beck f759e62aeb fix(browser): restore timer based background syncs (#14031)
* docs: fix a typo

* fix(browser): restore timer-based background syncs

The browser extension was not performing scheduled background syncs every 30 minutes as expected. This was due to missing task scheduling code that was accidentally removed during the web push implementation (PR #11346).

This commit:
- Creates a new BackgroundSyncService to manage sync scheduling
- Properly initializes the sync interval in main.background.ts
- Adds a test to ensure the sync initialization code isn't accidentally removed again
- Organizes platform module structure to support the new service

Fixes PM-19396

* review: remove unecassary await keyword
2025-03-28 14:17:18 -04:00

45 lines
1.7 KiB
TypeScript

import { TaskSchedulerService, ScheduledTaskNames } from "@bitwarden/common/platform/scheduling";
/**
* The default interval between background syncs.
* 300,000ms = 5 minutes
*/
export const DEFAULT_SYNC_INTERVAL_MS = 300000;
/**
* Service responsible for registering and managing background synchronization for the browser extension.
* Handles scheduling of periodic sync operations using the task scheduler infrastructure.
*/
export class BackgroundSyncService {
/**
* Creates a new instance of BackgroundSyncService.
* @param taskSchedulerService - Service that handles scheduling and execution of periodic tasks
*/
constructor(private taskSchedulerService: TaskSchedulerService) {}
/**
* Registers a callback function to be executed when the sync interval task is triggered.
* This associates the sync task name with the provided callback in the task scheduler.
*
* @param syncCallback - The function to execute when the sync task is triggered
*/
register(syncCallback: () => Promise<void>) {
this.taskSchedulerService.registerTaskHandler(
ScheduledTaskNames.scheduleNextSyncInterval,
syncCallback,
);
}
/**
* Initializes the background sync service by scheduling the sync interval task.
* This sets up a recurring timer that triggers the registered sync callback at regular intervals.
*
* @param intervalMs - The interval in milliseconds between sync operations (defaults to 300000ms/5 minutes)
*/
init(intervalMs: number = DEFAULT_SYNC_INTERVAL_MS) {
intervalMs = intervalMs < 1 ? DEFAULT_SYNC_INTERVAL_MS : intervalMs;
this.taskSchedulerService.setInterval(ScheduledTaskNames.scheduleNextSyncInterval, intervalMs);
}
}