| 1 | import { isMainThread, parentPort } from 'worker_threads' |
| 2 | import type { JSONValue, MessageValue } from '../utility-types' |
| 3 | import { AbstractWorker } from './abstract-worker' |
| 4 | import type { WorkerOptions } from './worker-options' |
| 5 | |
| 6 | /** |
| 7 | * An example worker that will be always alive, you just need to **extend** this class if you want a static pool. |
| 8 | * |
| 9 | * When this worker is inactive for more than 1 minute, it will send this info to the main thread, |
| 10 | * if you are using DynamicThreadPool, the workers created after will be killed, the min num of thread will be guaranteed. |
| 11 | * |
| 12 | * @author [Alessandro Pio Ardizio](https://github.com/pioardi) |
| 13 | * @since 0.0.1 |
| 14 | */ |
| 15 | export class ThreadWorker< |
| 16 | Data extends JSONValue = JSONValue, |
| 17 | Response extends JSONValue = JSONValue |
| 18 | > extends AbstractWorker<MessagePort, Data, Response> { |
| 19 | protected parent?: MessagePort |
| 20 | |
| 21 | public constructor (fn: (data: Data) => Response, opts: WorkerOptions = {}) { |
| 22 | super('worker-thread-pool:pioardi', isMainThread, fn, opts) |
| 23 | |
| 24 | parentPort?.on('message', (value: MessageValue<Data>) => { |
| 25 | if (value?.data && value.id) { |
| 26 | // here you will receive messages |
| 27 | // console.log('This is the main worker ' + isMainThread) |
| 28 | if (this.async) { |
| 29 | this.runInAsyncScope(this.runAsync.bind(this), this, fn, value) |
| 30 | } else { |
| 31 | this.runInAsyncScope(this.run.bind(this), this, fn, value) |
| 32 | } |
| 33 | } else if (value.parent) { |
| 34 | // save the port to communicate with the main thread |
| 35 | // this will be received once |
| 36 | this.parent = value.parent |
| 37 | } else if (value.kill) { |
| 38 | // here is time to kill this worker, just clearing the interval |
| 39 | if (this.interval) clearInterval(this.interval) |
| 40 | this.emitDestroy() |
| 41 | } |
| 42 | }) |
| 43 | } |
| 44 | |
| 45 | protected getMainWorker (): MessagePort { |
| 46 | if (!this.parent) { |
| 47 | throw new Error('Parent was not set') |
| 48 | } |
| 49 | return this.parent |
| 50 | } |
| 51 | |
| 52 | protected sendToMainWorker (message: MessageValue<Response>): void { |
| 53 | this.getMainWorker().postMessage(message) |
| 54 | } |
| 55 | } |