mirror of
https://codeberg.org/vlw/monkeydo.git
synced 2025-09-13 15:53:40 +02:00
dev21w40b
This commit is contained in:
parent
c3d584dc8f
commit
b6ab338e07
4 changed files with 54 additions and 22 deletions
31
Monkeydo.mjs
31
Monkeydo.mjs
|
@ -1,7 +1,8 @@
|
||||||
import { default as MonkeyWorker } from "./classes/Worker.mjs";
|
import { default as MonkeyWorker } from "./worker/MonkeyWorker.mjs";
|
||||||
|
|
||||||
export default class Monekydo {
|
export default class Monkeydo extends MonkeyWorker {
|
||||||
constructor(manifest = false) {
|
constructor(manifest = false) {
|
||||||
|
super();
|
||||||
this.monkeydo = {
|
this.monkeydo = {
|
||||||
version: "0.1",
|
version: "0.1",
|
||||||
debugLevel: 0,
|
debugLevel: 0,
|
||||||
|
@ -14,12 +15,18 @@ export default class Monekydo {
|
||||||
};
|
};
|
||||||
Object.seal(this.monkeydo);
|
Object.seal(this.monkeydo);
|
||||||
|
|
||||||
this.header = null;
|
this.manifest = {
|
||||||
this.body = null;
|
header: null,
|
||||||
|
body: null
|
||||||
|
};
|
||||||
|
|
||||||
if(!window.Worker) {
|
if(!window.Worker) {
|
||||||
this.except("JavaScript Workers aren't supported by your browser");
|
this.except("JavaScript Workers aren't supported by your browser");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(manifest) {
|
||||||
|
this.load(manifest);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
debug(attachment = "ATTACHMENT_EMPTY") {
|
debug(attachment = "ATTACHMENT_EMPTY") {
|
||||||
|
@ -41,7 +48,7 @@ export default class Monekydo {
|
||||||
// Attempt to parse the argument as JSON
|
// Attempt to parse the argument as JSON
|
||||||
try {
|
try {
|
||||||
data = JSON.parse(manifest);
|
data = JSON.parse(manifest);
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
// If that fails, attempt to parse it as a URL
|
// If that fails, attempt to parse it as a URL
|
||||||
try {
|
try {
|
||||||
|
@ -65,20 +72,20 @@ export default class Monekydo {
|
||||||
|
|
||||||
if(!data.hasOwnProperty("header") || !data.hasOwnProperty("body")) {
|
if(!data.hasOwnProperty("header") || !data.hasOwnProperty("body")) {
|
||||||
this.debug(data);
|
this.debug(data);
|
||||||
throw new Error(errorPrefix + "Object is not a Monkeydo manifest");
|
throw new Error(errorPrefix + "Expected 'header' and 'body' properties in object");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.header = data.header;
|
this.manifest.header = data.header;
|
||||||
this.body = data.body;
|
this.manifest.body = data.body;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
do() {
|
do() {
|
||||||
const errorPrefix = "DO_FAILED: ";
|
const errorPrefix = "DO_FAILED: ";
|
||||||
if(!this.header) {
|
if(!this.manifest.header) {
|
||||||
this.debug(this.header);
|
this.debug(this.manifest.header);
|
||||||
throw new Error(errorPrefix + `Expected Monkeydo manifest, got '${this.header}' instead`);
|
throw new Error(errorPrefix + `Expected header object from contructed property`);
|
||||||
}
|
}
|
||||||
const monkey = new MonkeyWorker();
|
this.giveManifest();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,10 +0,0 @@
|
||||||
export default class MonkeyWorker extends Worker {
|
|
||||||
constructor() {
|
|
||||||
super();
|
|
||||||
onmessage = (message) => this.instruction(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
instruction(message) {
|
|
||||||
console.log(message);
|
|
||||||
}
|
|
||||||
}
|
|
30
worker/MonkeyWorker.mjs
Normal file
30
worker/MonkeyWorker.mjs
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
// Spawn a dedicated worker for scheduling events from manifest
|
||||||
|
|
||||||
|
export default class MonkeyWorker {
|
||||||
|
constructor() {
|
||||||
|
// Get location of this file
|
||||||
|
this.ready = false;
|
||||||
|
let location = new URL(import.meta.url);
|
||||||
|
location = location.pathname.replace("MonkeyWorker.mjs","");
|
||||||
|
|
||||||
|
// Spawn worker from file relative to this file
|
||||||
|
this.worker = new Worker(location + "Sequencer.js");
|
||||||
|
this.worker.addEventListener("message",message => this.message(message));
|
||||||
|
}
|
||||||
|
|
||||||
|
play() {
|
||||||
|
this.worker.postMessage(["PLAYSTATE",true]);
|
||||||
|
}
|
||||||
|
|
||||||
|
pause() {
|
||||||
|
this.worker.postMessage(["PLAYSTATE",false]);
|
||||||
|
}
|
||||||
|
|
||||||
|
giveManifest() {
|
||||||
|
this.worker.postMessage(["GIVE_MANIFEST",this.manifest]);
|
||||||
|
}
|
||||||
|
|
||||||
|
message(message) {
|
||||||
|
console.log(message);
|
||||||
|
}
|
||||||
|
}
|
5
worker/Sequencer.js
Normal file
5
worker/Sequencer.js
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
postMessage("MONKEDO_THREAD_SPAWNED");
|
||||||
|
|
||||||
|
onmessage = (message) => {
|
||||||
|
console.log("Message received",message);
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue