docs: refactor TS example
[poolifier.git] / examples / typescript / pool.ts
index 3d9d9e6f482723cdb634e98bd02a9bbd46df3314..fb48f7b07db76324a75afc001e131dd448a6809f 100644 (file)
@@ -1,18 +1,20 @@
 import { dirname, extname, join } from 'node:path'
 import { fileURLToPath } from 'node:url'
-import type { MyData, MyResponse } from './worker'
+
 import {
+  availableParallelism,
   DynamicThreadPool,
-  FixedThreadPool,
-  availableParallelism
+  FixedThreadPool
 } from 'poolifier'
 
+import type { MyData, MyResponse } from './worker.js'
+
 const workerFile = join(
   dirname(fileURLToPath(import.meta.url)),
   `worker${extname(fileURLToPath(import.meta.url))}`
 )
 
-export const fixedPool = new FixedThreadPool<MyData, Promise<MyResponse>>(
+const fixedPool = new FixedThreadPool<MyData, MyResponse>(
   availableParallelism(),
   workerFile,
   {
@@ -25,7 +27,9 @@ export const fixedPool = new FixedThreadPool<MyData, Promise<MyResponse>>(
   }
 )
 
-export const dynamicPool = new DynamicThreadPool<MyData, Promise<MyResponse>>(
+await fixedPool.execute()
+
+const dynamicPool = new DynamicThreadPool<MyData, MyResponse>(
   Math.floor(availableParallelism() / 2),
   availableParallelism(),
   workerFile,
@@ -38,3 +42,11 @@ export const dynamicPool = new DynamicThreadPool<MyData, Promise<MyResponse>>(
     }
   }
 )
+
+await dynamicPool.execute()
+
+// eslint-disable-next-line @typescript-eslint/no-misused-promises
+setTimeout(async () => {
+  await fixedPool.destroy()
+  await dynamicPool.destroy()
+}, 3000)