| 22 | } from './types'; |
| 23 | |
| 24 | export default class Farm { |
| 25 | private readonly _computeWorkerKey: WorkerFarmOptions['computeWorkerKey']; |
| 26 | private readonly _workerSchedulingPolicy: WorkerSchedulingPolicy; |
| 27 | private readonly _cacheKeys: Record<string, WorkerInterface> = |
| 28 | Object.create(null); |
| 29 | private readonly _locks: Array<boolean> = []; |
| 30 | private _offset = 0; |
| 31 | private readonly _taskQueue: TaskQueue; |
| 32 | |
| 33 | constructor( |
| 34 | private readonly _numOfWorkers: number, |
| 35 | private readonly _callback: WorkerCallback, |
| 36 | options: WorkerFarmOptions = {}, |
| 37 | ) { |
| 38 | this._computeWorkerKey = options.computeWorkerKey; |
| 39 | this._workerSchedulingPolicy = |
| 40 | options.workerSchedulingPolicy ?? 'round-robin'; |
| 41 | this._taskQueue = options.taskQueue ?? new FifoQueue(); |
| 42 | } |
| 43 | |
| 44 | doWork( |
| 45 | method: string, |
| 46 | ...args: Array<unknown> |
| 47 | ): PromiseWithCustomMessage<unknown> { |
| 48 | const customMessageListeners = new Set<OnCustomMessage>(); |
| 49 | |
| 50 | const addCustomMessageListener = (listener: OnCustomMessage) => { |
| 51 | customMessageListeners.add(listener); |
| 52 | return () => { |
| 53 | customMessageListeners.delete(listener); |
| 54 | }; |
| 55 | }; |
| 56 | |
| 57 | const onCustomMessage: OnCustomMessage = message => { |
| 58 | for (const listener of customMessageListeners) listener(message); |
| 59 | }; |
| 60 | |
| 61 | const promise: PromiseWithCustomMessage<unknown> = new Promise( |
| 62 | // Bind args to this function so it won't reference to the parent scope. |
| 63 | // This prevents a memory leak in v8, because otherwise the function will |
| 64 | // retain args for the closure. |
| 65 | (( |
| 66 | args: Array<unknown>, |
| 67 | resolve: (value: unknown) => void, |
| 68 | reject: (reason?: any) => void, |
| 69 | ) => { |
| 70 | const computeWorkerKey = this._computeWorkerKey; |
| 71 | const request: ChildMessage = [CHILD_MESSAGE_CALL, false, method, args]; |
| 72 | |
| 73 | let worker: WorkerInterface | null = null; |
| 74 | let hash: string | null = null; |
| 75 | |
| 76 | if (computeWorkerKey) { |
| 77 | hash = computeWorkerKey.call(this, method, ...args); |
| 78 | worker = hash == null ? null : this._cacheKeys[hash]; |
| 79 | } |
| 80 | |
| 81 | const onStart: OnStart = (worker: WorkerInterface) => { |