mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 06:50:01 -04:00
chore: remove threading
This commit is contained in:
parent
a3cb21a500
commit
70779ffe5b
8 changed files with 0 additions and 140 deletions
|
@ -10,12 +10,10 @@ const __dirname = fileURLToPath(new URL(".", import.meta.url));
|
|||
const packagemeta = JSON.parse(await readFile("package.json"));
|
||||
|
||||
export default defineConfig({
|
||||
// change to production when needed
|
||||
mode: "development",
|
||||
entry: {
|
||||
shared: join(__dirname, "src/shared/index.ts"),
|
||||
worker: join(__dirname, "src/worker/index.ts"),
|
||||
thread: join(__dirname, "src/thread/thread.ts"),
|
||||
client: join(__dirname, "src/client/index.ts"),
|
||||
codecs: join(__dirname, "src/codecs/index.ts"),
|
||||
controller: join(__dirname, "src/controller/index.ts"),
|
||||
|
|
|
@ -21,7 +21,6 @@ export class ScramjetController {
|
|||
wasm: "/scramjet.wasm.js",
|
||||
shared: "/scramjet.shared.js",
|
||||
worker: "/scramjet.worker.js",
|
||||
thread: "/scramjet.thread.js",
|
||||
client: "/scramjet.client.js",
|
||||
codecs: "/scramjet.codecs.js",
|
||||
sync: "/scramjet.sync.js",
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
import { rewriteJs } from "../shared/rewriters/js";
|
||||
|
||||
// @ts-ignore
|
||||
onconnect = (e) => {
|
||||
const port = e.ports[0];
|
||||
|
||||
console.log("thread: connected to port", port);
|
||||
port.postMessage("ready");
|
||||
|
||||
let syncToken = 0;
|
||||
port.onmessage = ({ data }) => {
|
||||
console.log("thread: received message", data);
|
||||
const [task, ...args] = data;
|
||||
const token = syncToken++;
|
||||
|
||||
try {
|
||||
const res = tasks[task](...args);
|
||||
console.log("thread: task", task, "completed with token", token);
|
||||
port.postMessage({
|
||||
token,
|
||||
result: res,
|
||||
});
|
||||
} catch (e) {
|
||||
port.postMessage({
|
||||
token,
|
||||
error: e.message,
|
||||
});
|
||||
}
|
||||
|
||||
port.postMessage("idle");
|
||||
};
|
||||
};
|
||||
|
||||
const tasks = {
|
||||
rewriteJs: taskRewriteJs,
|
||||
};
|
||||
|
||||
function taskRewriteJs(js: ArrayBuffer, origin: string): string {
|
||||
// idk how to get the codec from here
|
||||
return rewriteJs(js, new URL(origin), () => {});
|
||||
}
|
1
src/types.d.ts
vendored
1
src/types.d.ts
vendored
|
@ -47,7 +47,6 @@ interface ScramjetConfig {
|
|||
wasm: string;
|
||||
shared: string;
|
||||
worker: string;
|
||||
thread: string;
|
||||
client: string;
|
||||
codecs: string;
|
||||
sync: string;
|
||||
|
|
|
@ -287,8 +287,6 @@ async function rewriteBody(
|
|||
}
|
||||
case "script":
|
||||
return rewriteJs(await response.arrayBuffer(), meta);
|
||||
// Disable threading for now, it's causing issues.
|
||||
// responseBody = await this.threadpool.rewriteJs(await responseBody.arrayBuffer(), url.toString());
|
||||
case "style":
|
||||
return rewriteCss(await response.text(), meta);
|
||||
case "sharedworker":
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
import { FakeServiceWorker } from "./fakesw";
|
||||
import { swfetch } from "./fetch";
|
||||
import { ScramjetThreadpool } from "./threadpool";
|
||||
import type BareClient from "@mercuryworkshop/bare-mux";
|
||||
|
||||
export class ScramjetServiceWorker extends EventTarget {
|
||||
client: BareClient;
|
||||
config: typeof self.$scramjet.config;
|
||||
threadpool: ScramjetThreadpool;
|
||||
|
||||
syncPool: Record<number, (val?: any) => void> = {};
|
||||
synctoken = 0;
|
||||
|
@ -19,8 +17,6 @@ export class ScramjetServiceWorker extends EventTarget {
|
|||
super();
|
||||
this.client = new self.$scramjet.shared.util.BareClient();
|
||||
|
||||
this.threadpool = new ScramjetThreadpool();
|
||||
|
||||
addEventListener("message", ({ data }: { data: MessageC2W }) => {
|
||||
if (!("scramjet$type" in data)) return;
|
||||
|
||||
|
|
|
@ -1,88 +0,0 @@
|
|||
type Thread = {
|
||||
handle: MessagePort;
|
||||
ready: boolean;
|
||||
busy: boolean;
|
||||
syncToken: number;
|
||||
promises: Map<number, { resolve; reject }>;
|
||||
};
|
||||
|
||||
export class ScramjetThreadpool {
|
||||
threads: Thread[] = [];
|
||||
constructor() {
|
||||
self.addEventListener("message", ({ data }) => {
|
||||
if (data.scramjet$type == "add") {
|
||||
this.spawn(data.handle);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
spawn(handle) {
|
||||
const thread = {
|
||||
handle,
|
||||
ready: false,
|
||||
busy: false,
|
||||
syncToken: 0,
|
||||
promises: new Map(),
|
||||
};
|
||||
|
||||
this.threads.push(thread);
|
||||
|
||||
thread.handle.onmessage = (e) => {
|
||||
if (e.data === "ready") {
|
||||
thread.ready = true;
|
||||
|
||||
return;
|
||||
}
|
||||
if (e.data === "idle") {
|
||||
thread.busy = false;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const { token, result, error } = e.data;
|
||||
const { resolve, reject } = thread.promises.get(token);
|
||||
thread.promises.delete(token);
|
||||
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(result);
|
||||
}
|
||||
};
|
||||
|
||||
thread.handle.start();
|
||||
}
|
||||
|
||||
pick(): Thread | undefined {
|
||||
const alive = this.threads.filter((t) => t.ready);
|
||||
const idle = alive.filter((t) => !t.busy);
|
||||
|
||||
// no threads
|
||||
if (!alive.length) return;
|
||||
|
||||
// there is a thread, but it's busy
|
||||
if (!idle.length) return alive[Math.floor(Math.random() * alive.length)];
|
||||
|
||||
// there's an open thread
|
||||
return idle[Math.floor(Math.random() * idle.length)];
|
||||
}
|
||||
|
||||
run(task: string, args: any[], transferrable: any[]): Promise<any> {
|
||||
const thread = this.pick();
|
||||
if (!thread) throw new Error("No threads available");
|
||||
thread.busy = true;
|
||||
|
||||
const token = thread.syncToken++;
|
||||
|
||||
// console.log("runthread: dispatching task", task, "to thread", thread, "of token", token)
|
||||
return new Promise((resolve, reject) => {
|
||||
thread.promises.set(token, { resolve, reject });
|
||||
|
||||
thread.handle.postMessage([task, ...args], transferrable);
|
||||
});
|
||||
}
|
||||
|
||||
async rewriteJs(js: ArrayBuffer, origin: string): Promise<string> {
|
||||
return await this.run("rewriteJs", [js, origin], [js]);
|
||||
}
|
||||
}
|
|
@ -2,7 +2,6 @@ const scramjet = new ScramjetController({
|
|||
wasm: "/scram/scramjet.wasm.js",
|
||||
codecs: "/scram/scramjet.codecs.js",
|
||||
worker: "/scram/scramjet.worker.js",
|
||||
thread: "/scram/scramjet.thread.js",
|
||||
client: "/scram/scramjet.client.js",
|
||||
shared: "/scram/scramjet.shared.js",
|
||||
sync: "/scram/scramjet.sync.js",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue