refactor(api): ♻️ Use Web Workers instead of spawning the same process once for each thread

This commit is contained in:
Jesse Wierzbinski 2024-06-26 14:44:08 -10:00
parent bc8220c8f9
commit d29603275a
No known key found for this signature in database
7 changed files with 213 additions and 164 deletions

View file

@ -33,21 +33,18 @@ export default class Start extends BaseCommand<typeof Start> {
public async run(): Promise<void> {
const { flags } = await this.parse(Start);
const numCpUs = flags["all-threads"] ? os.cpus().length : flags.threads;
const numCpus = flags["all-threads"] ? os.cpus().length : flags.threads;
// Check if index is a JS or TS file (depending on the environment)
const index = (await Bun.file("index.ts").exists())
? "index.ts"
: "index.js";
for (let i = 0; i < numCpUs; i++) {
const args = ["bun", index];
if (i !== 0 || flags.silent) {
args.push("--silent");
}
Bun.spawn(args, {
stdio: ["inherit", "inherit", "inherit"],
env: { ...process.env },
await import("../../setup");
for (let i = 0; i < numCpus; i++) {
new Worker(index, {
type: "module",
});
}
}