From c3d584dc8fb8d1ab132cc378f1b7be5b7ce12172 Mon Sep 17 00:00:00 2001 From: Victor Westerlund Date: Mon, 4 Oct 2021 17:12:02 +0200 Subject: [PATCH] dev21w40a --- .gitignore | 50 +++++++++++++++++++++++++++ Monkeydo.mjs | 84 ++++++++++++++++++++++++++++++++++++++++++++++ classes/Worker.mjs | 10 ++++++ 3 files changed, 144 insertions(+) create mode 100644 .gitignore create mode 100644 Monkeydo.mjs create mode 100644 classes/Worker.mjs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0a30567 --- /dev/null +++ b/.gitignore @@ -0,0 +1,50 @@ +# Bootstrapping # +################# +/node_modules +/public/hot +/public/storage +/storage/*.key +/vendor +.env +.env.backup +.phpunit.result.cache +Homestead.json +Homestead.yaml +npm-debug.log +yarn-error.log +public/robots.txt + + +# OS generated files # +###################### +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +Icon? +ehthumbs.db +Thumbs.db +.directory + +# Tool specific files # +####################### +# vim +*~ +*.swp +*.swo +# sublime text & textmate +*.sublime-* +*.stTheme.cache +*.tmlanguage.cache +*.tmPreferences.cache +# Eclipse +.settings/* +# JetBrains, aka PHPStorm, IntelliJ IDEA +.idea/* +# NetBeans +nbproject/* +# Visual Studio Code +.vscode +# Sass preprocessor +.sass-cache/ diff --git a/Monkeydo.mjs b/Monkeydo.mjs new file mode 100644 index 0000000..087974a --- /dev/null +++ b/Monkeydo.mjs @@ -0,0 +1,84 @@ +import { default as MonkeyWorker } from "./classes/Worker.mjs"; + +export default class Monekydo { + constructor(manifest = false) { + this.monkeydo = { + version: "0.1", + debugLevel: 0, + get debug() { + return this.debugLevel > 0 ? true : false; + }, + set debug(flag = 1) { + this.debugLevel = flag; + } + }; + Object.seal(this.monkeydo); + + this.header = null; + this.body = null; + + if(!window.Worker) { + this.except("JavaScript Workers aren't supported by your browser"); + } + } + + debug(attachment = "ATTACHMENT_EMPTY") { + if(this.monkeydo.debug) { + console.warn("-- Monkeydo debug -->",attachment); + return; + } + } + + async load(manifest) { + const errorPrefix = "MANIFEST_IMPORT_FAILED: "; + let data; + // Monkeydo can only load a JSON string or URL to a JSON file + if(typeof manifest !== "string") { + this.debug(manifest); + throw new TypeError(errorPrefix + "Expected JSON or URL"); + } + + // Attempt to parse the argument as JSON + try { + data = JSON.parse(manifest); + } + catch { + // If that fails, attempt to parse it as a URL + try { + manifest = new URL(manifest); + const fetchManifest = await fetch(manifest); + + // If the URL parsed but the fetch response is invalid, give up and throw an error + if(!fetchManifest.ok || fetchManifest.headers.get("Content-Type") !== "application/json") { + throw new TypeError(errorPrefix + "Invalid response Content-Type or HTTP status"); + } + data = await fetchManifest.json(); + } + catch(error) { + this.debug(manifest); + if(!error instanceof TypeError) { + throw new TypeError(errorPrefix + "Invalid JSON or URL"); + } + throw error; + } + } + + if(!data.hasOwnProperty("header") || !data.hasOwnProperty("body")) { + this.debug(data); + throw new Error(errorPrefix + "Object is not a Monkeydo manifest"); + } + + this.header = data.header; + this.body = data.body; + return true; + } + + do() { + const errorPrefix = "DO_FAILED: "; + if(!this.header) { + this.debug(this.header); + throw new Error(errorPrefix + `Expected Monkeydo manifest, got '${this.header}' instead`); + } + const monkey = new MonkeyWorker(); + } +} \ No newline at end of file diff --git a/classes/Worker.mjs b/classes/Worker.mjs new file mode 100644 index 0000000..5f76ae8 --- /dev/null +++ b/classes/Worker.mjs @@ -0,0 +1,10 @@ +export default class MonkeyWorker extends Worker { + constructor() { + super(); + onmessage = (message) => this.instruction(message); + } + + instruction(message) { + console.log(message); + } +} \ No newline at end of file