Skip to content

Commit

Permalink
fix: 🐛 waitForResolvedLinks if notDV
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Nov 30, 2021
1 parent 006da16 commit 4d04242
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
41 changes: 41 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
addFeatherIcon,
copy,
openView,
waitForResolvedLinks,
} from "obsidian-community-lib/dist/utils";
import StatsView from "./StatsView";
import DownView from "./DownView";
Expand Down Expand Up @@ -227,6 +228,9 @@ export default class BCPlugin extends Plugin {
})
);
}
} else {
await waitForResolvedLinks(this.app);
await this.initEverything();
}
});

Expand Down

0 comments on commit 4d04242

Please sign in to comment.