| 1 | import { WorkerData, WorkerOptions } from '../types/Worker'; |
| 2 | |
| 3 | import WorkerConstants from './WorkerConstants'; |
| 4 | |
| 5 | export default abstract class WorkerAbstract<T extends WorkerData> { |
| 6 | protected readonly workerScript: string; |
| 7 | protected readonly workerOptions: WorkerOptions; |
| 8 | public abstract readonly size: number; |
| 9 | public abstract readonly maxElementsPerWorker: number | null; |
| 10 | |
| 11 | /** |
| 12 | * `WorkerAbstract` constructor. |
| 13 | * |
| 14 | * @param workerScript |
| 15 | * @param workerOptions |
| 16 | */ |
| 17 | constructor( |
| 18 | workerScript: string, |
| 19 | workerOptions: WorkerOptions = { |
| 20 | workerStartDelay: WorkerConstants.DEFAULT_WORKER_START_DELAY, |
| 21 | elementStartDelay: WorkerConstants.DEFAULT_ELEMENT_START_DELAY, |
| 22 | poolMinSize: WorkerConstants.DEFAULT_POOL_MIN_SIZE, |
| 23 | poolMaxSize: WorkerConstants.DEFAULT_POOL_MAX_SIZE, |
| 24 | elementsPerWorker: WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER, |
| 25 | poolOptions: {}, |
| 26 | messageHandler: () => { |
| 27 | /* This is intentional */ |
| 28 | }, |
| 29 | } |
| 30 | ) { |
| 31 | this.workerScript = workerScript; |
| 32 | this.workerOptions = workerOptions; |
| 33 | } |
| 34 | |
| 35 | public abstract start(): Promise<void>; |
| 36 | public abstract stop(): Promise<void>; |
| 37 | public abstract addElement(elementData: T): Promise<void>; |
| 38 | } |