Commit | Line | Data |
---|---|---|
97231086 JB |
1 | import type { TaskFunctionObject } from './task-functions.js' |
2 | ||
d0bd5062 JB |
3 | import { |
4 | checkValidPriority, | |
3a502712 | 5 | checkValidWorkerChoiceStrategy, |
d0bd5062 | 6 | } from '../pools/utils.js' |
d35e5717 | 7 | import { isPlainObject } from '../utils.js' |
d35e5717 | 8 | import { KillBehaviors, type WorkerOptions } from './worker-options.js' |
9a38f99e | 9 | |
c63a35a0 | 10 | export const checkValidWorkerOptions = ( |
97231086 | 11 | opts: undefined | WorkerOptions |
c63a35a0 | 12 | ): void => { |
9a38f99e JB |
13 | if (opts != null && !isPlainObject(opts)) { |
14 | throw new TypeError('opts worker options parameter is not a plain object') | |
15 | } | |
16 | if ( | |
17 | opts?.killBehavior != null && | |
18 | !Object.values(KillBehaviors).includes(opts.killBehavior) | |
19 | ) { | |
20 | throw new TypeError( | |
21 | `killBehavior option '${opts.killBehavior}' is not valid` | |
22 | ) | |
23 | } | |
24 | if ( | |
25 | opts?.maxInactiveTime != null && | |
26 | !Number.isSafeInteger(opts.maxInactiveTime) | |
27 | ) { | |
28 | throw new TypeError('maxInactiveTime option is not an integer') | |
29 | } | |
30 | if (opts?.maxInactiveTime != null && opts.maxInactiveTime < 5) { | |
31 | throw new TypeError( | |
32 | 'maxInactiveTime option is not a positive integer greater or equal than 5' | |
33 | ) | |
34 | } | |
35 | if (opts?.killHandler != null && typeof opts.killHandler !== 'function') { | |
36 | throw new TypeError('killHandler option is not a function') | |
37 | } | |
9a38f99e JB |
38 | } |
39 | ||
bcfb06ce JB |
40 | export const checkValidTaskFunctionObjectEntry = < |
41 | Data = unknown, | |
42 | Response = unknown | |
43 | >( | |
44 | name: string, | |
45 | fnObj: TaskFunctionObject<Data, Response> | |
46 | ): void => { | |
9a38f99e JB |
47 | if (typeof name !== 'string') { |
48 | throw new TypeError('A taskFunctions parameter object key is not a string') | |
49 | } | |
50 | if (typeof name === 'string' && name.trim().length === 0) { | |
51 | throw new TypeError( | |
52 | 'A taskFunctions parameter object key is an empty string' | |
53 | ) | |
54 | } | |
31847469 | 55 | if (typeof fnObj.taskFunction !== 'function') { |
9a38f99e | 56 | throw new TypeError( |
6e5d7052 | 57 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
31847469 | 58 | `taskFunction object 'taskFunction' property '${fnObj.taskFunction}' is not a function` |
9a38f99e JB |
59 | ) |
60 | } | |
d0bd5062 | 61 | checkValidPriority(fnObj.priority) |
31847469 | 62 | checkValidWorkerChoiceStrategy(fnObj.strategy) |
9a38f99e JB |
63 | } |
64 | ||
65 | export const checkTaskFunctionName = (name: string): void => { | |
66 | if (typeof name !== 'string') { | |
67 | throw new TypeError('name parameter is not a string') | |
68 | } | |
69 | if (typeof name === 'string' && name.trim().length === 0) { | |
70 | throw new TypeError('name parameter is an empty string') | |
71 | } | |
72 | } |