changeset 54:06504ceb3283

Make all shutdown logic synchronous. It looks like there's a bug in Obsidian where adding asynchronous operations on shutdown (via `tasks.addPromise`) crashes the app when the user runs the `Reload App Without Saving` command. So we have to do it all synchronously instead.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 02 Oct 2023 10:10:09 -0700
parents f2e066bbe343
children 388eb78ef4b7
files src/main.ts
diffstat 1 files changed, 12 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/main.ts	Mon Oct 02 10:07:26 2023 -0700
+++ b/src/main.ts	Mon Oct 02 10:10:09 2023 -0700
@@ -461,14 +461,15 @@
 		delete this.data.rememberedFiles[file.path];
 	};
 
-	private readonly onAppQuit = async (tasks: Tasks): Promise<void> => {
-		const _this = this;
-		tasks.addPromise(
-			_this.rememberAllOpenedFileStates()
-			.then(() => _this.writeStateDatabase(STATE_DB_PATH)));
+	private readonly onAppQuit = function(tasks: Tasks) {
+		this.unregisterAllViews();
+		this.rememberAllOpenedFileStates();
+		this.writeStateDatabase(STATE_DB_PATH);
+		console.log("RememberFileState: done with app-quit cleanup.");
 	}
 
-	private readonly rememberAllOpenedFileStates = async(): Promise<void> => {
+	private readonly rememberAllOpenedFileStates = function() {
+		console.log("RememberFileState: remembering all opened file states...");
 		this.app.workspace.getLeavesOfType("markdown").forEach(
 			(leaf: WorkspaceLeaf) => { 
 				const view = leaf.view as MarkdownView;
@@ -477,13 +478,15 @@
 		);
 	}
 
-	private readonly writeStateDatabase = async(path: string): Promise<void> => {
+	private readonly writeStateDatabase = function(path: string) {
 		const fs = this.app.vault.adapter;
 		const jsonDb = JSON.stringify(this.data);
-		await fs.write(path, jsonDb);
+		console.log("RememberFileState: writing state database...");
+		fs.write(path, jsonDb)
+		.then(() => { console.log("RememberFileState: wrote state database."); });
 	}
 
-	private readonly readStateDatabase = async(path: string): Promise<void> => {
+	private readonly readStateDatabase = async function(path: string): Promise<void> {
 		const fs = this.app.vault.adapter;
 		if (await fs.exists(path)) {
 			const jsonDb = await fs.read(path);