Files
obsidian-excalidraw-plugin/tests/approved-settings-lifecycle.patch

209 lines
7.2 KiB
Diff

diff --git a/src/core/main.ts b/src/core/main.ts
index d3550b29..13787993 100644
--- a/src/core/main.ts
+++ b/src/core/main.ts
@@ -488,11 +488,17 @@ export default class ExcalidrawPlugin extends Plugin {
}
private async onloadCheckForOnceOffSettingsUpdates() {
+ if (!this.settings) {
+ return;
+ }
if (!this.settings.onceOffCompressFlagReset) {
this.settings.compress = true;
this.settings.onceOffCompressFlagReset = true;
await this.saveSettings();
}
+ if (!this.settings) {
+ return;
+ }
this.settingsTab = new ExcalidrawSettingTab(this.app, this);
this.addSettingTab(this.settingsTab);
this.settingsReady = true;
@@ -505,6 +511,9 @@ export default class ExcalidrawPlugin extends Plugin {
"\n----------------------------------\nWorkspace onLayoutReady event fired (these actions are outside the plugin initialization)",
);
await this.awaitSettings();
+ if (!this.settings) {
+ return;
+ }
this.logStartupEvent("Settings awaited");
this.extrasGateway = new ExcalidrawExtrasGateway(this.app, this);
if (!this.settings.overrideObsidianFontSize) {
diff --git a/src/core/managers/PluginSettingsManager.ts b/src/core/managers/PluginSettingsManager.ts
index b761df06..504b5fa2 100644
--- a/src/core/managers/PluginSettingsManager.ts
+++ b/src/core/managers/PluginSettingsManager.ts
@@ -59,6 +59,7 @@ export class PluginSettingsManager {
private persistenceBlockedByInvalidData = false;
private missingRecoveryNoticeShown = false;
private awaitingStartupRecoveryChoice = false;
+ private destroyed = false;
private readonly promptForRecoveryChoice: SettingsRecoveryPromptHandler;
public constructor(
@@ -76,7 +77,14 @@ export class PluginSettingsManager {
((mode) => new SettingsRecoveryPrompt(host.app, mode).start());
this.writer = new SerializedSettingsWriter({
saveData: async (data) => {
+ // Retired instances must not start storage work for a replacement plugin.
+ if (this.destroyed) {
+ return;
+ }
await this.saveRecoverySnapshot(data);
+ if (this.destroyed) {
+ return;
+ }
await this.host.saveData(data);
},
});
@@ -87,11 +95,17 @@ export class PluginSettingsManager {
* protected API-key fields.
*
* @remarks Unknown persisted properties are intentionally retained.
- * @returns `false` when the disk payload was invalid. Runtime callers use
- * this to suppress cache invalidation for a rejected synchronized payload.
+ * @returns `false` when the payload was invalid or this instance retired.
+ * Runtime callers use this to suppress cache invalidation for rejected data.
*/
public async loadSettings(): Promise<boolean> {
+ if (this.destroyed) {
+ return false;
+ }
const dataFileState = await this.getSettingsDataFileState();
+ if (this.destroyed) {
+ return false;
+ }
let loadedSettings: unknown = null;
let loadFailed = false;
try {
@@ -99,6 +113,9 @@ export class PluginSettingsManager {
} catch {
loadFailed = true;
}
+ if (this.destroyed) {
+ return false;
+ }
const classification = classifySettingsDataLoad({
value: loadedSettings,
@@ -129,6 +146,9 @@ export class PluginSettingsManager {
return false;
}
const recoveredSettings = await this.loadRecoverySnapshot();
+ if (this.destroyed) {
+ return false;
+ }
if (recoveredSettings) {
persistedSettings = recoveredSettings;
recoveredInvalidStartup = true;
@@ -140,6 +160,9 @@ export class PluginSettingsManager {
const choice = await this.requestStartupRecoveryChoice(
"invalid-without-recovery",
);
+ if (this.destroyed) {
+ return false;
+ }
persistedSettings = {};
if (choice === "reset-defaults") {
this.persistenceBlockedByInvalidData = false;
@@ -151,11 +174,17 @@ export class PluginSettingsManager {
}
} else if (isFirstInstallation) {
const recoveredSettings = await this.loadRecoverySnapshot();
+ if (this.destroyed) {
+ return false;
+ }
if (recoveredSettings) {
this.host.settings = Object.assign({}, DEFAULT_SETTINGS);
const choice = await this.requestStartupRecoveryChoice(
"missing-with-recovery",
);
+ if (this.destroyed) {
+ return false;
+ }
persistedSettings =
choice === "reset-defaults" ? {} : recoveredSettings;
} else {
@@ -298,6 +327,9 @@ export class PluginSettingsManager {
stringifySettingsCanonically(persistedSettings);
if (shouldPersistStartupChoice) {
await this.persistSnapshot(encryptedPersistedSettings, true);
+ if (this.destroyed) {
+ return false;
+ }
if (recoveredInvalidStartup) {
new Notice(t("SETTINGS_DATA_RECOVERED"), 12000);
}
@@ -308,14 +340,23 @@ export class PluginSettingsManager {
await this.persistSnapshot(encryptedPersistedSettings);
} else if (!this.persistenceBlockedByInvalidData) {
await this.saveRecoverySnapshot(encryptedPersistedSettings);
+ if (this.destroyed) {
+ return false;
+ }
this.writer.rememberPersistedSnapshot(persistedSettings);
}
+ if (this.destroyed) {
+ return false;
+ }
this.hasCompletedInitialLoad = true;
return !hasInvalidSettings;
}
- /** Encrypts protected fields and persists the current settings object. */
+ /** Persists live settings; late teardown callbacks cannot create an empty record. */
public saveSettings(): Promise<void> {
+ if (this.destroyed || !isSettingsDataRecord(this.host.settings)) {
+ return Promise.resolve();
+ }
if (this.persistenceBlockedByInvalidData) {
this.showMissingRecoveryNotice();
return Promise.resolve();
@@ -336,11 +377,18 @@ export class PluginSettingsManager {
settings: PersistedExcalidrawSettings,
force = false,
): Promise<void> {
+ if (this.destroyed) {
+ return Promise.resolve();
+ }
return this.writer.persist(settings, force ? { force: true } : undefined);
}
- /** Closes the device-local recovery database connection. */
+ /** Retires pending work before closing the device-local recovery connection. */
public destroy(): void {
+ if (this.destroyed) {
+ return;
+ }
+ this.destroyed = true;
this.recoveryStore.destroy();
}
@@ -394,6 +442,9 @@ export class PluginSettingsManager {
const recoveredSettings = await this.recoveryStore.load();
return isSettingsDataRecord(recoveredSettings) ? recoveredSettings : null;
} catch (error) {
+ if (this.destroyed) {
+ return null;
+ }
console.error("Could not load Excalidraw settings recovery", error);
return null;
}
@@ -402,9 +453,15 @@ export class PluginSettingsManager {
private async saveRecoverySnapshot(
settings: PersistedExcalidrawSettings,
): Promise<void> {
+ if (this.destroyed) {
+ return;
+ }
try {
await this.recoveryStore.save(settings);
} catch (error) {
+ if (this.destroyed) {
+ return;
+ }
console.error("Could not update Excalidraw settings recovery", error);
}
}