From e6be6ece48d9ac09988e31053ce9f45659985131 Mon Sep 17 00:00:00 2001 From: Michael Naumov Date: Wed, 17 Jul 2024 07:39:22 -0600 Subject: [PATCH] Fix metadataCache event handler --- src/Async.ts | 4 ++++ src/BetterMarkdownLinksPlugin.ts | 5 +++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Async.ts b/src/Async.ts index 615332f..0cdb3de 100644 --- a/src/Async.ts +++ b/src/Async.ts @@ -26,3 +26,7 @@ async function runWithTimeout(timeoutInMilliseconds: number, asyncFn: () => P throw new Error(`Timed out in ${timeoutInMilliseconds} milliseconds`); } } + +export function convertToSync(promise: Promise): void { + promise.catch(console.error); +} diff --git a/src/BetterMarkdownLinksPlugin.ts b/src/BetterMarkdownLinksPlugin.ts index 776358a..ef0361a 100644 --- a/src/BetterMarkdownLinksPlugin.ts +++ b/src/BetterMarkdownLinksPlugin.ts @@ -11,6 +11,7 @@ import { getAllLinks, getCacheSafe } from "./MetadataCache.ts"; +import { convertToSync } from "./Async.ts"; type GenerateMarkdownLinkFn = (file: TFile, sourcePath: string, subpath?: string, alias?: string) => string; @@ -49,7 +50,7 @@ export default class BetterMarkdownLinksPlugin extends Plugin { callback: this.convertLinksInEntireVault.bind(this) }); - this.registerEvent(this.app.metadataCache.on("changed", () => void this.handleMetadataCacheChanged)); + this.registerEvent(this.app.metadataCache.on("changed", (file) => convertToSync(this.handleMetadataCacheChanged(file)))); } public async saveSettings(newSettings: BetterMarkdownLinksPluginSettings): Promise { @@ -114,7 +115,7 @@ export default class BetterMarkdownLinksPlugin extends Plugin { } if (!checking) { - this.convertLinksInFile(activeFile).catch(console.error); + convertToSync(this.convertLinksInFile(activeFile)); } return true;