a6c9ec16b64760e8f4a79feeebef0339634ff660
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'
7 * An example worker that will be always alive, you just need to **extend** this class if you want a static pool.
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.
12 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
15 export class ThreadWorker
<
16 Data
extends JSONValue
= JSONValue
,
17 Response
extends JSONValue
= JSONValue
18 > extends AbstractWorker
<MessagePort
, Data
, Response
> {
19 protected parent?: MessagePort
21 public constructor (fn
: (data
: Data
) => Response
, opts
: WorkerOptions
= {}) {
22 super('worker-thread-pool:pioardi', isMainThread
, fn
, opts
)
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)
29 this.runInAsyncScope(this.runAsync
.bind(this), this, fn
, value
)
31 this.runInAsyncScope(this.run
.bind(this), this, fn
, value
)
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
)
45 protected getMainWorker (): MessagePort
{
47 throw new Error('Parent was not set')
52 protected sendToMainWorker (message
: MessageValue
<Response
>): void {
53 this.getMainWorker().postMessage(message
)