1 import { checkValidWorkerChoiceStrategy
} from
'../pools/utils.js'
2 import { isPlainObject
} from
'../utils.js'
3 import type { TaskFunctionObject
} from
'./task-functions.js'
4 import { KillBehaviors
, type WorkerOptions
} from
'./worker-options.js'
6 export const checkValidWorkerOptions
= (
7 opts
: WorkerOptions
| undefined
9 if (opts
!= null && !isPlainObject(opts
)) {
10 throw new TypeError('opts worker options parameter is not a plain object')
13 opts
?.killBehavior
!= null &&
14 !Object.values(KillBehaviors
).includes(opts
.killBehavior
)
17 `killBehavior option '${opts.killBehavior}' is not valid`
21 opts
?.maxInactiveTime
!= null &&
22 !Number.isSafeInteger(opts
.maxInactiveTime
)
24 throw new TypeError('maxInactiveTime option is not an integer')
26 if (opts
?.maxInactiveTime
!= null && opts
.maxInactiveTime
< 5) {
28 'maxInactiveTime option is not a positive integer greater or equal than 5'
31 if (opts
?.killHandler
!= null && typeof opts
.killHandler
!== 'function') {
32 throw new TypeError('killHandler option is not a function')
36 export const checkValidTaskFunctionEntry
= <Data
= unknown
, Response
= unknown
>(
38 fnObj
: TaskFunctionObject
<Data
, Response
>
40 if (typeof name
!== 'string') {
41 throw new TypeError('A taskFunctions parameter object key is not a string')
43 if (typeof name
=== 'string' && name
.trim().length
=== 0) {
45 'A taskFunctions parameter object key is an empty string'
48 if (typeof fnObj
.taskFunction
!== 'function') {
50 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
51 `taskFunction object 'taskFunction' property '${fnObj.taskFunction}' is not a function`
54 if (fnObj
.priority
!= null && !Number.isSafeInteger(fnObj
.priority
)) {
56 `taskFunction object 'priority' property '${fnObj.priority}' is not an integer`
59 checkValidWorkerChoiceStrategy(fnObj
.strategy
)
62 export const checkTaskFunctionName
= (name
: string): void => {
63 if (typeof name
!== 'string') {
64 throw new TypeError('name parameter is not a string')
66 if (typeof name
=== 'string' && name
.trim().length
=== 0) {
67 throw new TypeError('name parameter is an empty string')