- this.opts.killBehavior = opts.killBehavior ?? DEFAULT_KILL_BEHAVIOR
- this.opts.maxInactiveTime =
- opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME
- this.opts.killHandler = opts.killHandler ?? EMPTY_FUNCTION
- delete this.opts.async
+ if (opts != null && !isPlainObject(opts)) {
+ throw new TypeError('opts worker options parameter is not a plain object')
+ }
+ if (
+ opts?.killBehavior != null &&
+ !Object.values(KillBehaviors).includes(opts.killBehavior)
+ ) {
+ throw new TypeError(
+ `killBehavior option '${opts.killBehavior}' is not valid`
+ )
+ }
+ if (
+ opts?.maxInactiveTime != null &&
+ !Number.isSafeInteger(opts.maxInactiveTime)
+ ) {
+ throw new TypeError('maxInactiveTime option is not an integer')
+ }
+ if (opts?.maxInactiveTime != null && opts.maxInactiveTime < 5) {
+ throw new TypeError(
+ 'maxInactiveTime option is not a positive integer greater or equal than 5'
+ )
+ }
+ if (opts?.killHandler != null && typeof opts.killHandler !== 'function') {
+ throw new TypeError('killHandler option is not a function')
+ }
+ if (opts?.async != null) {
+ throw new Error('async option is deprecated')
+ }
+ this.opts = { ...DEFAULT_WORKER_OPTIONS, ...opts }
+ }
+
+ private checkValidTaskFunctionEntry (
+ name: string,
+ fn: TaskFunction<Data, Response>
+ ): void {
+ if (typeof name !== 'string') {
+ throw new TypeError(
+ 'A taskFunctions parameter object key is not a string'
+ )
+ }
+ if (typeof name === 'string' && name.trim().length === 0) {
+ throw new TypeError(
+ 'A taskFunctions parameter object key is an empty string'
+ )
+ }
+ if (typeof fn !== 'function') {
+ throw new TypeError(
+ 'A taskFunctions parameter object value is not a function'
+ )
+ }