Skip to content

Commit

Permalink
feat(troika-worker-utils): Add a handle to force running worker modul…
Browse files Browse the repository at this point in the history
…es on main thread
  • Loading branch information
lojjic committed Nov 10, 2024
1 parent 57f3b0b commit 4f8bc13
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/troika-worker-utils/src/WorkerModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export function defineWorkerModule(options) {
}
let {dependencies, init, getTransferables, workerId} = options

const onMainThread = defineMainThreadModule(options)
if (!supportsWorkers()) {
return defineMainThreadModule(options)
return onMainThread
}

if (workerId == null) {
Expand Down Expand Up @@ -86,6 +87,9 @@ export function defineWorkerModule(options) {
init: stringifyFunction(init),
getTransferables: getTransferables && stringifyFunction(getTransferables)
}

moduleFunc.onMainThread = onMainThread;

return moduleFunc
}

Expand Down
12 changes: 9 additions & 3 deletions packages/troika-worker-utils/src/mainThreadFallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ export function defineMainThreadModule(options) {
let {dependencies, init} = options

// Resolve dependencies
dependencies = Array.isArray(dependencies) ? dependencies.map(dep =>
dep && dep._getInitResult ? dep._getInitResult() : dep
) : []
dependencies = Array.isArray(dependencies) ? dependencies.map(dep => {
if (dep) {
dep = dep.onMainThread || dep
if (dep._getInitResult) {
dep = dep._getInitResult()
}
}
return dep
}) : []

// Invoke init with the resolved dependencies
let initPromise = Promise.all(dependencies).then(deps => {
Expand Down

0 comments on commit 4f8bc13

Please sign in to comment.