Update examples/typescript/pool.ts
[poolifier.git] / examples / typescript / pool.ts
1 import { dirname, extname, join } from 'path'
2 import { fileURLToPath } from 'url'
3 import type { MyData, MyResponse } from './worker'
4 import {
5 DynamicThreadPool,
6 FixedThreadPool,
7 availableParallelism
8 } from 'poolifier'
9 import type { MyData, MyResponse } from './worker'
10
11 const workerFile = join(
12 dirname(fileURLToPath(import.meta.url)),
13 `worker${extname(fileURLToPath(import.meta.url))}`
14 )
15
16 export const fixedPool = new FixedThreadPool<MyData, Promise<MyResponse>>(
17 availableParallelism(),
18 workerFile,
19 {
20 errorHandler: (e: Error) => {
21 console.error(e)
22 },
23 onlineHandler: () => {
24 console.info('Worker is online')
25 }
26 }
27 )
28
29 export const dynamicPool = new DynamicThreadPool<MyData, Promise<MyResponse>>(
30 Math.floor(availableParallelism() / 2),
31 availableParallelism(),
32 workerFile,
33 {
34 errorHandler: (e: Error) => {
35 console.error(e)
36 },
37 onlineHandler: () => {
38 console.info('Worker is online')
39 }
40 }
41 )