- parentPort.on('message', (value) => {
- if (value && value.data && value._id) {
- // here you will receive messages
- // console.log('This is the main thread ' + isMainThread)
- if (this.async) {
- this.runInAsyncScope(this._runAsync.bind(this), this, fn, value)
- } else {
- this.runInAsyncScope(this._run.bind(this), this, fn, value)
+ parentPort?.on(
+ 'message',
+ (value: {
+ data?: Response
+ _id?: number
+ parent?: MessagePort
+ kill?: number
+ }) => {
+ if (value?.data && value._id) {
+ // here you will receive messages
+ // console.log('This is the main thread ' + isMainThread)
+ if (this.async) {
+ this.runInAsyncScope(this._runAsync.bind(this), this, fn, value)
+ } else {
+ this.runInAsyncScope(this._run.bind(this), this, fn, value)
+ }
+ } else if (value.parent) {
+ // save the port to communicate with the main thread
+ // this will be received once
+ this.parent = value.parent
+ } else if (value.kill) {
+ // here is time to kill this thread, just clearing the interval
+ if (this.interval) clearInterval(this.interval)
+ this.emitDestroy()