Fix a weird race condition where the worker wouldn't be ready when it needed to be

This commit is contained in:
Ean Milligan (Bastion) 2022-06-28 23:44:14 -04:00
parent 872f67d908
commit 61607dc75d
2 changed files with 11 additions and 5 deletions

View File

@ -52,12 +52,14 @@ const handleRollWorker = async (rq: QueuedRoll) => {
} }
}, config.limits.workerTimeout); }, config.limits.workerTimeout);
rollWorker.addEventListener('message', async (workerMessage) => {
if (workerMessage.data === 'ready') {
rollWorker.postMessage({ rollWorker.postMessage({
rollCmd: rq.rollCmd, rollCmd: rq.rollCmd,
modifiers: rq.modifiers, modifiers: rq.modifiers,
}); });
return;
rollWorker.addEventListener('message', async (workerMessage) => { }
let apiErroredOut = false; let apiErroredOut = false;
try { try {
currentWorkers--; currentWorkers--;

View File

@ -1,5 +1,9 @@
import { parseRoll } from './parser.ts'; import { parseRoll } from './parser.ts';
// Alert rollQueue that this worker is ready
self.postMessage('ready');
// Handle the roll
self.onmessage = async (e: any) => { self.onmessage = async (e: any) => {
const payload = e.data; const payload = e.data;
const returnmsg = parseRoll(payload.rollCmd, payload.modifiers) || { const returnmsg = parseRoll(payload.rollCmd, payload.modifiers) || {