From 4d042421ea0638afe420fc42d419e51f8c242f37 Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Tue, 30 Nov 2021 19:15:14 +0200 Subject: [PATCH] fix: :bug: waitForResolvedLinks if notDV --- main.js | 41 +++++++++++++++++++++++++++++++++++++++++ src/main.ts | 4 ++++ 2 files changed, 45 insertions(+) diff --git a/main.js b/main.js index 77ee1027..0db0ff61 100644 --- a/main.js +++ b/main.js @@ -19956,6 +19956,14 @@ module.exports = __webpack_require__(/*! /home/travis/build/feathericons/feather * This module contains various utility functions commonly used in Obsidian plugins. * @module obsidian-community-lib */ +/** + * You can await this Function to delay execution + * + * @param delay The delay in ms + */ +async function wait(delay) { + return new Promise((resolve) => setTimeout(resolve, delay)); +} /** * Adds a specific Feather Icon to Obsidian. * @@ -20102,6 +20110,35 @@ async function openView(app, viewType, viewClass, side = "right") { active: true, }); return leaf.view; +} +/** + * Check if `app.metadataCache.ResolvedLinks` have fully initalised. + * + * Used with {@link waitForResolvedLinks}. + * @param {App} app + * @param {number} noFiles Number of files in your vault. + * @returns {boolean} + */ +function resolvedLinksComplete(app, noFiles) { + const { resolvedLinks } = app.metadataCache; + return Object.keys(resolvedLinks).length === noFiles; +} +/** + * Wait for `app.metadataCache.ResolvedLinks` to have fully initialised. + * @param {App} app + * @param {number} [delay=1000] Number of milliseconds to wait between each check. + * @param {number} [max=50] Maximum number of iterations to check before throwing an error and breaking out of the loop. + */ +async function waitForResolvedLinks(app, delay = 1000, max = 50) { + const noFiles = app.vault.getMarkdownFiles().length; + let i = 0; + while (!resolvedLinksComplete(app, noFiles) && i < max) { + await wait(delay); + i++; + } + if (i === max) { + throw Error("Obsidian-Community-Lib: ResolvedLinks did not finish initialising. `max` iterations was reached first."); + } } const MATRIX_VIEW = "BC-matrix"; @@ -49605,6 +49642,10 @@ class BCPlugin extends require$$0.Plugin { })); } } + else { + await waitForResolvedLinks(this.app); + await this.initEverything(); + } }); require$$0.addIcon(TRAIL_ICON, TRAIL_ICON_SVG); for (const view of this.VIEWS) { diff --git a/src/main.ts b/src/main.ts index c4001de0..7af4c48b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -18,6 +18,7 @@ import { addFeatherIcon, copy, openView, + waitForResolvedLinks, } from "obsidian-community-lib/dist/utils"; import StatsView from "./StatsView"; import DownView from "./DownView"; @@ -227,6 +228,9 @@ export default class BCPlugin extends Plugin { }) ); } + } else { + await waitForResolvedLinks(this.app); + await this.initEverything(); } });