Added flag queue

Calls sent to setFlag before a monkey has been spawned will be queued until the next GIVE_MANIFEST event is received.

- Replaced this.monkey with let monkey
This commit is contained in:
Victor Westerlund 2021-10-18 13:12:04 +02:00
parent c713bb8755
commit b2556f1ced
2 changed files with 27 additions and 5 deletions

View file

@ -92,6 +92,9 @@ class Monkey {
}
}
// Unspawned monkey target
let monkey = undefined;
// Event handler for messages received from initiator
onmessage = (message) => {
const type = message.data[0] ? message.data[0] : message.data;
@ -101,7 +104,7 @@ onmessage = (message) => {
// Attempt to load manfiest provided by initiator thread
case "GIVE_MANIFEST":
try {
this.monkey = new Monkey(data);
monkey = new Monkey(data);
postMessage(["RECEIVED_MANIFEST","OK"]);
}
catch(error) {
@ -111,19 +114,19 @@ onmessage = (message) => {
case "SET_PLAYING":
if(data === true) {
this.monkey.play();
monkey.play();
return;
}
this.monkey.interrupt();
monkey.interrupt();
break;
case "GET_FLAG":
const flag = this.monkey.flags[data];
const flag = monkey.flags[data];
postMessage(parseInt(flag));
break;
case "SET_FLAG":
this.monkey.flags[data[0]] = data[1];
monkey.flags[data[0]] = data[1];
break;
default: return; // No op

View file

@ -15,6 +15,11 @@ export default class MonkeyManager {
this.worker.addEventListener("message",message => this.message(message));
this.reversed = false;
this.init = {
ready: false,
flags: []
}
}
// Get a status flag from the worker
@ -29,6 +34,12 @@ export default class MonkeyManager {
// Set a status flag for the worker
async setFlag(flag,value = 0) {
// Player is not initialized, add flag to queue
if(!this.init.ready) {
this.init.flags.push([flag,value]);
return false;
}
const flagExists = await this.getFlag(flag);
if(flagExists === null) {
this.debug(flagExists);
@ -48,6 +59,7 @@ export default class MonkeyManager {
if(message.data[1] !== "OK") {
reject(message.data);
}
this.init.ready = true;
resolve();
});
this.worker.removeEventListener("message",ack);
@ -62,6 +74,13 @@ export default class MonkeyManager {
return status;
}
initFlags() {
if(this.init.flags.length > 0) {
this.init.flags.forEach(flag => this.setFlag(...flag));
}
this.init.flags = [];
}
// Call method from object and pass arguments
run(task) {
this.methods[task.func](...task.args);