'use strict'
const {
- Worker, isMainThread, MessageChannel, SHARE_ENV
+ Worker,
+ isMainThread,
+ MessageChannel,
+ SHARE_ENV
} = require('worker_threads')
function empty () {}
*/
class FixedThreadPool {
/**
- *
- * @param {Number} numThreads Num of threads for this worker pool
- * @param {string} a file path with implementation of @see ThreadWorker class, relative path is fine
- * @param {Object} an object with possible options for example errorHandler, onlineHandler.
- */
+ *
+ * @param {Number} numThreads Num of threads for this worker pool
+ * @param {string} a file path with implementation of @see ThreadWorker class, relative path is fine
+ * @param {Object} an object with possible options for example errorHandler, onlineHandler.
+ */
constructor (numThreads, filePath, opts) {
- if (!isMainThread) throw new Error('Cannot start a thread pool from a worker thread !!!')
- if (!filePath) throw new Error('Please specify a file with a worker implementation')
+ if (!isMainThread) {
+ throw new Error('Cannot start a thread pool from a worker thread !!!')
+ }
+ if (!filePath) {
+ throw new Error('Please specify a file with a worker implementation')
+ }
this.numThreads = numThreads
this.workers = []
this.nextWorker = 0
_execute (worker, id) {
return new Promise((resolve, reject) => {
- const listener = (message) => {
+ const listener = message => {
if (message._id === id) {
worker.port2.removeListener('message', listener)
this.tasks.set(worker, this.tasks.get(worker) - 1)
}
_chooseWorker () {
- if ((this.workers.length - 1) === this.nextWorker) {
+ if (this.workers.length - 1 === this.nextWorker) {
this.nextWorker = 0
return this.workers[this.nextWorker]
} else {