1 import { isPlainObject
} from
'../utils'
2 import type { TaskFunction
} from
'./task-functions'
3 import { KillBehaviors
, type WorkerOptions
} from
'./worker-options'
5 export const checkValidWorkerOptions
= (opts
: WorkerOptions
): void => {
6 if (opts
!= null && !isPlainObject(opts
)) {
7 throw new TypeError('opts worker options parameter is not a plain object')
10 opts
?.killBehavior
!= null &&
11 !Object.values(KillBehaviors
).includes(opts
.killBehavior
)
14 `killBehavior option '${opts.killBehavior}' is not valid`
18 opts
?.maxInactiveTime
!= null &&
19 !Number.isSafeInteger(opts
.maxInactiveTime
)
21 throw new TypeError('maxInactiveTime option is not an integer')
23 if (opts
?.maxInactiveTime
!= null && opts
.maxInactiveTime
< 5) {
25 'maxInactiveTime option is not a positive integer greater or equal than 5'
28 if (opts
?.killHandler
!= null && typeof opts
.killHandler
!== 'function') {
29 throw new TypeError('killHandler option is not a function')
33 export const checkValidTaskFunctionEntry
= <Data
= unknown
, Response
= unknown
>(
35 fn
: TaskFunction
<Data
, Response
>
37 if (typeof name
!== 'string') {
38 throw new TypeError('A taskFunctions parameter object key is not a string')
40 if (typeof name
=== 'string' && name
.trim().length
=== 0) {
42 'A taskFunctions parameter object key is an empty string'
45 if (typeof fn
!== 'function') {
47 'A taskFunctions parameter object value is not a function'
52 export const checkTaskFunctionName
= (name
: string): void => {
53 if (typeof name
!== 'string') {
54 throw new TypeError('name parameter is not a string')
56 if (typeof name
=== 'string' && name
.trim().length
=== 0) {
57 throw new TypeError('name parameter is an empty string')