Skip to content

Commit

Permalink
perf(LiteStorage): save 操作时若无数据变更则忽略写文件操作
Browse files Browse the repository at this point in the history
  • Loading branch information
renxia committed Dec 26, 2024
1 parent 383fdc8 commit 232e110
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/node/LiteStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class LiteStorage<T extends object = Record<string, unknown>> {
private barrier = new Barrier();
private isToml = false;
private isJson5 = false;
private isChanged = false;

// @ts-ignore
private cache: LSCache<T>;
Expand Down Expand Up @@ -97,6 +98,7 @@ export class LiteStorage<T extends object = Record<string, unknown>> {
/** 主动保存 */
public async save(value?: T, mode: 'merge' | 'cover' = 'merge') {
if (value) return this.set(value, mode);
if (!this.isChanged) return this;
await this.reload();
return this.toCache();
}
Expand Down Expand Up @@ -143,6 +145,7 @@ export class LiteStorage<T extends object = Record<string, unknown>> {
if (mode === 'merge') assign(this.cache.data[uuid], value);
else this.cache.data[uuid] = value;
}
this.isChanged = true;
this.save();
} else {
console.warn('[LiteStorage][set]error', uuid, value);
Expand Down

0 comments on commit 232e110

Please sign in to comment.