refactor: consolidate and debloat calendar sync implementation

Merge duplicated Google/Outlook engines, background handlers, and API
layers into shared calendarSync modules to cut complexity without changing
user-facing sync behavior.
This commit is contained in:
SethBurkart123
2026-06-28 10:45:45 +10:00
parent f4230e02b9
commit 7779edb063
52 changed files with 1965 additions and 3258 deletions
+44 -11
View File
@@ -1,20 +1,53 @@
import browser from "webextension-polyfill";
import {
GOOGLE_CALENDAR_SYNC_WEEKS_DEFAULT,
GOOGLE_CALENDAR_SYNC_WEEKS,
GOOGLE_CALENDAR_SYNC_WEEKS_MAX,
GOOGLE_CALENDAR_SYNC_WEEKS_MIN,
} from "@/config/googleCalendar";
import { readOutlookCalendarState } from "@/seqta/utils/outlookCalendar/storage";
import { readGoogleCalendarState } from "@/seqta/utils/googleCalendar/storage";
import {
readSharedCalendarSyncSettings,
WEEKLY_SYNC_INTERVAL_MS,
writeSharedCalendarSyncSettings,
} from "./sharedSettings";
import { readOutlookCalendarState } from "@/seqta/utils/outlookCalendar/storage";
export { CALENDAR_WEEKLY_ALARM, WEEKLY_SYNC_INTERVAL_MS } from "./sharedSettings";
export const BSPLUS_CALENDAR_SYNC_SETTINGS_KEY = "bsplus_calendar_sync_settings";
export const CALENDAR_WEEKLY_ALARM = "bsplus_calendar_weekly";
export const WEEKLY_SYNC_INTERVAL_MS = 7 * 24 * 60 * 60 * 1000;
export interface SharedCalendarSyncSettings {
syncWeeksAhead?: number;
autoSyncWeekly?: boolean;
lastWeeklySyncAt?: number;
pendingWeeklySync?: boolean;
}
export async function readSharedCalendarSyncSettings(): Promise<SharedCalendarSyncSettings> {
const got = await browser.storage.local.get(BSPLUS_CALENDAR_SYNC_SETTINGS_KEY);
const raw = got[BSPLUS_CALENDAR_SYNC_SETTINGS_KEY];
const shared =
raw && typeof raw === "object" && !Array.isArray(raw)
? (raw as SharedCalendarSyncSettings)
: {};
if (Object.keys(shared).length > 0) return shared;
const legacy = await readGoogleCalendarState();
return {
syncWeeksAhead: legacy.syncWeeksAhead,
autoSyncWeekly: legacy.autoSyncWeekly,
lastWeeklySyncAt: legacy.lastWeeklySyncAt,
pendingWeeklySync: legacy.pendingWeeklySync,
};
}
export async function writeSharedCalendarSyncSettings(
patch: Partial<SharedCalendarSyncSettings>,
): Promise<SharedCalendarSyncSettings> {
const current = await readSharedCalendarSyncSettings();
const next = { ...current, ...patch };
await browser.storage.local.set({ [BSPLUS_CALENDAR_SYNC_SETTINGS_KEY]: next });
return next;
}
export function clampSyncWeeks(weeks: number): number {
if (!Number.isFinite(weeks)) return GOOGLE_CALENDAR_SYNC_WEEKS_DEFAULT;
if (!Number.isFinite(weeks)) return GOOGLE_CALENDAR_SYNC_WEEKS;
return Math.min(
GOOGLE_CALENDAR_SYNC_WEEKS_MAX,
Math.max(GOOGLE_CALENDAR_SYNC_WEEKS_MIN, Math.round(weeks)),
@@ -23,7 +56,7 @@ export function clampSyncWeeks(weeks: number): number {
export async function getSyncWeeksAhead(): Promise<number> {
const settings = await readSharedCalendarSyncSettings();
return clampSyncWeeks(settings.syncWeeksAhead ?? GOOGLE_CALENDAR_SYNC_WEEKS_DEFAULT);
return clampSyncWeeks(settings.syncWeeksAhead ?? GOOGLE_CALENDAR_SYNC_WEEKS);
}
export async function getAutoSyncWeekly(): Promise<boolean> {
@@ -31,7 +64,7 @@ export async function getAutoSyncWeekly(): Promise<boolean> {
return settings.autoSyncWeekly !== false;
}
async function isAnyCalendarConnected(): Promise<boolean> {
export async function isAnyCalendarConnected(): Promise<boolean> {
const [google, outlook] = await Promise.all([
readGoogleCalendarState(),
readOutlookCalendarState(),