/**
* Kill code.
*/
- readonly kill?: KillBehavior | 1
+ readonly kill?: KillBehavior | true
/**
* Task error.
*/
*/
readonly started?: boolean
/**
- * Whether the worker is dynamic or not.
+ * Whether the worker starts or stops its aliveness check.
*/
- readonly dynamic?: boolean
+ readonly checkAlive?: boolean
}
/**
if (message.statistics != null) {
// Statistics message received
this.statistics = message.statistics
- } else if (message.dynamic === true) {
- // Worker dynamic message received
- this.startCheckAlive()
+ } else if (message.checkAlive != null) {
+ // Check alive message received
+ message.checkAlive ? this.startCheckAlive() : this.stopCheckAlive()
} else if (message.id != null && message.data != null) {
// Task message received
const fn = this.getTaskFunction(message.name)
} else {
this.runInAsyncScope(this.runSync.bind(this), this, fn, message)
}
- } else if (message.kill != null) {
+ } else if (message.kill === true) {
// Kill message received
- this.aliveInterval != null && clearInterval(this.aliveInterval)
+ this.stopCheckAlive()
this.emitDestroy()
}
}
+ /**
+ * Starts the worker alive check interval.
+ */
private startCheckAlive (): void {
this.lastTaskTimestamp = performance.now()
this.aliveInterval = setInterval(
this.checkAlive.bind(this)()
}
+ /**
+ * Stops the worker alive check interval.
+ */
+ private stopCheckAlive (): void {
+ this.aliveInterval != null && clearInterval(this.aliveInterval)
+ }
+
+ /**
+ * Checks if the worker should be terminated, because its living too long.
+ */
+ private checkAlive (): void {
+ if (
+ performance.now() - this.lastTaskTimestamp >
+ (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME)
+ ) {
+ this.sendToMainWorker({ kill: this.opts.killBehavior })
+ }
+ }
+
/**
* Returns the main worker.
*
message: MessageValue<Response, Data>
): void
- /**
- * Checks if the worker should be terminated, because its living too long.
- */
- protected checkAlive (): void {
- if (
- performance.now() - this.lastTaskTimestamp >
- (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME)
- ) {
- this.sendToMainWorker({ kill: this.opts.killBehavior })
- }
- }
-
/**
* Handles an error and convert it to a string so it can be sent back to the main worker.
*