1 import { isPlainObject
} from
'../utils.js'
2 import type { TaskFunction
} from
'./task-functions.js'
3 import { KillBehaviors
, type WorkerOptions
} from
'./worker-options.js'
5 export const checkValidWorkerOptions
= (
6 opts
: WorkerOptions
| undefined
8 if (opts
!= null && !isPlainObject(opts
)) {
9 throw new TypeError('opts worker options parameter is not a plain object')
12 opts
?.killBehavior
!= null &&
13 !Object.values(KillBehaviors
).includes(opts
.killBehavior
)
16 `killBehavior option '${opts.killBehavior}' is not valid`
20 opts
?.maxInactiveTime
!= null &&
21 !Number.isSafeInteger(opts
.maxInactiveTime
)
23 throw new TypeError('maxInactiveTime option is not an integer')
25 if (opts
?.maxInactiveTime
!= null && opts
.maxInactiveTime
< 5) {
27 'maxInactiveTime option is not a positive integer greater or equal than 5'
30 if (opts
?.killHandler
!= null && typeof opts
.killHandler
!== 'function') {
31 throw new TypeError('killHandler option is not a function')
35 export const checkValidTaskFunctionEntry
= <Data
= unknown
, Response
= unknown
>(
37 fn
: TaskFunction
<Data
, Response
>
39 if (typeof name
!== 'string') {
40 throw new TypeError('A taskFunctions parameter object key is not a string')
42 if (typeof name
=== 'string' && name
.trim().length
=== 0) {
44 'A taskFunctions parameter object key is an empty string'
47 if (typeof fn
!== 'function') {
49 'A taskFunctions parameter object value is not a function'
54 export const checkTaskFunctionName
= (name
: string): void => {
55 if (typeof name
!== 'string') {
56 throw new TypeError('name parameter is not a string')
58 if (typeof name
=== 'string' && name
.trim().length
=== 0) {
59 throw new TypeError('name parameter is an empty string')