feat: add SafeSettings class for unified settings updates

- Create SafeSettings class with update() and updateSync() methods
- Provides automatic backup and rollback on failure
- Initialize in main.ts
This commit is contained in:
HeroBlackInk 2026-02-22 01:02:56 +08:00
parent fb73690b53
commit bad55ae564
2 changed files with 56 additions and 2 deletions

View file

@ -33,6 +33,9 @@ import { StoragePathManager } from './src/storagePathManager';
//settings backup
import { SettingsBackup } from './src/settingsBackup';
//safe settings
import { SafeSettings } from './src/safeSettings';
//import modal
import { SetDefalutProjectInTheFilepathModal } from 'src/modal';
@ -50,8 +53,9 @@ export default class UltimateTodoistSyncForObsidian extends Plugin {
databaseChecker: DatabaseChecker | undefined;
deviceManager: DeviceManager | undefined;
storagePathManager: StoragePathManager | undefined;
settingsBackup: SettingsBackup | undefined;
lastLines: Map<string,number>;
settingsBackup: SettingsBackup | undefined;
safeSettings: SafeSettings | undefined;
lastLines: Map<string,number>;
statusBar;
syncLock: boolean;
saveLock: boolean;
@ -68,6 +72,7 @@ export default class UltimateTodoistSyncForObsidian extends Plugin {
}
this.settingsBackup = new SettingsBackup(this.app, this);
this.safeSettings = new SafeSettings(this);
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new UltimateTodoistSyncSettingTab(this.app, this));

49
src/safeSettings.ts Normal file
View file

@ -0,0 +1,49 @@
import UltimateTodoistSyncForObsidian from "../main";
export class SafeSettings {
private plugin: UltimateTodoistSyncForObsidian;
constructor(plugin: UltimateTodoistSyncForObsidian) {
this.plugin = plugin;
}
async update(changes: Partial<typeof this.plugin.settings>, shouldSave = false): Promise<void> {
await this.plugin.settingsBackup?.backup();
const backup = JSON.parse(JSON.stringify(this.plugin.settings));
try {
Object.assign(this.plugin.settings, changes);
if (shouldSave) {
await this.plugin.saveSettings();
}
console.log('[SafeSettings] Update completed successfully');
} catch (error) {
console.error('[SafeSettings] Update failed, rolling back...', error);
this.plugin.settings = backup;
await this.plugin.settingsBackup?.restore();
throw error;
}
}
updateSync(changes: Partial<typeof this.plugin.settings>, shouldSave = false): void {
this.plugin.settingsBackup?.backup();
const backup = JSON.parse(JSON.stringify(this.plugin.settings));
try {
Object.assign(this.plugin.settings, changes);
if (shouldSave) {
this.plugin.saveSettings();
}
console.log('[SafeSettings] UpdateSync completed successfully');
} catch (error) {
console.error('[SafeSettings] UpdateSync failed, rolling back...', error);
this.plugin.settings = backup;
this.plugin.settingsBackup?.restore();
throw error;
}
}
}