UNPKG

winterspec

Version:

Write Winter-CG compatible routes with filesystem routing and tons of features

49 lines (48 loc) 1.85 kB
import { devServer } from "../../dev/dev.js"; import { loadConfig } from "../../config/index.js"; import { once } from "node:events"; export class Worker { constructor(initialData) { this.initialData = initialData; this.startBundlerPromise = this.startBundler(); } async handleTestWorker(testWorker) { let workerRpcCallback; const channel = { post: (data) => testWorker.publish(data), on: (data) => { workerRpcCallback = data; }, }; const messageHandlerAbortController = new AbortController(); const messageHandlerPromise = Promise.race([ once(messageHandlerAbortController.signal, "abort"), (async () => { let didAddChannel = false; for await (const msg of testWorker.subscribe()) { if (!didAddChannel) { const bundler = await this.startBundlerPromise; bundler.birpc.updateChannels((channels) => channels.push(channel)); didAddChannel = true; } workerRpcCallback(msg.data); if (messageHandlerAbortController.signal.aborted) { break; } } })(), ]); testWorker.teardown(async () => { const bundler = await this.startBundlerPromise; bundler.birpc.updateChannels((channels) => channels.filter((c) => c !== channel)); messageHandlerAbortController.abort(); await messageHandlerPromise; }); } async startBundler() { const config = await loadConfig(this.initialData.rootDirectory); return devServer.headless.startBundler({ config, }); } }