mirror of
https://codeberg.org/vlw/monkeydo.git
synced 2025-09-13 23:53:41 +02:00
Added ack function to class scope
Moved ack function from giveManifest to the MonkeyManager class so other functions can use it in the future.
This commit is contained in:
parent
f53bea079e
commit
c713bb8755
3 changed files with 39 additions and 29 deletions
|
@ -1,7 +1,7 @@
|
||||||
import { default as MonkeyWorker } from "./do/MonkeyManager.mjs";
|
import { default as MonkeyWorker } from "./do/MonkeyManager.mjs";
|
||||||
|
|
||||||
export default class Monkeydo extends MonkeyWorker {
|
export default class Monkeydo extends MonkeyWorker {
|
||||||
constructor(methods = {},manifest = false) {
|
constructor(methods = {}) {
|
||||||
super(methods);
|
super(methods);
|
||||||
this.monkeydo = {
|
this.monkeydo = {
|
||||||
version: "0.2.1",
|
version: "0.2.1",
|
||||||
|
@ -25,10 +25,6 @@ export default class Monkeydo extends MonkeyWorker {
|
||||||
if(!window.Worker) {
|
if(!window.Worker) {
|
||||||
throw new Error("JavaScript Workers aren't supported by your browser");
|
throw new Error("JavaScript Workers aren't supported by your browser");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(manifest) {
|
|
||||||
this.load(manifest);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
debug(...attachments) {
|
debug(...attachments) {
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
class Monkey {
|
class Monkey {
|
||||||
constructor(manifest) {
|
constructor(manifest) {
|
||||||
const self = this;
|
const self = this;
|
||||||
|
|
||||||
this.tasks = manifest.tasks;
|
this.tasks = manifest.tasks;
|
||||||
this.tasksLength = this.tasks.length - 1;
|
this.tasksLength = this.tasks.length - 1;
|
||||||
|
|
||||||
|
@ -32,10 +33,10 @@ class Monkey {
|
||||||
Object.seal(this.queue);
|
Object.seal(this.queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse task components and send them to main thread
|
// Pass task to main thread for execution
|
||||||
run(task) {
|
run(task) {
|
||||||
this.i++; // Advance index
|
|
||||||
postMessage(["TASK",task]);
|
postMessage(["TASK",task]);
|
||||||
|
this.i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Interrupt timeout and put monkey to sleep
|
// 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) => {
|
onmessage = (message) => {
|
||||||
const type = message.data[0] ? message.data[0] : message.data;
|
const type = message.data[0] ? message.data[0] : message.data;
|
||||||
const data = message.data[1];
|
const data = message.data[1];
|
||||||
|
|
|
@ -37,26 +37,11 @@ export default class MonkeyManager {
|
||||||
this.worker.postMessage(["SET_FLAG",[flag,value]]);
|
this.worker.postMessage(["SET_FLAG",[flag,value]]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call method from object and pass arguments
|
// Get acknowledgement from worker for a transactional operation
|
||||||
runTask(task) {
|
async ack(name) {
|
||||||
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]);
|
|
||||||
|
|
||||||
const status = await new Promise((resolve,reject) => {
|
const status = await new Promise((resolve,reject) => {
|
||||||
const ack = this.worker.addEventListener("message",message => {
|
const ack = this.worker.addEventListener("message",message => {
|
||||||
if(message.data[0] !== "RECEIVED_MANIFEST") {
|
if(message.data[0] !== name) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,12 +55,40 @@ export default class MonkeyManager {
|
||||||
return status;
|
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) {
|
message(message) {
|
||||||
const type = message.data[0] ? message.data[0] : message.data;
|
const type = message.data[0] ? message.data[0] : message.data;
|
||||||
const data = message.data[1];
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue