diff --git a/Monkeydo.mjs b/Monkeydo.mjs index 4cdf17e..9fed33f 100644 --- a/Monkeydo.mjs +++ b/Monkeydo.mjs @@ -1,7 +1,7 @@ import { default as MonkeyWorker } from "./do/MonkeyManager.mjs"; export default class Monkeydo extends MonkeyWorker { - constructor(methods = {},manifest = false) { + constructor(methods = {}) { super(methods); this.monkeydo = { version: "0.2.1", @@ -25,10 +25,6 @@ export default class Monkeydo extends MonkeyWorker { if(!window.Worker) { throw new Error("JavaScript Workers aren't supported by your browser"); } - - if(manifest) { - this.load(manifest); - } } debug(...attachments) { diff --git a/do/Monkey.js b/do/Monkey.js index 95774b0..de30396 100644 --- a/do/Monkey.js +++ b/do/Monkey.js @@ -3,6 +3,7 @@ class Monkey { constructor(manifest) { const self = this; + this.tasks = manifest.tasks; this.tasksLength = this.tasks.length - 1; @@ -32,10 +33,10 @@ class Monkey { Object.seal(this.queue); } - // Parse task components and send them to main thread + // Pass task to main thread for execution run(task) { - this.i++; // Advance index postMessage(["TASK",task]); + this.i++; } // Interrupt timeout and put monkey to sleep @@ -91,7 +92,7 @@ class Monkey { } } -// Global message event handler for this worker +// Event handler for messages received from initiator onmessage = (message) => { const type = message.data[0] ? message.data[0] : message.data; const data = message.data[1]; diff --git a/do/MonkeyManager.mjs b/do/MonkeyManager.mjs index 4d20861..3b3e7b0 100644 --- a/do/MonkeyManager.mjs +++ b/do/MonkeyManager.mjs @@ -37,26 +37,11 @@ export default class MonkeyManager { this.worker.postMessage(["SET_FLAG",[flag,value]]); } - // Call method from object and pass arguments - runTask(task) { - this.methods[task.func](...task.args); - } - - play() { - this.worker.postMessage(["SET_PLAYING",true]); - } - - pause() { - this.worker.postMessage(["SET_PLAYING",false]); - } - - // Pass manifest to worker and await response - async giveManifest() { - this.worker.postMessage(["GIVE_MANIFEST",this.manifest]); - + // Get acknowledgement from worker for a transactional operation + async ack(name) { const status = await new Promise((resolve,reject) => { const ack = this.worker.addEventListener("message",message => { - if(message.data[0] !== "RECEIVED_MANIFEST") { + if(message.data[0] !== name) { return false; } @@ -70,12 +55,40 @@ export default class MonkeyManager { return status; } + // Pass manifest to worker and await response from worker + async giveManifest() { + this.worker.postMessage(["GIVE_MANIFEST",this.manifest]); + const status = await this.ack("RECEIVED_MANIFEST"); + return status; + } + + // Call method from object and pass arguments + run(task) { + this.methods[task.func](...task.args); + } + + play() { + this.worker.postMessage(["SET_PLAYING",true]); + } + + pause() { + this.worker.postMessage(["SET_PLAYING",false]); + } + + // Event handler for messages received from worker message(message) { const type = message.data[0] ? message.data[0] : message.data; const data = message.data[1]; - if(type !== "TASK") { - return false; + + switch(type) { + case "TASK": + this.run(data); + break; + + case "DEBUG": + default: + this.debug("MESSAGE_FROM_WORKER",message.data); + break; } - this.runTask(data); } } \ No newline at end of file