Merge dependabot/npm_and_yarn/examples/typescript/http-server-pool/express-worker_thr...
[poolifier.git] / examples / typescript / pool.ts
index a53857c0f311a63ff321b4ce08b9e1ef02465dcd..144d55002bd048ea865365cb6696cdb887745230 100644 (file)
@@ -1,15 +1,20 @@
-import { dirname, join } from 'path'
-import { fileURLToPath } from 'url'
-import type { MyData, MyResponse } from './worker'
+import { dirname, extname, join } from 'node:path'
+import { fileURLToPath } from 'node:url'
+import type { MyData, MyResponse } from './worker.js'
 import {
   DynamicThreadPool,
   FixedThreadPool,
   availableParallelism
 } from 'poolifier'
 
-export const fixedPool = new FixedThreadPool<MyData, Promise<MyResponse>>(
+const workerFile = join(
+  dirname(fileURLToPath(import.meta.url)),
+  `worker${extname(fileURLToPath(import.meta.url))}`
+)
+
+export const fixedPool = new FixedThreadPool<MyData, MyResponse>(
   availableParallelism(),
-  join(dirname(fileURLToPath(import.meta.url)), 'worker.js'),
+  workerFile,
   {
     errorHandler: (e: Error) => {
       console.error(e)
@@ -20,10 +25,10 @@ export const fixedPool = new FixedThreadPool<MyData, Promise<MyResponse>>(
   }
 )
 
-export const dynamicPool = new DynamicThreadPool<MyData, Promise<MyResponse>>(
+export const dynamicPool = new DynamicThreadPool<MyData, MyResponse>(
   Math.floor(availableParallelism() / 2),
   availableParallelism(),
-  join(dirname(fileURLToPath(import.meta.url)), 'worker.js'),
+  workerFile,
   {
     errorHandler: (e: Error) => {
       console.error(e)
@@ -33,3 +38,9 @@ export const dynamicPool = new DynamicThreadPool<MyData, Promise<MyResponse>>(
     }
   }
 )
+
+// eslint-disable-next-line @typescript-eslint/no-misused-promises
+setTimeout(async () => {
+  await fixedPool.destroy()
+  await dynamicPool.destroy()
+}, 3000)