Skip to content

Commit 9a7527a

Browse files
committed
feat: select between tiny and node
1 parent d0d1615 commit 9a7527a

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

src/createWorker.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ export function createWorker(workerPath: string & Blob, options: WorkerOptions)
1919
getWebWorker().default
2020
} else if (options.backend === "node") {
2121
WorkerConstructor = options.blob ?
22-
getNodeWorker().blob :
23-
getNodeWorker().default
22+
getNodeWorker("node").blob :
23+
getNodeWorker("node").default
2424
} else if (options.backend === "tiny") {
25-
// TODO
26-
throw new Error("Tiny worker is not supported using `createWorker` yet.")
25+
WorkerConstructor = options.blob ?
26+
getNodeWorker("tiny").blob :
27+
getNodeWorker("tiny").default
2728
} else {
2829
throw new Error("The worker backend is not supported.")
2930
}

src/master/implementation.node.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,21 +248,35 @@ function initTinyWorker(): ImplementationExport {
248248
let implementation: ImplementationExport
249249
let isTinyWorker: boolean
250250

251-
function selectWorkerImplementation(): ImplementationExport {
252-
try {
251+
function selectWorkerImplementation(selection?: string): ImplementationExport {
252+
if (!selection) {
253+
254+
// automatic version based selection
255+
try {
256+
isTinyWorker = false
257+
return initWorkerThreadsWorker()
258+
} catch(error) {
259+
// tslint:disable-next-line no-console
260+
console.debug("Node worker_threads not available. Trying to fall back to tiny-worker polyfill...")
261+
isTinyWorker = true
262+
return initTinyWorker()
263+
}
264+
265+
// manual selection
266+
} else if (selection === "node") {
253267
isTinyWorker = false
254268
return initWorkerThreadsWorker()
255-
} catch(error) {
256-
// tslint:disable-next-line no-console
257-
console.debug("Node worker_threads not available. Trying to fall back to tiny-worker polyfill...")
269+
} else if (selection === "tiny") {
258270
isTinyWorker = true
259271
return initTinyWorker()
272+
} else {
273+
throw new Error("selection is not supported" + selection)
260274
}
261275
}
262276

263-
export function getWorkerImplementation(): ImplementationExport {
277+
export function getWorkerImplementation(selection?: string): ImplementationExport {
264278
if (!implementation) {
265-
implementation = selectWorkerImplementation()
279+
implementation = selectWorkerImplementation(selection)
266280
}
267281
return implementation
268282
}

0 commit comments

Comments
 (0)