From 2173cc6150dd4beafe5af66bb32ef2053c9c1032 Mon Sep 17 00:00:00 2001 From: Vlyan Date: Tue, 21 Dec 2021 12:50:59 +0100 Subject: [PATCH] v9 - roll now async (sync is removed in v10) --- system/scripts/dice/dietype/l5r-base-die.js | 4 ++-- system/scripts/dice/roll-n-keep-dialog.js | 2 +- system/scripts/dice/roll.js | 6 +++--- system/scripts/hooks.js | 4 ++-- system/scripts/main-l5r5e.js | 6 +++--- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/system/scripts/dice/dietype/l5r-base-die.js b/system/scripts/dice/dietype/l5r-base-die.js index 7ff5e76..806d4f4 100644 --- a/system/scripts/dice/dietype/l5r-base-die.js +++ b/system/scripts/dice/dietype/l5r-base-die.js @@ -79,14 +79,14 @@ export class L5rBaseDie extends DiceTerm { * @return {L5rBaseDie} The evaluated RollTerm * @override */ - evaluate({ minimize = false, maximize = false, async = false } = {}) { + async evaluate({ minimize = false, maximize = false, async = true } = {}) { if (this._evaluated) { throw new Error(`This ${this.constructor.name} has already been evaluated and is immutable`); } // Roll the initial number of dice for (let n = 1; n <= this.number; n++) { - this.roll({ minimize, maximize, async }); // TODO async/await in v10.x currently the inline roll is sync + await this.roll({ minimize, maximize, async }); } // Apply modifiers diff --git a/system/scripts/dice/roll-n-keep-dialog.js b/system/scripts/dice/roll-n-keep-dialog.js index c8f0d39..dec2ecd 100644 --- a/system/scripts/dice/roll-n-keep-dialog.js +++ b/system/scripts/dice/roll-n-keep-dialog.js @@ -583,7 +583,7 @@ export class RollnKeepDialog extends FormApplication { }; // Fill the data - roll.evaluate({ async: false }); + await roll.evaluate(); // Modify results roll.terms.map((term) => { diff --git a/system/scripts/dice/roll.js b/system/scripts/dice/roll.js index 32dacb5..a37993a 100644 --- a/system/scripts/dice/roll.js +++ b/system/scripts/dice/roll.js @@ -56,7 +56,7 @@ export class RollL5r5e extends Roll { * Execute the Roll, replacing dice and evaluating the total result * @override **/ - evaluate({ minimize = false, maximize = false, async = false } = {}) { + async evaluate({ minimize = false, maximize = false, async = true } = {}) { if (this._evaluated) { throw new Error("This Roll object has already been rolled."); } @@ -71,7 +71,7 @@ export class RollL5r5e extends Roll { this._total = 0; // Roll - super.evaluate({ minimize, maximize, async }); + await super.evaluate({ minimize, maximize, async }); this._evaluated = true; // Save initial formula @@ -297,7 +297,7 @@ export class RollL5r5e extends Roll { async toMessage(messageData = {}, { rollMode = null, create = true } = {}) { // Perform the roll, if it has not yet been rolled if (!this._evaluated) { - this.evaluate({ async: false }); + await this.evaluate(); } // RollMode diff --git a/system/scripts/hooks.js b/system/scripts/hooks.js index e808748..9125fa6 100644 --- a/system/scripts/hooks.js +++ b/system/scripts/hooks.js @@ -68,7 +68,7 @@ export default class HooksL5r5e { * @param {string} userId * @return {boolean} */ - static preCreateChatMessage(document, data, options, userId) { + static async preCreateChatMessage(document, data, options, userId) { // Roll from DP have the "isL5r5eTemplate" option set if (!document.isRoll || options?.isL5r5eTemplate || !document.data?.roll) { return; @@ -83,7 +83,7 @@ export default class HooksL5r5e { } // So now we have our wrong message only, redo it using the roll - roll.toMessage(); + await roll.toMessage(); // Return false to let the system known we handled this return false; diff --git a/system/scripts/main-l5r5e.js b/system/scripts/main-l5r5e.js index b14fff8..a9aef08 100644 --- a/system/scripts/main-l5r5e.js +++ b/system/scripts/main-l5r5e.js @@ -248,6 +248,6 @@ Hooks.on("renderChatMessage", (message, html, data) => HooksL5r5e.renderChatMess Hooks.on("renderCombatTracker", (app, html, data) => HooksL5r5e.renderCombatTracker(app, html, data)); Hooks.on("renderCompendium", async (app, html, data) => HooksL5r5e.renderCompendium(app, html, data)); Hooks.on("diceSoNiceRollStart", (messageId, context) => HooksL5r5e.diceSoNiceRollStart(messageId, context)); -Hooks.on("preCreateChatMessage", (document, data, options, userId) => - HooksL5r5e.preCreateChatMessage(document, data, options, userId) -); + +// TODO Seem fixed in v9, remove in v10 if good +// Hooks.on("preCreateChatMessage", (document, data, options, userId) => HooksL5r5e.preCreateChatMessage(document, data, options, userId));