only import worker_threads when we create a worker factory

This commit is contained in:
Florian Dold 2020-02-24 22:56:54 +05:30
parent 1cc8500cb0
commit 02c265f684
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B

View File

@ -19,8 +19,6 @@ import { CryptoWorkerFactory } from "./cryptoApi";
// tslint:disable:no-var-requires // tslint:disable:no-var-requires
import { CryptoWorker } from "./cryptoWorker"; import { CryptoWorker } from "./cryptoWorker";
import worker_threads = require("worker_threads");
import os = require("os"); import os = require("os");
import { CryptoImplementation } from "./cryptoImplementation"; import { CryptoImplementation } from "./cryptoImplementation";
@ -84,6 +82,7 @@ export function handleWorkerMessage(msg: any) {
try { try {
const result = (impl as any)[operation](...args); const result = (impl as any)[operation](...args);
const worker_threads = require("worker_threads");
const p = worker_threads.parentPort; const p = worker_threads.parentPort;
worker_threads.parentPort?.postMessage; worker_threads.parentPort?.postMessage;
if (p) { if (p) {
@ -133,9 +132,10 @@ class NodeThreadCryptoWorker implements CryptoWorker {
*/ */
onerror: undefined | ((m: any) => void); onerror: undefined | ((m: any) => void);
private nodeWorker: worker_threads.Worker; private nodeWorker: import("worker_threads").Worker;
constructor() { constructor() {
const worker_threads = require("worker_threads");
this.nodeWorker = new worker_threads.Worker(workerCode, { eval: true }); this.nodeWorker = new worker_threads.Worker(workerCode, { eval: true });
this.nodeWorker.on("error", (err: Error) => { this.nodeWorker.on("error", (err: Error) => {
console.error("error in node worker:", err); console.error("error in node worker:", err);