fix: fix formatting issue.
[poolifier.git] / examples / typescript / websocket-server-pool / ws-hybrid / src / request-handler-worker.ts
CommitLineData
02999424
JB
1import { ThreadWorker } from 'poolifier'
2import {
3 type DataPayload,
4 type ThreadWorkerData,
5 type ThreadWorkerResponse
6} from './types.js'
7
041dc05b 8const factorial: (n: number) => number = n => {
02999424
JB
9 if (n === 0) {
10 return 1
11 }
12 return factorial(n - 1) * n
13}
14
15class 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
33export const requestHandlerWorker = new RequestHandlerWorker<
34ThreadWorkerData<DataPayload>,
35ThreadWorkerResponse<DataPayload>
36>()