Commit | Line | Data |
---|---|---|
d35e5717 JB |
1 | import { isPlainObject } from '../utils.js' |
2 | import type { TaskFunction } from './task-functions.js' | |
3 | import { KillBehaviors, type WorkerOptions } from './worker-options.js' | |
9a38f99e | 4 | |
c63a35a0 JB |
5 | export const checkValidWorkerOptions = ( |
6 | opts: WorkerOptions | undefined | |
7 | ): void => { | |
9a38f99e JB |
8 | if (opts != null && !isPlainObject(opts)) { |
9 | throw new TypeError('opts worker options parameter is not a plain object') | |
10 | } | |
11 | if ( | |
12 | opts?.killBehavior != null && | |
13 | !Object.values(KillBehaviors).includes(opts.killBehavior) | |
14 | ) { | |
15 | throw new TypeError( | |
16 | `killBehavior option '${opts.killBehavior}' is not valid` | |
17 | ) | |
18 | } | |
19 | if ( | |
20 | opts?.maxInactiveTime != null && | |
21 | !Number.isSafeInteger(opts.maxInactiveTime) | |
22 | ) { | |
23 | throw new TypeError('maxInactiveTime option is not an integer') | |
24 | } | |
25 | if (opts?.maxInactiveTime != null && opts.maxInactiveTime < 5) { | |
26 | throw new TypeError( | |
27 | 'maxInactiveTime option is not a positive integer greater or equal than 5' | |
28 | ) | |
29 | } | |
30 | if (opts?.killHandler != null && typeof opts.killHandler !== 'function') { | |
31 | throw new TypeError('killHandler option is not a function') | |
32 | } | |
9a38f99e JB |
33 | } |
34 | ||
35 | export const checkValidTaskFunctionEntry = <Data = unknown, Response = unknown>( | |
36 | name: string, | |
37 | fn: TaskFunction<Data, Response> | |
38 | ): void => { | |
39 | if (typeof name !== 'string') { | |
40 | throw new TypeError('A taskFunctions parameter object key is not a string') | |
41 | } | |
42 | if (typeof name === 'string' && name.trim().length === 0) { | |
43 | throw new TypeError( | |
44 | 'A taskFunctions parameter object key is an empty string' | |
45 | ) | |
46 | } | |
47 | if (typeof fn !== 'function') { | |
48 | throw new TypeError( | |
49 | 'A taskFunctions parameter object value is not a function' | |
50 | ) | |
51 | } | |
52 | } | |
53 | ||
54 | export const checkTaskFunctionName = (name: string): void => { | |
55 | if (typeof name !== 'string') { | |
56 | throw new TypeError('name parameter is not a string') | |
57 | } | |
58 | if (typeof name === 'string' && name.trim().length === 0) { | |
59 | throw new TypeError('name parameter is an empty string') | |
60 | } | |
61 | } |