3 checkValidWorkerChoiceStrategy
,
4 } from
'../pools/utils.js'
5 import { isPlainObject
} from
'../utils.js'
6 import type { TaskFunctionObject
} from
'./task-functions.js'
7 import { KillBehaviors
, type WorkerOptions
} from
'./worker-options.js'
9 export const checkValidWorkerOptions
= (
10 opts
: WorkerOptions
| undefined
12 if (opts
!= null && !isPlainObject(opts
)) {
13 throw new TypeError('opts worker options parameter is not a plain object')
16 opts
?.killBehavior
!= null &&
17 !Object.values(KillBehaviors
).includes(opts
.killBehavior
)
20 `killBehavior option '${opts.killBehavior}' is not valid`
24 opts
?.maxInactiveTime
!= null &&
25 !Number.isSafeInteger(opts
.maxInactiveTime
)
27 throw new TypeError('maxInactiveTime option is not an integer')
29 if (opts
?.maxInactiveTime
!= null && opts
.maxInactiveTime
< 5) {
31 'maxInactiveTime option is not a positive integer greater or equal than 5'
34 if (opts
?.killHandler
!= null && typeof opts
.killHandler
!== 'function') {
35 throw new TypeError('killHandler option is not a function')
39 export const checkValidTaskFunctionObjectEntry
= <
44 fnObj
: TaskFunctionObject
<Data
, Response
>
46 if (typeof name
!== 'string') {
47 throw new TypeError('A taskFunctions parameter object key is not a string')
49 if (typeof name
=== 'string' && name
.trim().length
=== 0) {
51 'A taskFunctions parameter object key is an empty string'
54 if (typeof fnObj
.taskFunction
!== 'function') {
56 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
57 `taskFunction object 'taskFunction' property '${fnObj.taskFunction}' is not a function`
60 checkValidPriority(fnObj
.priority
)
61 checkValidWorkerChoiceStrategy(fnObj
.strategy
)
64 export const checkTaskFunctionName
= (name
: string): void => {
65 if (typeof name
!== 'string') {
66 throw new TypeError('name parameter is not a string')
68 if (typeof name
=== 'string' && name
.trim().length
=== 0) {
69 throw new TypeError('name parameter is an empty string')