Skip to content

Commit

Permalink
fix: race condition for frontmatter parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Mar 24, 2022
1 parent 5d7adc9 commit 5dbd063
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MarkdownView, normalizePath, Notice, parseFrontMatterAliases, parseFrontMatterEntry, Plugin, TFile, TFolder } from "obsidian";
import { CachedMetadata, MarkdownView, normalizePath, Notice, parseFrontMatterAliases, parseFrontMatterEntry, Plugin, TFile, TFolder } from "obsidian";
import { stripMD } from "obsidian-community-lib";
import { appHasDailyNotesPluginLoaded, createDailyNote, getAllDailyNotes, getDailyNote } from "obsidian-daily-notes-interface";
import { v4 as uuidv4 } from 'uuid';
Expand Down Expand Up @@ -540,7 +540,6 @@ export default class AdvancedURI extends Plugin {
this.app.workspace.setActiveLeaf(leaf, true, true);
}
});
console.log(parameters);

if (!fileIsAlreadyOpened)
await this.app.workspace.openLinkText(outputFileName, "", parameters.newpane !== undefined ? parameters.newpane == "true" : this.settings.openFileOnWriteInNewPane, this.getViewStateFromMode(parameters));
Expand Down Expand Up @@ -713,8 +712,16 @@ export default class AdvancedURI extends Plugin {
};

async getUIDFromFile(file: TFile): Promise<string> {
const frontmatter = this.app.metadataCache.getFileCache(file).frontmatter;
let uid = parseFrontMatterEntry(frontmatter, this.settings.idField);
let cache: CachedMetadata;

//await parsing of frontmatter
for (let i = 0; i <= 20; i++) {
cache = this.app.metadataCache.getFileCache(file);

if (cache !== undefined) break;
await new Promise(resolve => setTimeout(resolve, 150));
}
const uid = parseFrontMatterEntry(cache.frontmatter, this.settings.idField);
if (uid != undefined) return uid;
return await this.writeUIDToFile(file, uuidv4());
};
Expand Down

0 comments on commit 5dbd063

Please sign in to comment.