mirror of
https://codeberg.org/vlw/monkeydo.git
synced 2025-09-13 15:53:40 +02:00
dev21w44b
This commit is contained in:
parent
dd071ea8bb
commit
ff0ed25a3b
3 changed files with 71 additions and 60 deletions
10
Monkeydo.mjs
10
Monkeydo.mjs
|
@ -20,14 +20,6 @@ export default class Monkeydo extends MonkeyMaster {
|
||||||
}
|
}
|
||||||
|
|
||||||
async load(manifest) {
|
async load(manifest) {
|
||||||
let data = "";
|
|
||||||
if(typeof manifest === "object") {
|
|
||||||
data = JSON.stringify(manifest);
|
|
||||||
}
|
|
||||||
const load = await this.transaction("LOAD_MANIFEST",manifest);
|
|
||||||
if(!load) {
|
|
||||||
throw new Error("Failed to load manifest");
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,14 +1,25 @@
|
||||||
// Dedicated worker (monkey) which executes tasks from a Monkeydo manifest
|
// Dedicated worker (monkey) that executes tasks from a Monkeydo manifest
|
||||||
|
|
||||||
|
importScripts("https://unpkg.com/comlink/dist/umd/comlink.js");
|
||||||
|
|
||||||
class Monkey {
|
class Monkey {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.manifest = {};
|
this.manifest = {};
|
||||||
|
|
||||||
|
// Runtime flags
|
||||||
|
this.flags = new Uint8ClampedArray(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set or get a runtime flag
|
||||||
|
flag(index,value = null) {
|
||||||
|
return value ? this.flags[index] = value : this.flags[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
async loadManifest(manifest) {
|
async loadManifest(manifest) {
|
||||||
try {
|
try {
|
||||||
const data = JSON.parse(manifest);
|
const data = JSON.parse(manifest);
|
||||||
this.manifest = data;
|
this.manifest = data;
|
||||||
|
this.flags[0] = 1;
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
const url = new URL(manifest);
|
const url = new URL(manifest);
|
||||||
|
@ -16,9 +27,4 @@ class Monkey {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const monkey = new Monkey();
|
Comlink.expose(Monkey);
|
||||||
|
|
||||||
// Event handler for messages received from initiator
|
|
||||||
onmessage = (message) => {
|
|
||||||
console.log(message);
|
|
||||||
}
|
|
|
@ -1,55 +1,68 @@
|
||||||
// Task manager for Monkeydo dedicated workers (monkeys)
|
// Task manager for Monkeydo dedicated workers (monkeys)
|
||||||
|
|
||||||
class WorkerTransactions {
|
import * as Comlink from "https://unpkg.com/comlink/dist/esm/comlink.mjs";
|
||||||
|
|
||||||
|
export default class MonkeyMaster {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.txn = {
|
this.comlink = null;
|
||||||
_open: [], // Open transations
|
|
||||||
prefix: "TXN",
|
this.ready = false;
|
||||||
timeout: 2000,
|
this.flagQueue = [];
|
||||||
// Close a transaction
|
this.init();
|
||||||
set close(name) {
|
}
|
||||||
this._open[name].resolve();
|
|
||||||
},
|
// Import worker relative to this module
|
||||||
// Open a new transaction
|
getWorkerPath() {
|
||||||
set open(name) {
|
const name = "Monkey.js";
|
||||||
name = [this.prefix,name];
|
const url = new URL(import.meta.url);
|
||||||
name = name.join("_").toUpperCase();
|
|
||||||
this._open[name] = new Promise();
|
const path = url.pathname.split("/");
|
||||||
},
|
path[path.length - 1] = name;
|
||||||
get status(name) {
|
|
||||||
return this._open[name];
|
url.pathname = path.join("/");
|
||||||
|
return url.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
async init() {
|
||||||
|
// Spawn and wrap dedicated worker with Comlink
|
||||||
|
const worker = new Worker(this.getWorkerPath());
|
||||||
|
const Monkey = Comlink.wrap(worker);
|
||||||
|
|
||||||
|
this.comlink = await new Monkey();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return a flag array index by name
|
||||||
|
flagStringToIndex(flag) {
|
||||||
|
const flags = [
|
||||||
|
"MANIFEST_LOADED",
|
||||||
|
"PLAYING",
|
||||||
|
"LOOP"
|
||||||
|
];
|
||||||
|
// Translate string to index
|
||||||
|
if(typeof flag === "string" || flag < 0) {
|
||||||
|
const key = flags.indexOf(flag.toUpperCase());
|
||||||
|
if(key < 0) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
// Check key is in bounds
|
||||||
}
|
if(flag < 0 || flags > flags.length - 1) {
|
||||||
|
throw new Error(`Array key '${flag}' out of range`);
|
||||||
export default class MonkeyMaster extends WorkerTransactions {
|
|
||||||
constructor() {
|
|
||||||
super();
|
|
||||||
// Spawn dedicated worker
|
|
||||||
this.monkey = new Worker("Monkey.js");
|
|
||||||
this.monkey.addEventListener("message",message => this.receive(message.data));
|
|
||||||
|
|
||||||
if(this?.crossOriginIsolateds === true) {
|
|
||||||
// TODO; SharedArrayBuffer goes here
|
|
||||||
}
|
}
|
||||||
|
return flag;
|
||||||
}
|
}
|
||||||
|
|
||||||
send(data) {
|
async getFlag(flag) {
|
||||||
this.monkey.postMessage(data);
|
const key = this.flagStringToIndex(flag);
|
||||||
|
return await this.comlink.flag(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
async setFlag(flag,value) {
|
||||||
|
const key = this.flagStringToIndex(flag);
|
||||||
|
const update = await this.comlink.flag(0,12);
|
||||||
|
if(!update) {
|
||||||
|
this.flagQueue.push([key,value]);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
async receive(data) {
|
|
||||||
if(data[0] === "TASK") {
|
|
||||||
this.do(data[1]);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.txn.close = data[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
async transaction(name,data) {
|
|
||||||
this.txn.open = name;
|
|
||||||
const send = this.send([this.txn.prefix,name,data]);
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue