"build:clean": "rimraf ./dist",
"lint": "cross-env TIMING=1 eslint --ext .js,.ts src",
"lint:fix": "cross-env TIMING=1 eslint --fix --ext .js,.ts src",
+ "import-sort": "npx import-sort-cli --write 'src/**/*.ts{,x}'",
"tsc": "tsc",
"test": "echo \"Error: no tests implemented\" && exit 1",
"clinic:clean": "clinic clean",
--- /dev/null
+import { Worker } from 'worker_threads';
+
+// FIXME: make it more generic
+export interface WorkerData {
+ index: number;
+ templateFile: string;
+}
+
+export interface WorkerSetElement {
+ worker: Worker,
+ numberOfWorkerElements: number
+}
+
+export enum WorkerEvents {
+ START_WORKER_ELEMENT = 'startWorkerElement',
+}
+
+import { WorkerData, WorkerEvents, WorkerSetElement } from '../types/Worker';
+
import Constants from '../utils/Constants';
import Utils from '../utils/Utils';
import { Worker } from 'worker_threads';
-import WorkerData from '../types/WorkerData';
-import { WorkerEvents } from '../types/WorkerEvents';
import Wrk from './Wrk';
export default class WorkerSet extends Wrk {
public maxElementsPerWorker: number;
- private workers: Set<Worker>;
- private lastWorkerNumberOfElements: number;
+ private workers: Set<WorkerSetElement>;
/**
* Create a new `WorkerSet`.
*/
constructor(workerScript: string, maxElementsPerWorker = 1) {
super(workerScript);
- this.workers = new Set<Worker>();
+ this.workers = new Set<WorkerSetElement>();
this.maxElementsPerWorker = maxElementsPerWorker;
- this.lastWorkerNumberOfElements = 0;
}
get size(): number {
if (!this.workers) {
throw Error('Cannot add a WorkerSet element: workers set does not exist');
}
- if (this.lastWorkerNumberOfElements >= this.maxElementsPerWorker) {
+ if (this.getLastWorkerSetElement().numberOfWorkerElements >= this.maxElementsPerWorker) {
void this.startWorker();
- this.lastWorkerNumberOfElements = 0;
// Start worker sequentially to optimize memory at startup
void Utils.sleep(Constants.START_WORKER_DELAY);
}
this.getLastWorker().postMessage({ id: WorkerEvents.START_WORKER_ELEMENT, workerData: elementData });
- this.lastWorkerNumberOfElements++;
+ this.getLastWorkerSetElement().numberOfWorkerElements++;
}
/**
reject(new Error(`Worker stopped with exit code ${code}`));
}
});
- this.workers.add(worker);
+ this.workers.add({ worker, numberOfWorkerElements: 0 });
});
}
- private getLastWorker(): Worker {
- let worker: Worker;
+ private getLastWorkerSetElement(): WorkerSetElement {
+ let workerSetElement: WorkerSetElement;
// eslint-disable-next-line no-empty
- for (worker of this.workers) { }
- return worker;
+ for (workerSetElement of this.workers) { }
+ return workerSetElement;
+ }
+
+ private getLastWorker(): Worker {
+ return this.getLastWorkerSetElement().worker;
}
}