}
this.taskFunctions = new Map<string, WorkerFunction<Data, Response>>()
if (typeof taskFunctions === 'function') {
- this.taskFunctions.set(DEFAULT_TASK_NAME, taskFunctions.bind(this))
+ const boundFn = taskFunctions.bind(this)
+ this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn)
+ this.taskFunctions.set(
+ typeof taskFunctions.name === 'string' &&
+ taskFunctions.name.trim().length > 0
+ ? taskFunctions.name
+ : 'fn1',
+ boundFn
+ )
} else if (isPlainObject(taskFunctions)) {
let firstEntry = true
for (const [name, fn] of Object.entries(taskFunctions)) {
'A taskFunctions parameter object value is not a function'
)
}
- this.taskFunctions.set(name, fn.bind(this))
+ const boundFn = fn.bind(this)
+ this.taskFunctions.set(name, boundFn)
if (firstEntry) {
- this.taskFunctions.set(DEFAULT_TASK_NAME, fn.bind(this))
+ this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn)
firstEntry = false
}
}
if (typeof fn !== 'function') {
throw new TypeError('fn parameter is not a function')
}
+ const boundFn = fn.bind(this)
try {
if (
this.taskFunctions.get(name) ===
this.taskFunctions.get(DEFAULT_TASK_NAME)
) {
- this.taskFunctions.set(DEFAULT_TASK_NAME, fn.bind(this))
+ this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn)
}
- this.taskFunctions.set(name, fn.bind(this))
+ this.taskFunctions.set(name, boundFn)
return true
} catch {
return false
try {
this.taskFunctions.set(
DEFAULT_TASK_NAME,
- this.taskFunctions.get(name)?.bind(this) as WorkerFunction<
- Data,
- Response
- >
+ this.taskFunctions.get(name) as WorkerFunction<Data, Response>
)
return true
} catch {
)
})
+ it('Verify that taskFunctions parameter with unique function is taken', () => {
+ const worker = new ThreadWorker(() => {})
+ expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
+ expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
+ expect(worker.taskFunctions.size).toBe(2)
+ expect(worker.taskFunctions.get('default')).toStrictEqual(
+ worker.taskFunctions.get('fn1')
+ )
+ })
+
it('Verify that taskFunctions parameter with multiple task functions contains function', () => {
const fn1 = () => {
return 1
return 2
}
const worker = new ClusterWorker({ fn1, fn2 })
- expect(typeof worker.taskFunctions.get('default') === 'function').toBe(true)
- expect(typeof worker.taskFunctions.get('fn1') === 'function').toBe(true)
- expect(typeof worker.taskFunctions.get('fn2') === 'function').toBe(true)
+ expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
+ expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
+ expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
+ expect(worker.taskFunctions.size).toBe(3)
+ expect(worker.taskFunctions.get('default')).toStrictEqual(
+ worker.taskFunctions.get('fn1')
+ )
})
it('Verify that handleError() method works properly', () => {
new StubWorkerWithMainWorker(() => {}).getMainWorker()
).toThrowError('Main worker not set')
})
+
+ it('Verify that hasTaskFunction() works', () => {
+ const fn1 = () => {
+ return 1
+ }
+ const fn2 = () => {
+ return 2
+ }
+ const worker = new ClusterWorker({ fn1, fn2 })
+ expect(worker.hasTaskFunction('default')).toBe(true)
+ expect(worker.hasTaskFunction('fn1')).toBe(true)
+ expect(worker.hasTaskFunction('fn2')).toBe(true)
+ expect(worker.hasTaskFunction('fn3')).toBe(false)
+ })
+
+ it('Verify that addTaskFunction() works', () => {
+ const fn1 = () => {
+ return 1
+ }
+ const fn2 = () => {
+ return 2
+ }
+ const fn1Replacement = () => {
+ return 3
+ }
+ const worker = new ThreadWorker(fn1)
+ expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
+ expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
+ expect(worker.taskFunctions.size).toBe(2)
+ expect(worker.taskFunctions.get('default')).toStrictEqual(
+ worker.taskFunctions.get('fn1')
+ )
+ expect(() => worker.addTaskFunction('default', fn2)).toThrowError(
+ new Error('Cannot add a task function with the default reserved name')
+ )
+ worker.addTaskFunction('fn2', fn2)
+ expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
+ expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
+ expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
+ expect(worker.taskFunctions.size).toBe(3)
+ expect(worker.taskFunctions.get('default')).toStrictEqual(
+ worker.taskFunctions.get('fn1')
+ )
+ worker.addTaskFunction('fn1', fn1Replacement)
+ expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
+ expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
+ expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
+ expect(worker.taskFunctions.size).toBe(3)
+ expect(worker.taskFunctions.get('default')).toStrictEqual(
+ worker.taskFunctions.get('fn1')
+ )
+ })
+
+ it('Verify that removeTaskFunction() works', () => {
+ const fn1 = () => {
+ return 1
+ }
+ const fn2 = () => {
+ return 2
+ }
+ const worker = new ThreadWorker({ fn1, fn2 })
+ expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
+ expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
+ expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
+ expect(worker.taskFunctions.size).toBe(3)
+ expect(worker.taskFunctions.get('default')).toStrictEqual(
+ worker.taskFunctions.get('fn1')
+ )
+ expect(() => worker.removeTaskFunction('default')).toThrowError(
+ new Error(
+ 'Cannot remove the task function with the default reserved name'
+ )
+ )
+ expect(() => worker.removeTaskFunction('fn1')).toThrowError(
+ new Error(
+ 'Cannot remove the task function used as the default task function'
+ )
+ )
+ worker.removeTaskFunction('fn2')
+ expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
+ expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
+ expect(worker.taskFunctions.get('fn2')).toBeUndefined()
+ expect(worker.taskFunctions.size).toBe(2)
+ })
+
+ it('Verify that setDefaultTaskFunction() works', () => {
+ const fn1 = () => {
+ return 1
+ }
+ const fn2 = () => {
+ return 2
+ }
+ const worker = new ThreadWorker({ fn1, fn2 })
+ expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
+ expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
+ expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
+ expect(worker.taskFunctions.size).toBe(3)
+ expect(worker.taskFunctions.get('default')).toStrictEqual(
+ worker.taskFunctions.get('fn1')
+ )
+ expect(() => worker.setDefaultTaskFunction('default')).toThrowError(
+ new Error(
+ 'Cannot set the default task function reserved name as the default task function'
+ )
+ )
+ worker.setDefaultTaskFunction('fn1')
+ expect(worker.taskFunctions.get('default')).toStrictEqual(
+ worker.taskFunctions.get('fn1')
+ )
+ worker.setDefaultTaskFunction('fn2')
+ expect(worker.taskFunctions.get('default')).toStrictEqual(
+ worker.taskFunctions.get('fn2')
+ )
+ })
})