Skip to content

Commit b73a70e

Browse files
committed
fix: settings corruption
1 parent 2900339 commit b73a70e

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/main.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ export default class PodNotes extends Plugin implements IPodNotes {
7474

7575
private maxLayoutReadyAttempts = 10;
7676
private layoutReadyAttempts = 0;
77+
private isReady = false;
78+
private pendingSave: IPodNotesSettings | null = null;
79+
private saveScheduled = false;
80+
private saveChain: Promise<void> = Promise.resolve();
7781

7882
override async onload() {
7983
plugin.set(this);
@@ -320,6 +324,8 @@ export default class PodNotes extends Plugin implements IPodNotes {
320324
);
321325

322326
this.registerEvent(getContextMenuHandler(this.app));
327+
328+
this.isReady = true;
323329
}
324330

325331
onLayoutReady(): void {
@@ -381,6 +387,45 @@ export default class PodNotes extends Plugin implements IPodNotes {
381387
}
382388

383389
async saveSettings() {
384-
await this.saveData(this.settings);
390+
if (!this.isReady) return;
391+
392+
this.pendingSave = this.cloneSettings();
393+
394+
if (this.saveScheduled) {
395+
return this.saveChain;
396+
}
397+
398+
this.saveScheduled = true;
399+
400+
this.saveChain = this.saveChain
401+
.then(async () => {
402+
while (this.pendingSave) {
403+
const snapshot = this.pendingSave;
404+
this.pendingSave = null;
405+
await this.saveData(snapshot);
406+
}
407+
})
408+
.catch((error) => {
409+
console.error("PodNotes: failed to save settings", error);
410+
})
411+
.finally(() => {
412+
this.saveScheduled = false;
413+
414+
// If a save was requested while we were saving, run again.
415+
if (this.pendingSave) {
416+
void this.saveSettings();
417+
}
418+
});
419+
420+
return this.saveChain;
421+
}
422+
423+
private cloneSettings(): IPodNotesSettings {
424+
// structuredClone is available in Obsidian's Electron runtime; fallback for safety.
425+
if (typeof structuredClone === "function") {
426+
return structuredClone(this.settings);
427+
}
428+
429+
return JSON.parse(JSON.stringify(this.settings)) as IPodNotesSettings;
385430
}
386431
}

0 commit comments

Comments
 (0)