Worker working everywhere it seems

pull/3/head
Zef Hemel 2022-02-25 10:06:56 +01:00
parent 5e5968f09e
commit 29cbb74508
4 changed files with 32 additions and 22 deletions

View File

@ -1,12 +1,14 @@
{ {
"name": "noot", "name": "noot",
"version": "1.0.0", "version": "1.0.0",
"source": "src/index.html", "source": [
"src/index.html"
],
"license": "MIT", "license": "MIT",
"browserslist": "> 0.5%, last 2 versions, not dead", "browserslist": "> 0.5%, last 2 versions, not dead",
"scripts": { "scripts": {
"start": "parcel", "start": "cp src/function_worker.js dist/ && parcel",
"build": "parcel build", "build": "parcel build && cp src/function_worker.js dist/",
"check-watch": "tsc --noEmit --watch" "check-watch": "tsc --noEmit --watch"
}, },
"devDependencies": { "devDependencies": {
@ -14,7 +16,11 @@
"@types/react": "^17.0.39", "@types/react": "^17.0.39",
"@types/react-dom": "^17.0.11", "@types/react-dom": "^17.0.11",
"parcel": "^2.3.2", "parcel": "^2.3.2",
"typescript": ">=3.0.0" "typescript": ">=3.0.0",
"uglify-js": "^3.15.1"
},
"resolutions": {
"@lezer/common": "git://github.com/zefhemel/common.git#046c880d1fcab713cadad327a5b7d8bb5de6522c"
}, },
"dependencies": { "dependencies": {
"@codemirror/basic-setup": "^0.19.1", "@codemirror/basic-setup": "^0.19.1",

View File

@ -1,16 +1,16 @@
function safeRun(fn: () => Promise<void>) { function safeRun(fn) {
fn().catch((e) => { fn().catch((e) => {
console.error(e); console.error(e);
}); });
} }
let func = null; let func = null;
let pendingRequests: { let pendingRequests = {};
[key: number]: any;
} = {}; console.log("hello world!");
self.addEventListener("syscall", (event) => { self.addEventListener("syscall", (event) => {
let customEvent = event as CustomEvent; let customEvent = event;
let detail = customEvent.detail; let detail = customEvent.detail;
pendingRequests[detail.id] = detail.callback; pendingRequests[detail.id] = detail.callback;
self.postMessage({ self.postMessage({
@ -22,7 +22,7 @@ self.addEventListener("syscall", (event) => {
}); });
self.addEventListener("result", (event) => { self.addEventListener("result", (event) => {
let customEvent = event as CustomEvent; let customEvent = event;
self.postMessage({ self.postMessage({
type: "result", type: "result",
result: customEvent.detail, result: customEvent.detail,
@ -30,8 +30,8 @@ self.addEventListener("result", (event) => {
}); });
self.addEventListener("app-error", (event) => { self.addEventListener("app-error", (event) => {
let customEvent = event as CustomEvent; let customEvent = event;
postMessage({ self.postMessage({
type: "error", type: "error",
reason: customEvent.detail, reason: customEvent.detail,
}); });
@ -39,17 +39,12 @@ self.addEventListener("app-error", (event) => {
self.addEventListener("message", (event) => { self.addEventListener("message", (event) => {
safeRun(async () => { safeRun(async () => {
let messageEvent = event as MessageEvent; let messageEvent = event;
let data = messageEvent.data; let data = messageEvent.data;
switch (data.type) { switch (data.type) {
case "boot": case "boot":
console.log("Booting", `./${data.prefix}/function/${data.name}`); console.log("Booting", `./${data.prefix}/function/${data.name}`);
importScripts(`./${data.prefix}/function/${data.name}`); importScripts(`./${data.prefix}/function/${data.name}`);
// if (data.userAgent && data.userAgent.indexOf("Firefox") !== -1) {
// // @ts-ignore
// } else {
// await import(`./${data.prefix}/function/${data.name}`);
// }
self.postMessage({ self.postMessage({
type: "inited", type: "inited",
}); });

View File

@ -22,7 +22,12 @@ export class FunctionWorker {
private cartridge: Cartridge; private cartridge: Cartridge;
constructor(cartridge: Cartridge, pathPrefix: string, name: string) { constructor(cartridge: Cartridge, pathPrefix: string, name: string) {
this.worker = new Worker(new URL("function_worker.ts", import.meta.url)); // this.worker = new Worker(new URL("function_worker.ts", import.meta.url), {
// type: "classic",
// });
let worker = window.Worker;
this.worker = new worker("function_worker.js");
// console.log("Starting worker", this.worker); // console.log("Starting worker", this.worker);
this.worker.onmessage = this.onmessage.bind(this); this.worker.onmessage = this.onmessage.bind(this);
this.worker.postMessage({ this.worker.postMessage({

View File

@ -280,10 +280,9 @@
style-mod "^4.0.0" style-mod "^4.0.0"
w3c-keyname "^2.2.4" w3c-keyname "^2.2.4"
"@lezer/common@^0.15.0", "@lezer/common@^0.15.5": "@lezer/common@^0.15.0", "@lezer/common@^0.15.5", "@lezer/common@git://github.com/zefhemel/common.git#046c880d1fcab713cadad327a5b7d8bb5de6522c":
version "0.15.11" version "0.15.11"
resolved "https://registry.yarnpkg.com/@lezer/common/-/common-0.15.11.tgz#965b5067036305f12e8a3efc344076850be1d3a8" resolved "git://github.com/zefhemel/common.git#046c880d1fcab713cadad327a5b7d8bb5de6522c"
integrity sha512-vv0nSdIaVCRcJ8rPuDdsrNVfBOYe/4Szr/LhF929XyDmBndLDuWiCCHooGlGlJfzELyO608AyDhVsuX/ZG36NA==
"@lezer/css@^0.15.2": "@lezer/css@^0.15.2":
version "0.15.2" version "0.15.2"
@ -1889,6 +1888,11 @@ typescript@>=3.0.0:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.5.5.tgz#d8c953832d28924a9e3d37c73d729c846c5896f3" resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.5.5.tgz#d8c953832d28924a9e3d37c73d729c846c5896f3"
integrity sha512-TCTIul70LyWe6IJWT8QSYeA54WQe8EjQFU4wY52Fasj5UKx88LNYKCgBEHcOMOrFF1rKGbD8v/xcNWVUq9SymA== integrity sha512-TCTIul70LyWe6IJWT8QSYeA54WQe8EjQFU4wY52Fasj5UKx88LNYKCgBEHcOMOrFF1rKGbD8v/xcNWVUq9SymA==
uglify-js@^3.15.1:
version "3.15.1"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.15.1.tgz#9403dc6fa5695a6172a91bc983ea39f0f7c9086d"
integrity sha512-FAGKF12fWdkpvNJZENacOH0e/83eG6JyVQyanIJaBXCN1J11TUQv1T1/z8S+Z0CG0ZPk1nPcreF/c7lrTd0TEQ==
util-deprecate@^1.0.2: util-deprecate@^1.0.2:
version "1.0.2" version "1.0.2"
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"