Skip to content

Commit

Permalink
Revert "fix: Fixes dice-mod race condition (close #277)"
Browse files Browse the repository at this point in the history
This reverts commit 06a55a5.
  • Loading branch information
valentine195 committed Jan 9, 2024
1 parent f808960 commit c35d28f
Showing 1 changed file with 25 additions and 31 deletions.
56 changes: 25 additions & 31 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,6 @@ export default class DiceRollerPlugin extends Plugin {
this.fileMap.set(file, [...this.fileMap.get(file), roller]);
}

processing: WeakMap<TFile, number> = new WeakMap();
promises: WeakMap<TFile, Array<(content: string[]) => Promise<string[]>>> =
new WeakMap();
async postprocessor(el: HTMLElement, ctx: MarkdownPostProcessorContext) {
let nodeList = el.querySelectorAll("code");

Expand All @@ -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<number, BasicRoller> = {};

let fileContent: string[];
let replacementFound: boolean = false;
const modPromises: Promise<void>[] = [];
for (let index = 0; index < nodeList.length; index++) {
const node = nodeList.item(index);

Expand All @@ -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*?/
);
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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] = {};
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit c35d28f

Please sign in to comment.