mirror of
https://codeberg.org/vlw/monkeydo.git
synced 2025-09-13 23:53:41 +02:00
dev21w45c
Ready for review
This commit is contained in:
parent
675fe748e4
commit
539690cbdd
3 changed files with 28 additions and 23 deletions
10
Monkeydo.mjs
10
Monkeydo.mjs
|
@ -13,12 +13,8 @@ export default class Monkeydo extends MonkeyMaster {
|
||||||
// Execute a task
|
// Execute a task
|
||||||
do(task) {
|
do(task) {
|
||||||
if(!task[1] in this.methods) return;
|
if(!task[1] in this.methods) return;
|
||||||
const args = task.splice(0,2);
|
const args = task.splice(2);
|
||||||
this.methods[task[1]](...args);
|
this.methods[task[1]]?.(...args);
|
||||||
}
|
|
||||||
|
|
||||||
async debug(state = true) {
|
|
||||||
return await this.setFlag("debug",state);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loop playback X times or negative number for infinite
|
// Loop playback X times or negative number for infinite
|
||||||
|
@ -28,7 +24,7 @@ export default class Monkeydo extends MonkeyMaster {
|
||||||
}
|
}
|
||||||
times = Math.floor(times);
|
times = Math.floor(times);
|
||||||
times = Math.min(Math.max(times,0),255); // Clamp number to 8 bits
|
times = Math.min(Math.max(times,0),255); // Clamp number to 8 bits
|
||||||
return await this.setFlag("playing",times);
|
return await this.setFlag("loop",times);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load Monkeydo manifest
|
// Load Monkeydo manifest
|
||||||
|
|
|
@ -4,7 +4,7 @@ importScripts("https://unpkg.com/comlink/dist/umd/comlink.js");
|
||||||
|
|
||||||
class Monkey {
|
class Monkey {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.flags = new Uint8ClampedArray(2);
|
this.flags = new Uint8ClampedArray(3);
|
||||||
this.tasks = [];
|
this.tasks = [];
|
||||||
this.tasksLength = 0;
|
this.tasksLength = 0;
|
||||||
this.i = 0;
|
this.i = 0;
|
||||||
|
@ -17,9 +17,8 @@ class Monkey {
|
||||||
|
|
||||||
// Task scheduler
|
// Task scheduler
|
||||||
next() {
|
next() {
|
||||||
if(this.flags[0] === 0 || this.flags[1] === 0) return;
|
if(this.flags[0] === 0 || this.flags[2] === 0) return this.abort();
|
||||||
const task = this.tasks[this.i];
|
const task = this.tasks[this.i];
|
||||||
console.log(task,this.i);
|
|
||||||
|
|
||||||
// Run task after delay
|
// Run task after delay
|
||||||
this.queue.thisTask = setTimeout(() => {
|
this.queue.thisTask = setTimeout(() => {
|
||||||
|
@ -30,9 +29,8 @@ class Monkey {
|
||||||
|
|
||||||
// Loop until flag is 0 or infinite if 255
|
// Loop until flag is 0 or infinite if 255
|
||||||
if(this.i === this.tasksLength) {
|
if(this.i === this.tasksLength) {
|
||||||
this.i = 0;
|
this.i = -1;
|
||||||
if(this.flags[1] === 255) return;
|
if(this.flags[1] < 255) this.flags[2]--;
|
||||||
this.flags[1]--;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Queue the next task
|
// Queue the next task
|
||||||
|
@ -40,11 +38,11 @@ class Monkey {
|
||||||
}
|
}
|
||||||
|
|
||||||
abort() {
|
abort() {
|
||||||
|
this.flags[2] = 0; // Playing: false
|
||||||
clearTimeout(this.queue.thisTask);
|
clearTimeout(this.queue.thisTask);
|
||||||
clearTimeout(this.queue.nextTask);
|
clearTimeout(this.queue.nextTask);
|
||||||
this.queue.thisTask = null;
|
this.queue.thisTask = null;
|
||||||
this.queue.nextTask = null;
|
this.queue.nextTask = null;
|
||||||
this.flags[1] = 0; // Playing: false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set or get a runtime flag
|
// Set or get a runtime flag
|
||||||
|
|
|
@ -41,7 +41,7 @@ export default class MonkeyMaster {
|
||||||
const worker = new Worker(this.getWorkerPath());
|
const worker = new Worker(this.getWorkerPath());
|
||||||
worker.addEventListener("message",event => {
|
worker.addEventListener("message",event => {
|
||||||
if(event.data[0] !== "TASK") return;
|
if(event.data[0] !== "TASK") return;
|
||||||
this.do(event.data);
|
this.do(event.data[1]); // Send inner array (task)
|
||||||
});
|
});
|
||||||
|
|
||||||
const Monkey = Comlink.wrap(worker);
|
const Monkey = Comlink.wrap(worker);
|
||||||
|
@ -61,31 +61,32 @@ export default class MonkeyMaster {
|
||||||
flagStringToIndex(flag) {
|
flagStringToIndex(flag) {
|
||||||
const flags = [
|
const flags = [
|
||||||
"MANIFEST_LOADED",
|
"MANIFEST_LOADED",
|
||||||
|
"LOOP",
|
||||||
"PLAYING"
|
"PLAYING"
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
// Translate string to index
|
// Translate string to index
|
||||||
if(typeof flag === "string" || flag < 0) {
|
if(typeof flag === "string" || flag < 0) {
|
||||||
flag = flags.indexOf(flag.toUpperCase());
|
flag = flags.indexOf(flag.toUpperCase());
|
||||||
if(flag < 0) return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that key is in bounds
|
// Check that key is in bounds
|
||||||
if(flag < 0 || flags > flags.length - 1) {
|
if(flag < 0 || flags > flags.length - 1) return false;
|
||||||
throw new Error(`Array key '${flag}' out of range`);
|
|
||||||
}
|
|
||||||
return flag;
|
return flag;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getFlag(flag) {
|
async getFlag(flag) {
|
||||||
const key = this.flagStringToIndex(flag);
|
const key = this.flagStringToIndex(flag);
|
||||||
|
if(!key) Promise.reject("Invalid flag");
|
||||||
return await this.comlink.flag(key);
|
return await this.comlink.flag(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set or queue worker runtime flag
|
// Set or queue worker runtime flag
|
||||||
async setFlag(flag,value) {
|
async setFlag(flag,value) {
|
||||||
const key = this.flagStringToIndex(flag);
|
const key = this.flagStringToIndex(flag);
|
||||||
|
if(!key) Promise.reject("Invalid flag");
|
||||||
|
|
||||||
|
// Set the flag when the worker is ready
|
||||||
if(!this.ready) {
|
if(!this.ready) {
|
||||||
this.queue.flag = [key,value];
|
this.queue.flag = [key,value];
|
||||||
return;
|
return;
|
||||||
|
@ -104,10 +105,13 @@ export default class MonkeyMaster {
|
||||||
if(!this.ready) await this.init();
|
if(!this.ready) await this.init();
|
||||||
return await new Promise((resolve,reject) => {
|
return await new Promise((resolve,reject) => {
|
||||||
let load = null;
|
let load = null;
|
||||||
|
// Attempt load string as URL and fetch manifest
|
||||||
try {
|
try {
|
||||||
const url = new URL(manifest);
|
const url = new URL(manifest);
|
||||||
|
// If the URL parsed but fetch failed, this promise will reject
|
||||||
load = this.comlink.fetchManifest(url.toString());
|
load = this.comlink.fetchManifest(url.toString());
|
||||||
}
|
}
|
||||||
|
// Or attempt to load string as JSON if it's not a URL
|
||||||
catch {
|
catch {
|
||||||
load = this.comlink.loadManifest(manifest);
|
load = this.comlink.loadManifest(manifest);
|
||||||
}
|
}
|
||||||
|
@ -116,11 +120,18 @@ export default class MonkeyMaster {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async stop() {
|
||||||
|
return await this.comlink.abort();
|
||||||
|
}
|
||||||
|
|
||||||
// Start playback of a loaded manifest
|
// Start playback of a loaded manifest
|
||||||
async start() {
|
async start() {
|
||||||
// Set playstate if no value is present already
|
const playing = await this.getFlag("playing");
|
||||||
const loop = await this.getFlag("playing");
|
let loop = await this.getFlag("loop");
|
||||||
if(loop < 1) await this.setFlag("playing",1);
|
loop = loop > 0 ? loop : 1; // Play once if loop has no value
|
||||||
|
|
||||||
|
if(playing > 0) return;
|
||||||
|
await this.setFlag("playing",loop);
|
||||||
return await this.comlink.next();
|
return await this.comlink.next();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue