docs: refactor TS example
[poolifier.git] / examples / typescript / pool.ts
index f85496434c2516eea46c7b5f1cde937565a2985d..fb48f7b07db76324a75afc001e131dd448a6809f 100644 (file)
@@ -1,19 +1,20 @@
-import { dirname, extname, 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 {
+  availableParallelism,
   DynamicThreadPool,
-  FixedThreadPool,
-  availableParallelism
+  FixedThreadPool
 } from 'poolifier'
-import type { MyData, MyResponse } from './worker'
+
+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,
   {
@@ -26,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,
@@ -39,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)