refactor: use SafeSettings for settings updates in cacheOperation

- Replace direct settings assignments with safeSettings.update/updateSync
- setTaskFileMapping and deleteTaskFileMapping use updateSync (no immediate save)
- updateRenamedFilePath and rebuildCache use update with shouldSave=true
- Provides automatic backup and rollback on failure
This commit is contained in:
HeroBlackInk 2026-02-22 01:11:32 +08:00
parent bad55ae564
commit 397e246599

View file

@ -240,12 +240,12 @@ export class CacheOperation {
* - defaultProjectId
*/
async newEmptyFileMetadata(filepath: string): Promise<void> {
const metadatas = this.plugin.settings.fileMetadata;
const metadatas = { ...this.plugin.settings.fileMetadata };
if (metadatas[filepath]) {
return;
}
metadatas[filepath] = {};
this.plugin.settings.fileMetadata = metadatas;
await this.plugin.safeSettings?.update({ fileMetadata: metadatas });
}
/**
@ -257,7 +257,7 @@ export class CacheOperation {
* -
*/
async updateFileMetadata(filepath: string, newMetadata: { defaultProjectId?: string }): Promise<void> {
const metadatas = this.plugin.settings.fileMetadata;
const metadatas = { ...this.plugin.settings.fileMetadata };
if (!metadatas[filepath]) {
metadatas[filepath] = {};
@ -267,7 +267,7 @@ export class CacheOperation {
metadatas[filepath].defaultProjectId = newMetadata.defaultProjectId;
}
this.plugin.settings.fileMetadata = metadatas;
await this.plugin.safeSettings?.update({ fileMetadata: metadatas });
this.plugin.logOperation?.log('CACHE_FILE_METADATA_UPDATED', `Updated file metadata for: ${filepath}`, filepath);
}
@ -371,7 +371,9 @@ export class CacheOperation {
* @param syncEnabled - true
*/
setTaskFileMapping(taskId: string, filePath: string, lineNumber: number, status: 'active' | 'nonActive' | 'conflicted' | 'issue' = 'active', syncEnabled: boolean = true): void {
this.plugin.settings.taskFileMapping[taskId] = { filePath, lineNumber, status, syncEnabled };
const mapping = { ...this.plugin.settings.taskFileMapping };
mapping[taskId] = { filePath, lineNumber, status, syncEnabled };
this.plugin.safeSettings?.updateSync({ taskFileMapping: mapping });
}
/**
@ -379,7 +381,9 @@ export class CacheOperation {
* @param taskId - Todoist ID
*/
deleteTaskFileMapping(taskId: string): void {
delete this.plugin.settings.taskFileMapping[taskId];
const mapping = { ...this.plugin.settings.taskFileMapping };
delete mapping[taskId];
this.plugin.safeSettings?.updateSync({ taskFileMapping: mapping });
}
/**
@ -532,15 +536,14 @@ export class CacheOperation {
* @param filepath -
* @param defaultProjectId - ID
*/
setDefaultProjectIdForFilepath(filepath:string,defaultProjectId:string){
const metadatas = this.plugin.settings.fileMetadata
setDefaultProjectIdForFilepath(filepath:string, defaultProjectId:string){
const metadatas = { ...this.plugin.settings.fileMetadata }
if (!metadatas[filepath]) {
metadatas[filepath] = {}
}
metadatas[filepath].defaultProjectId = defaultProjectId
// 将更新后的metadatas对象保存回设置对象中
this.plugin.settings.fileMetadata = metadatas
this.plugin.safeSettings?.update({ fileMetadata: metadatas })
}
@ -636,19 +639,21 @@ export class CacheOperation {
* @param newpath -
*/
async updateRenamedFilePath(oldpath: string, newpath: string): Promise<void> {
const taskFileMapping = this.plugin.settings.taskFileMapping || {};
const fileMetadata = this.plugin.settings.fileMetadata || {};
const taskFileMapping = { ...this.plugin.settings.taskFileMapping };
const fileMetadata = { ...this.plugin.settings.fileMetadata };
for (const [taskId, mapping] of Object.entries(taskFileMapping)) {
if (mapping.filePath === oldpath) {
taskFileMapping[taskId] = { ...mapping, filePath: newpath };
}
}
this.plugin.settings.taskFileMapping = taskFileMapping;
if (fileMetadata[oldpath]) {
fileMetadata[newpath] = fileMetadata[oldpath];
delete fileMetadata[oldpath];
this.plugin.settings.fileMetadata = fileMetadata;
}
await this.plugin.safeSettings?.update({
taskFileMapping,
fileMetadata
}, true);
this.plugin.logOperation?.log('CACHE_RENAMED', `Renamed file path from ${oldpath} to ${newpath}`, newpath);
}
@ -714,7 +719,7 @@ export class CacheOperation {
// ==========================================================================================
// Step 1: Clear taskFileMapping (keep fileMetadata for other uses)
this.plugin.settings.taskFileMapping = {};
await this.plugin.safeSettings?.update({ taskFileMapping: {} }, true);
// ==========================================================================================
// Step 2: 确保 syncData 已加载