diff --git a/do/Monkey.js b/do/Monkey.js index de30396..ccfef0c 100644 --- a/do/Monkey.js +++ b/do/Monkey.js @@ -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 diff --git a/do/MonkeyManager.mjs b/do/MonkeyManager.mjs index 3b3e7b0..971e709 100644 --- a/do/MonkeyManager.mjs +++ b/do/MonkeyManager.mjs @@ -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);