.catch(emptyFunction)
})
+expressApp.get('/api/factorial/:number', (req: Request, res: Response) => {
+ const { number } = req.params
+ requestHandlerPool
+ .execute({ body: { number } }, 'factorial')
+ .then(response => {
+ return res.send(response.body).end()
+ })
+ .catch(emptyFunction)
+})
+
try {
expressApp.listen(port, () => {
console.info(
import { ThreadWorker } from 'poolifier'
-import { type WorkerData, type WorkerResponse } from './types.js'
+import {
+ type BodyPayload,
+ type WorkerData,
+ type WorkerResponse
+} from './types.js'
+
+const factorial: (n: number) => number = n => {
+ if (n === 0) {
+ return 1
+ }
+ return factorial(n - 1) * n
+}
class RequestHandlerWorker<
- Data extends WorkerData,
- Response extends WorkerResponse
+ Data extends WorkerData<BodyPayload>,
+ Response extends WorkerResponse<BodyPayload>
> extends ThreadWorker<Data, Response> {
public constructor () {
super({
echo: (workerData?: Data) => {
return workerData as unknown as Response
+ },
+ factorial: (workerData?: Data) => {
+ return {
+ body: { number: factorial(workerData?.body?.number as number) }
+ } as unknown as Response
}
})
}
}
export const requestHandlerWorker = new RequestHandlerWorker<
-WorkerData,
-WorkerResponse
+WorkerData<BodyPayload>,
+WorkerResponse<BodyPayload>
>()