UNPKG

1.19 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = void 0;
7
8var _BaseWorkerPool = _interopRequireDefault(require('./base/BaseWorkerPool'));
9
10function _interopRequireDefault(obj) {
11 return obj && obj.__esModule ? obj : {default: obj};
12}
13
14/**
15 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
16 *
17 * This source code is licensed under the MIT license found in the
18 * LICENSE file in the root directory of this source tree.
19 */
20const canUseWorkerThreads = () => {
21 try {
22 require('worker_threads');
23
24 return true;
25 } catch {
26 return false;
27 }
28};
29
30class WorkerPool extends _BaseWorkerPool.default {
31 send(workerId, request, onStart, onEnd, onCustomMessage) {
32 this.getWorkerById(workerId).send(request, onStart, onEnd, onCustomMessage);
33 }
34
35 createWorker(workerOptions) {
36 let Worker;
37
38 if (this._options.enableWorkerThreads && canUseWorkerThreads()) {
39 Worker = require('./workers/NodeThreadsWorker').default;
40 } else {
41 Worker = require('./workers/ChildProcessWorker').default;
42 }
43
44 return new Worker(workerOptions);
45 }
46}
47
48var _default = WorkerPool;
49exports.default = _default;