comparison src/main.ts @ 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
comparison
equal deleted inserted replaced
53:f2e066bbe343 54:06504ceb3283
459 file: TAbstractFile, 459 file: TAbstractFile,
460 ): Promise<void> => { 460 ): Promise<void> => {
461 delete this.data.rememberedFiles[file.path]; 461 delete this.data.rememberedFiles[file.path];
462 }; 462 };
463 463
464 private readonly onAppQuit = async (tasks: Tasks): Promise<void> => { 464 private readonly onAppQuit = function(tasks: Tasks) {
465 const _this = this; 465 this.unregisterAllViews();
466 tasks.addPromise( 466 this.rememberAllOpenedFileStates();
467 _this.rememberAllOpenedFileStates() 467 this.writeStateDatabase(STATE_DB_PATH);
468 .then(() => _this.writeStateDatabase(STATE_DB_PATH))); 468 console.log("RememberFileState: done with app-quit cleanup.");
469 } 469 }
470 470
471 private readonly rememberAllOpenedFileStates = async(): Promise<void> => { 471 private readonly rememberAllOpenedFileStates = function() {
472 console.log("RememberFileState: remembering all opened file states...");
472 this.app.workspace.getLeavesOfType("markdown").forEach( 473 this.app.workspace.getLeavesOfType("markdown").forEach(
473 (leaf: WorkspaceLeaf) => { 474 (leaf: WorkspaceLeaf) => {
474 const view = leaf.view as MarkdownView; 475 const view = leaf.view as MarkdownView;
475 this.rememberFileState(view); 476 this.rememberFileState(view);
476 } 477 }
477 ); 478 );
478 } 479 }
479 480
480 private readonly writeStateDatabase = async(path: string): Promise<void> => { 481 private readonly writeStateDatabase = function(path: string) {
481 const fs = this.app.vault.adapter; 482 const fs = this.app.vault.adapter;
482 const jsonDb = JSON.stringify(this.data); 483 const jsonDb = JSON.stringify(this.data);
483 await fs.write(path, jsonDb); 484 console.log("RememberFileState: writing state database...");
484 } 485 fs.write(path, jsonDb)
485 486 .then(() => { console.log("RememberFileState: wrote state database."); });
486 private readonly readStateDatabase = async(path: string): Promise<void> => { 487 }
488
489 private readonly readStateDatabase = async function(path: string): Promise<void> {
487 const fs = this.app.vault.adapter; 490 const fs = this.app.vault.adapter;
488 if (await fs.exists(path)) { 491 if (await fs.exists(path)) {
489 const jsonDb = await fs.read(path); 492 const jsonDb = await fs.read(path);
490 try 493 try
491 { 494 {