docs: refactor TS example
[poolifier.git] / examples / typescript / pool.ts
index 57898d98a9d53a8c89c3d774847e9cae694a53fe..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.js'
+
 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,
@@ -39,6 +43,8 @@ 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()