chore: cleanup `@std/expect` imports
[poolifier.git] / src / request-handler-worker.ts
1 import { ThreadWorker } from 'poolifier'
2 import {
3 type DataPayload,
4 type ThreadWorkerData,
5 type ThreadWorkerResponse
6 } from './types.js'
7
8 const factorial: (n: number) => number = n => {
9 if (n === 0) {
10 return 1
11 }
12 return factorial(n - 1) * n
13 }
14
15 class RequestHandlerWorker<
16 Data extends ThreadWorkerData<DataPayload>,
17 Response extends ThreadWorkerResponse<DataPayload>
18 > extends ThreadWorker<Data, Response> {
19 public constructor () {
20 super({
21 echo: (workerData?: Data) => {
22 return workerData as unknown as Response
23 },
24 factorial: (workerData?: Data) => {
25 return {
26 data: { number: factorial(workerData?.data?.number as number) }
27 } as unknown as Response
28 }
29 })
30 }
31 }
32
33 export const requestHandlerWorker = new RequestHandlerWorker<
34 ThreadWorkerData<DataPayload>,
35 ThreadWorkerResponse<DataPayload>
36 >()