From d396e51708d151d9cee04394d3cd8ac87c1a7dc0 Mon Sep 17 00:00:00 2001 From: Jason Johnston Date: Sun, 10 Nov 2024 18:54:55 -0700 Subject: [PATCH] fix(troika-worker-utils): Avoid no-workers log errors when forcing main thread Fixes #337 - this moves the supportsWorkers check from module definition time to call time, so it's skipped entirely when the user calls onMainThread directly. --- packages/troika-worker-utils/src/WorkerModules.js | 7 ++++--- packages/troika-worker-utils/src/mainThreadFallback.js | 2 ++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/troika-worker-utils/src/WorkerModules.js b/packages/troika-worker-utils/src/WorkerModules.js index 5069bdde..4f1566ad 100644 --- a/packages/troika-worker-utils/src/WorkerModules.js +++ b/packages/troika-worker-utils/src/WorkerModules.js @@ -30,9 +30,6 @@ export function defineWorkerModule(options) { let {dependencies, init, getTransferables, workerId} = options const onMainThread = defineMainThreadModule(options) - if (!supportsWorkers()) { - return onMainThread - } if (workerId == null) { workerId = '#default' @@ -60,6 +57,10 @@ export function defineWorkerModule(options) { }) function moduleFunc(...args) { + if (!supportsWorkers()) { + return onMainThread(...args) + } + // Register this module if needed if (!registrationPromise) { registrationPromise = callWorker(workerId,'registerModule', moduleFunc.workerModuleData) diff --git a/packages/troika-worker-utils/src/mainThreadFallback.js b/packages/troika-worker-utils/src/mainThreadFallback.js index 56715b38..c879d27a 100644 --- a/packages/troika-worker-utils/src/mainThreadFallback.js +++ b/packages/troika-worker-utils/src/mainThreadFallback.js @@ -20,7 +20,9 @@ export function defineMainThreadModule(options) { // Resolve dependencies dependencies = Array.isArray(dependencies) ? dependencies.map(dep => { if (dep) { + // If it's a worker module, use its main thread impl dep = dep.onMainThread || dep + // If it's a main thread worker module, use its init return value if (dep._getInitResult) { dep = dep._getInitResult() }