Skip to content

Commit

Permalink
fix(troika-worker-utils): Avoid no-workers log errors when forcing ma…
Browse files Browse the repository at this point in the history
…in 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.
  • Loading branch information
lojjic committed Nov 11, 2024
1 parent 9c214c7 commit d396e51
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/troika-worker-utils/src/WorkerModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions packages/troika-worker-utils/src/mainThreadFallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit d396e51

Please sign in to comment.