From 3ffaedba3f90e1356cf5ad5a065236d5524a6f2e Mon Sep 17 00:00:00 2001 From: Jonas Karlsson Date: Tue, 28 Dec 2021 14:54:09 +0100 Subject: [PATCH] fix: Hopefully fix race condition. --- src/module/app/mystify-token/index.ts | 6 ++++-- src/module/keybinds.ts | 11 ++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/module/app/mystify-token/index.ts b/src/module/app/mystify-token/index.ts index 2c368d222..a0abd3603 100644 --- a/src/module/app/mystify-token/index.ts +++ b/src/module/app/mystify-token/index.ts @@ -123,7 +123,8 @@ export async function doMystification(token: Token, active: boolean) { }) ); } - (game as Game).scenes?.active?.updateEmbeddedDocuments("Token", updates); + + return updates; } export function renderNameHud(data: TokenData, html: JQuery) { @@ -141,7 +142,8 @@ export function renderNameHud(data: TokenData, html: JQuery) { const hudElement = $(e.currentTarget); const active = hudElement.hasClass("active"); if (token !== null && isTokenMystified(token) === active) { - await doMystification(token, active); + const updates = await doMystification(token, active); + await (game as Game).scenes?.active?.updateEmbeddedDocuments("Token", updates); } hudElement.toggleClass("active"); }); diff --git a/src/module/keybinds.ts b/src/module/keybinds.ts index a43b9b91b..8404b67ae 100644 --- a/src/module/keybinds.ts +++ b/src/module/keybinds.ts @@ -1,7 +1,7 @@ // Keyboard key controlling whether the actor should be mystified, if this feature is toggled on import { MODULENAME } from "./xdy-pf2e-workbench"; -import { doMystification, isTokenMystified } from "./app/mystify-token"; import { moveSelectedAheadOfCurrent } from "./app/moveCombatant"; +import { doMystification, isTokenMystified } from "./app/mystify-token"; export let mystifyModifierKey: string; @@ -35,8 +35,13 @@ export function registerKeybindings() { key: "KeyM", }, ], - onDown: () => { - canvas?.tokens?.controlled.filter(async (token) => await doMystification(token, isTokenMystified(token))); + onDown: async () => { + const updates = []; + for (const token of canvas?.tokens?.controlled || []) { + updates.push(...(await doMystification(token, isTokenMystified(token)))); + } + + await (game as Game).scenes?.active?.updateEmbeddedDocuments("Token", updates); }, restricted: false, reservedModifiers: [],