From c35d28fcc052a1320d518f32e9020f7c31a8703f Mon Sep 17 00:00:00 2001 From: Jeremy Valentine <38669521+valentine195@users.noreply.github.com> Date: Tue, 9 Jan 2024 09:06:26 -0500 Subject: [PATCH] Revert "fix: Fixes dice-mod race condition (close #277)" This reverts commit 06a55a5fcd00d72f90567e45852533927c6d2d1b. --- src/main.ts | 56 ++++++++++++++++++++++++----------------------------- 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/src/main.ts b/src/main.ts index b57f5b3..9a0455d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -374,9 +374,6 @@ export default class DiceRollerPlugin extends Plugin { this.fileMap.set(file, [...this.fileMap.get(file), roller]); } - processing: WeakMap = new WeakMap(); - promises: WeakMap Promise>> = - new WeakMap(); async postprocessor(el: HTMLElement, ctx: MarkdownPostProcessorContext) { let nodeList = el.querySelectorAll("code"); @@ -389,15 +386,12 @@ export default class DiceRollerPlugin extends Plugin { if ((!file || !(file instanceof TFile)) && path != "STATBLOCK_RENDERER") return; - if (file && file instanceof TFile) { - this.processing.set(file, (this.processing.get(file) ?? 0) + 1); - if (!this.promises.has(file)) { - this.promises.set(file, []); - } - } const toPersist: Record = {}; + let fileContent: string[]; + let replacementFound: boolean = false; + const modPromises: Promise[] = []; for (let index = 0; index < nodeList.length; index++) { const node = nodeList.item(index); @@ -410,6 +404,13 @@ export default class DiceRollerPlugin extends Plugin { if (isTemplateFolder(this.data.diceModTemplateFolders, file)) continue; try { + if (!replacementFound) { + fileContent = ( + await this.app.vault.cachedRead(file) + ).split("\n"); + replacementFound = true; + } + let [full, content] = node.innerText.match( /^dice\-mod:\s*([\s\S]+)\s*?/ ); @@ -438,12 +439,10 @@ export default class DiceRollerPlugin extends Plugin { }); } - await roller.roll(); - - this.promises.get(file).unshift( - async (content: string[]) => - new Promise(async (resolve, reject) => { - let splitContent = content.slice( + modPromises.push( + new Promise((resolve, reject) => { + roller.on("new-result", async () => { + let splitContent = fileContent.slice( info.lineStart, info.lineEnd + 1 ); @@ -473,15 +472,18 @@ export default class DiceRollerPlugin extends Plugin { .split("\n"); } - content.splice( + fileContent.splice( info.lineStart, info.lineEnd - info.lineStart + 1, ...splitContent ); - resolve(content); - }) + resolve(); + }); + }) ); + await roller.roll(); + continue; } catch (e) { console.error(e); @@ -576,6 +578,11 @@ export default class DiceRollerPlugin extends Plugin { } } if (!file || !(file instanceof TFile)) return; + if (replacementFound && modPromises.length) { + await Promise.all(modPromises); + sleep(500) + await this.app.vault.modify(file, fileContent.join("\n")); + } if (path in this.data.results) { this.data.results[path][lineStart] = {}; @@ -654,19 +661,6 @@ export default class DiceRollerPlugin extends Plugin { }); } } - this.consume(file); - } - - async consume(file: TFile) { - if (!this.processing.has(file)) return; - this.processing.set(file, this.processing.get(file) - 1); - if (this.processing.get(file) == 0) { - let content = (await this.app.vault.cachedRead(file)).split("\n"); - for (const promise of this.promises.get(file)) { - content = await promise(content); - } - this.app.vault.process(file, (data) => content.join("\n")); - } } get canUseDataview() {