From aac37fd296433cc44904afbe06448138bc0be8db Mon Sep 17 00:00:00 2001 From: Alice Date: Mon, 14 Aug 2023 11:13:56 -0400 Subject: [PATCH] chore(compiler): remove some unused code (#4665) This deletes the code in `src/compiler/sys/worker/web-worker-*.ts`. Both of the files in that directory which appeared to implement some sort of web worker-based compilation task runner were unused. --- src/compiler/sys/worker/web-worker-main.ts | 169 ------------------- src/compiler/sys/worker/web-worker-thread.ts | 75 -------- 2 files changed, 244 deletions(-) delete mode 100644 src/compiler/sys/worker/web-worker-main.ts delete mode 100644 src/compiler/sys/worker/web-worker-thread.ts diff --git a/src/compiler/sys/worker/web-worker-main.ts b/src/compiler/sys/worker/web-worker-main.ts deleted file mode 100644 index d2d8e30d810..00000000000 --- a/src/compiler/sys/worker/web-worker-main.ts +++ /dev/null @@ -1,169 +0,0 @@ -import { TASK_CANCELED_MSG } from '@utils'; - -import type * as d from '../../../declarations'; - -export const createWebWorkerMainController = ( - sys: d.CompilerSystem, - maxConcurrentWorkers: number, -): d.WorkerMainController => { - let msgIds = 0; - let isDestroyed = false; - let isQueued = false; - let workerIds = 0; - let workerBlob: Blob; - const tasks = new Map(); - const queuedSendMsgs: d.MsgToWorker[] = []; - const workers: WorkerChild[] = []; - const maxWorkers = Math.max(Math.min(maxConcurrentWorkers, sys.hardwareConcurrency), 2) - 1; - const tick = Promise.resolve(); - - const onMsgsFromWorker = (worker: WorkerChild, ev: MessageEvent) => { - if (!isDestroyed) { - const msgsFromWorker: d.MsgFromWorker[] = ev.data; - if (Array.isArray(msgsFromWorker)) { - for (const msgFromWorker of msgsFromWorker) { - if (msgFromWorker) { - const task = tasks.get(msgFromWorker.stencilId); - if (task) { - tasks.delete(msgFromWorker.stencilId); - if (msgFromWorker.stencilRtnError) { - task.reject(msgFromWorker.stencilRtnError); - } else { - task.resolve(msgFromWorker.stencilRtnValue); - } - - worker.activeTasks--; - if (worker.activeTasks < 0 || worker.activeTasks > 50) { - worker.activeTasks = 0; - } - } else if (msgFromWorker.stencilRtnError) { - console.error(msgFromWorker.stencilRtnError); - } - } - } - } - } - }; - - const onWorkerError = (e: ErrorEvent) => console.error(e); - - const createWorkerMain = () => { - let worker: Worker = null; - const workerUrl = sys.getCompilerExecutingPath(); - const workerOpts: WorkerOptions = { - name: `stencil.worker.${workerIds++}`, - }; - - try { - // first try directly starting the worker with the URL - worker = new Worker(workerUrl, workerOpts); - } catch (e) { - // probably a cross-origin issue, try using a Blob instead - if (workerBlob == null) { - workerBlob = new Blob([`importScripts('${workerUrl}');`], { type: 'application/javascript' }); - } - worker = new Worker(URL.createObjectURL(workerBlob), workerOpts); - } - - const workerChild: WorkerChild = { - worker, - activeTasks: 0, - sendQueue: [], - }; - worker.onerror = onWorkerError; - worker.onmessage = (ev) => onMsgsFromWorker(workerChild, ev); - - return workerChild; - }; - - const sendMsgsToWorkers = (w: WorkerChild) => { - if (w.sendQueue.length > 0) { - w.worker.postMessage(w.sendQueue); - w.sendQueue.length = 0; - } - }; - - const queueMsgToWorker = (msg: d.MsgToWorker) => { - let theChosenOne: WorkerChild; - - if (workers.length > 0) { - theChosenOne = workers[0]; - - if (maxWorkers > 1) { - for (const worker of workers) { - if (worker.activeTasks < theChosenOne.activeTasks) { - theChosenOne = worker; - } - } - - if (theChosenOne.activeTasks > 0 && workers.length < maxWorkers) { - theChosenOne = createWorkerMain(); - workers.push(theChosenOne); - } - } - } else { - theChosenOne = createWorkerMain(); - workers.push(theChosenOne); - } - - theChosenOne.activeTasks++; - theChosenOne.sendQueue.push(msg); - }; - - const flushSendQueue = () => { - isQueued = false; - queuedSendMsgs.forEach(queueMsgToWorker); - queuedSendMsgs.length = 0; - workers.forEach(sendMsgsToWorkers); - }; - - const send = (...args: any[]) => - new Promise((resolve, reject) => { - if (isDestroyed) { - reject(TASK_CANCELED_MSG); - } else { - const msg: d.MsgToWorker = { - stencilId: msgIds++, - args, - }; - queuedSendMsgs.push(msg); - - tasks.set(msg.stencilId, { - resolve, - reject, - }); - - if (!isQueued) { - isQueued = true; - tick.then(flushSendQueue); - } - } - }); - - const destroy = () => { - isDestroyed = true; - tasks.forEach((t) => t.reject(TASK_CANCELED_MSG)); - tasks.clear(); - workers.forEach((w) => w.worker.terminate()); - workers.length = 0; - }; - - const handler = (name: string) => { - return function (...args: any[]) { - return send(name, ...args); - }; - }; - - return { - send, - destroy, - handler, - maxWorkers, - }; -}; - -interface WorkerChild { - worker: Worker; - activeTasks: number; - sendQueue: d.MsgToWorker[]; -} diff --git a/src/compiler/sys/worker/web-worker-thread.ts b/src/compiler/sys/worker/web-worker-thread.ts deleted file mode 100644 index 5287a1667f3..00000000000 --- a/src/compiler/sys/worker/web-worker-thread.ts +++ /dev/null @@ -1,75 +0,0 @@ -import { isNumber, isString } from '@utils'; - -import { MsgFromWorker, MsgToWorker, WorkerMsgHandler } from '../../../declarations'; - -export const initWebWorkerThread = (msgHandler: WorkerMsgHandler) => { - let isQueued = false; - - const tick = Promise.resolve(); - - const msgsFromWorkerQueue: MsgFromWorker[] = []; - - const drainMsgQueueFromWorkerToMain = () => { - isQueued = false; - (self as any).postMessage(msgsFromWorkerQueue); - msgsFromWorkerQueue.length = 0; - }; - - const queueMsgFromWorkerToMain = (msgFromWorkerToMain: MsgFromWorker) => { - msgsFromWorkerQueue.push(msgFromWorkerToMain); - if (!isQueued) { - isQueued = true; - tick.then(drainMsgQueueFromWorkerToMain); - } - }; - - const error = (stencilMsgId: number, err: any) => { - const errMsgFromWorkerToMain: MsgFromWorker = { - stencilId: stencilMsgId, - stencilRtnValue: null, - stencilRtnError: 'Error', - }; - if (isString(err)) { - errMsgFromWorkerToMain.stencilRtnError += ': ' + err; - } else if (err) { - if (err.stack) { - errMsgFromWorkerToMain.stencilRtnError += ': ' + err.stack; - } else if (err.message) { - errMsgFromWorkerToMain.stencilRtnError += ': ' + err.message; - } - } - queueMsgFromWorkerToMain(errMsgFromWorkerToMain); - }; - - const receiveMsgFromMainToWorker = async (msgToWorker: MsgToWorker) => { - if (msgToWorker && isNumber(msgToWorker.stencilId)) { - try { - // run the handler to get the data - const msgFromWorkerToMain: MsgFromWorker = { - stencilId: msgToWorker.stencilId, - stencilRtnValue: await msgHandler(msgToWorker), - stencilRtnError: null, - }; - queueMsgFromWorkerToMain(msgFromWorkerToMain); - } catch (e) { - // error occurred while running the task - error(msgToWorker.stencilId, e); - } - } - }; - - self.onmessage = (ev: MessageEvent) => { - // message from the main thread - const msgsFromMainToWorker: MsgToWorker[] = ev.data; - if (Array.isArray(msgsFromMainToWorker)) { - for (const msgFromMainToWorker of msgsFromMainToWorker) { - receiveMsgFromMainToWorker(msgFromMainToWorker); - } - } - }; - - self.onerror = (e) => { - // uncaught error occurred on the worker thread - error(-1, e); - }; -};