refactor(backup): pass in options service directly

This commit is contained in:
Elian Doran
2026-04-12 18:39:38 +03:00
parent 3ad20e43f1
commit 89b3dec84a
8 changed files with 13 additions and 14 deletions

View File

@@ -13,8 +13,8 @@ export default class StandaloneBackupService extends BackupService {
private backupDir: FileSystemDirectoryHandle | null = null;
private opfsAvailable: boolean | null = null;
constructor(getOptions: () => BackupOptionsService) {
super(getOptions);
constructor(options: BackupOptionsService) {
super(options);
}
private isOpfsAvailable(): boolean {

View File

@@ -176,7 +176,7 @@ async function initialize(): Promise<void> {
request: new FetchRequestProvider(),
platform: new StandalonePlatformProvider(queryString),
log: logService,
backup: new StandaloneBackupService(() => coreModule!.options),
backup: new StandaloneBackupService(coreModule!.options),
translations: translationProvider,
schema: schemaModule.default,
getDemoArchive: async () => {

View File

@@ -130,7 +130,7 @@ beforeAll(async () => {
});
},
platform: new StandalonePlatformProvider(""),
backup: new StandaloneBackupService(() => options),
backup: new StandaloneBackupService(options),
schema: schemaSql,
dbConfig: {
provider: sqlProvider,

View File

@@ -151,7 +151,7 @@ async function main() {
// both source and bundled-production modes.
getDemoArchive: async () => fs.readFileSync(path.join(RESOURCE_DIR, "db", "demo.zip")),
inAppHelp: new NodejsInAppHelpProvider(),
backup: new ServerBackupService(() => options),
backup: new ServerBackupService(options),
image: (await import("@triliumnext/server/src/services/image_provider.js")).serverImageProvider,
extraAppInfo: {
nodeVersion: process.version,

View File

@@ -44,6 +44,6 @@ beforeAll(async () => {
platform: new ServerPlatformProvider(),
translations: initializeTranslationsWithParams,
inAppHelp: new NodejsInAppHelpProvider(),
backup: new ServerBackupService(() => options)
backup: new ServerBackupService(options)
});
});

View File

@@ -8,8 +8,8 @@ import log from "./services/log.js";
import sql from "./services/sql.js";
export default class ServerBackupService extends BackupService {
constructor(getOptions: () => BackupOptionsService) {
super(getOptions);
constructor(options: BackupOptionsService) {
super(options);
}
override getExistingBackups(): DatabaseBackup[] {

View File

@@ -81,7 +81,7 @@ async function startApplication() {
// both source and bundled-production modes.
getDemoArchive: async () => fs.readFileSync(path.join(RESOURCE_DIR, "db", "demo.zip")),
inAppHelp: new NodejsInAppHelpProvider(),
backup: new ServerBackupService(() => options),
backup: new ServerBackupService(options),
image: (await import("./services/image_provider.js")).serverImageProvider,
extraAppInfo: {
nodeVersion: process.version,

View File

@@ -15,7 +15,7 @@ export interface BackupOptionsService {
* Platform-specific implementations must extend this class.
*/
export default abstract class BackupService {
constructor(protected readonly getOptions: () => BackupOptionsService) {}
constructor(protected readonly options: BackupOptionsService) {}
/**
* Create a backup with the given name.
@@ -59,7 +59,7 @@ export default abstract class BackupService {
backupType === "weekly" ? "weeklyBackupEnabled" :
"monthlyBackupEnabled";
return this.getOptions().getOptionBool(optionName);
return this.options.getOptionBool(optionName);
}
/**
@@ -74,13 +74,12 @@ export default abstract class BackupService {
return;
}
const options = this.getOptions();
const now = new Date();
const lastBackupDate = dateUtils.parseDateTime(options.getOption(optionName));
const lastBackupDate = dateUtils.parseDateTime(this.options.getOption(optionName));
if (now.getTime() - lastBackupDate.getTime() > periodInSeconds * 1000) {
await this.backupNow(backupType);
options.setOption(optionName, dateUtils.utcNowDateTime());
this.options.setOption(optionName, dateUtils.utcNowDateTime());
}
}
}