refactor: add PoolEvents/PoolEvent types
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 7 Apr 2023 21:15:34 +0000 (23:15 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 7 Apr 2023 21:15:34 +0000 (23:15 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
17 files changed:
CHANGELOG.md
README.md
examples/dynamicExample.js
examples/fixedExample.js
package.json
pnpm-lock.yaml
src/index.ts
src/pools/abstract-pool.ts
src/pools/pool.ts
src/pools/selection-strategies/worker-choice-strategy-context.ts
src/utility-types.ts
src/worker/abstract-worker.ts
tests/pools/abstract/abstract-pool.test.js
tests/pools/cluster/dynamic.test.js
tests/pools/cluster/fixed.test.js
tests/pools/thread/dynamic.test.js
tests/pools/thread/fixed.test.js

index 583021716de9841cff2fba1d25cdbfef1818f21a..97c416b3067dee899d3d649a324a2bce0f9b844b 100644 (file)
@@ -7,9 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [Unreleased]
 
+### Added
+
+- Add `PoolEvents` enumeration and `PoolEvent` type.
+
 ### Fixed
 
-- fix: destroy worker only on check alive.
+- fix: destroy worker only on alive check.
 
 ## [2.4.3] - 2023-04-07
 
index dc956759aef4cfa76ae4a409e4a4cd0a1fa1dd2c..49f24ef5e11c6e201568c99f7386716a2933b053 100644 (file)
--- a/README.md
+++ b/README.md
@@ -113,22 +113,22 @@ Instantiate your pool based on your needed :
 
 ```js
 'use strict'
-const { FixedThreadPool, DynamicThreadPool } = require('poolifier')
+const { DynamicThreadPool, FixedThreadPool, PoolEvents } = require('poolifier')
 
 // a fixed worker-threads pool
 const pool = new FixedThreadPool(15,
   './yourWorker.js',
   { errorHandler: (e) => console.error(e), onlineHandler: () => console.log('worker is online') })
 
-pool.emitter.on('busy', () => console.log('Pool is busy'))
+pool.emitter.on(PoolEvents.busy, () => console.log('Pool is busy'))
 
 // or a dynamic worker-threads pool
 const pool = new DynamicThreadPool(10, 100,
   './yourWorker.js',
   { errorHandler: (e) => console.error(e), onlineHandler: () => console.log('worker is online') })
 
-pool.emitter.on('full', () => console.log('Pool is full'))
-pool.emitter.on('busy', () => console.log('Pool is busy'))
+pool.emitter.on(PoolEvents.full, () => console.log('Pool is full'))
+pool.emitter.on(PoolEvents.busy, () => console.log('Pool is busy'))
 
 // the execute method signature is the same for both implementations,
 // so you can easy switch from one to another
index 79f73c11c53b0f296cd077ecca35fc53a18cab1c..e1d9b5035c1e1f45e1be29cc2f9b20c184f4c2f1 100644 (file)
@@ -1,4 +1,4 @@
-const { DynamicThreadPool } = require('poolifier')
+const { DynamicThreadPool, PoolEvents } = require('poolifier')
 let resolved = 0
 let poolFull = 0
 let poolBusy = 0
@@ -6,8 +6,8 @@ const pool = new DynamicThreadPool(10, 20, './yourWorker.js', {
   errorHandler: e => console.error(e),
   onlineHandler: () => console.log('worker is online')
 })
-pool.emitter.on('full', () => poolFull++)
-pool.emitter.on('busy', () => poolBusy++)
+pool.emitter.on(PoolEvents.full, () => poolFull++)
+pool.emitter.on(PoolEvents.busy, () => poolBusy++)
 
 const start = Date.now()
 const iterations = 1000
index c6ea884897191956ddf2491608bfac175e2c923c..d10c206281fe03af8b6c1e4e08399e50ff5a59b4 100644 (file)
@@ -1,11 +1,11 @@
-const { FixedThreadPool } = require('poolifier')
+const { FixedThreadPool, PoolEvents } = require('poolifier')
 let resolved = 0
 let poolBusy = 0
 const pool = new FixedThreadPool(15, './yourWorker.js', {
   errorHandler: e => console.error(e),
   onlineHandler: () => console.log('worker is online')
 })
-pool.emitter.on('busy', () => poolBusy++)
+pool.emitter.on(PoolEvents.busy, () => poolBusy++)
 
 const start = Date.now()
 const iterations = 1000
index 8bcd8a8ebf03ea87b1536995bf6e590c886acf7f..d7befe0af7f7efb767b5bead01c9118e54bef019 100644 (file)
     "source-map-support": "^0.5.21",
     "ts-standard": "^12.0.2",
     "typedoc": "^0.23.28",
-    "typescript": "^5.0.3"
+    "typescript": "^5.0.4"
   }
 }
index 06dadc5717bdd93641e7ffa6a6aad8ec471d8243..933753fbf912b6cf5e5c1f16db3e5239d9853f60 100644 (file)
@@ -18,16 +18,16 @@ devDependencies:
     version: 0.4.0(rollup@3.20.2)
   '@rollup/plugin-typescript':
     specifier: ^11.1.0
-    version: 11.1.0(rollup@3.20.2)(typescript@5.0.3)
+    version: 11.1.0(rollup@3.20.2)(typescript@5.0.4)
   '@types/node':
     specifier: ^18.15.11
     version: 18.15.11
   '@typescript-eslint/eslint-plugin':
     specifier: ^5.57.1
-    version: 5.57.1(@typescript-eslint/parser@5.57.1)(eslint@8.37.0)(typescript@5.0.3)
+    version: 5.57.1(@typescript-eslint/parser@5.57.1)(eslint@8.37.0)(typescript@5.0.4)
   '@typescript-eslint/parser':
     specifier: ^5.57.1
-    version: 5.57.1(eslint@8.37.0)(typescript@5.0.3)
+    version: 5.57.1(eslint@8.37.0)(typescript@5.0.4)
   benny:
     specifier: ^3.7.1
     version: 3.7.1
@@ -42,7 +42,7 @@ devDependencies:
     version: 17.0.0(eslint-plugin-import@2.27.5)(eslint-plugin-n@15.7.0)(eslint-plugin-promise@6.1.1)(eslint@8.37.0)
   eslint-config-standard-with-typescript:
     specifier: ^34.0.1
-    version: 34.0.1(@typescript-eslint/eslint-plugin@5.57.1)(eslint-plugin-import@2.27.5)(eslint-plugin-n@15.7.0)(eslint-plugin-promise@6.1.1)(eslint@8.37.0)(typescript@5.0.3)
+    version: 34.0.1(@typescript-eslint/eslint-plugin@5.57.1)(eslint-plugin-import@2.27.5)(eslint-plugin-n@15.7.0)(eslint-plugin-promise@6.1.1)(eslint@8.37.0)(typescript@5.0.4)
   eslint-define-config:
     specifier: ^1.17.0
     version: 1.17.0
@@ -90,7 +90,7 @@ devDependencies:
     version: 2.8.7
   prettier-plugin-organize-imports:
     specifier: ^3.2.2
-    version: 3.2.2(prettier@2.8.7)(typescript@5.0.3)
+    version: 3.2.2(prettier@2.8.7)(typescript@5.0.4)
   release-it:
     specifier: ^15.10.1
     version: 15.10.1
@@ -114,13 +114,13 @@ devDependencies:
     version: 0.5.21
   ts-standard:
     specifier: ^12.0.2
-    version: 12.0.2(eslint-import-resolver-typescript@3.5.5)(typescript@5.0.3)
+    version: 12.0.2(eslint-import-resolver-typescript@3.5.5)(typescript@5.0.4)
   typedoc:
     specifier: ^0.23.28
-    version: 0.23.28(typescript@5.0.3)
+    version: 0.23.28(typescript@5.0.4)
   typescript:
-    specifier: ^5.0.3
-    version: 5.0.3
+    specifier: ^5.0.4
+    version: 5.0.4
 
 packages:
 
@@ -267,13 +267,13 @@ packages:
       '@types/node': 18.15.11
       chalk: 4.1.2
       cosmiconfig: 8.1.3
-      cosmiconfig-typescript-loader: 4.3.0(@types/node@18.15.11)(cosmiconfig@8.1.3)(ts-node@10.9.1)(typescript@5.0.3)
+      cosmiconfig-typescript-loader: 4.3.0(@types/node@18.15.11)(cosmiconfig@8.1.3)(ts-node@10.9.1)(typescript@5.0.4)
       lodash.isplainobject: 4.0.6
       lodash.merge: 4.6.2
       lodash.uniq: 4.5.0
       resolve-from: 5.0.0
-      ts-node: 10.9.1(@types/node@18.15.11)(typescript@5.0.3)
-      typescript: 5.0.3
+      ts-node: 10.9.1(@types/node@18.15.11)(typescript@5.0.4)
+      typescript: 5.0.4
     transitivePeerDependencies:
       - '@swc/core'
       - '@swc/wasm'
@@ -734,7 +734,7 @@ packages:
       terser: 5.16.8
     dev: true
 
-  /@rollup/plugin-typescript@11.1.0(rollup@3.20.2)(typescript@5.0.3):
+  /@rollup/plugin-typescript@11.1.0(rollup@3.20.2)(typescript@5.0.4):
     resolution: {integrity: sha512-86flrfE+bSHB69znnTV6kVjkncs2LBMhcTCyxWgRxLyfXfQrxg4UwlAqENnjrrxnSNS/XKCDJCl8EkdFJVHOxw==}
     engines: {node: '>=14.0.0'}
     peerDependencies:
@@ -750,7 +750,7 @@ packages:
       '@rollup/pluginutils': 5.0.2(rollup@3.20.2)
       resolve: 1.22.2
       rollup: 3.20.2
-      typescript: 5.0.3
+      typescript: 5.0.4
     dev: true
 
   /@rollup/pluginutils@5.0.2(rollup@3.20.2):
@@ -908,7 +908,7 @@ packages:
       '@types/yargs-parser': 21.0.0
     dev: true
 
-  /@typescript-eslint/eslint-plugin@5.57.1(@typescript-eslint/parser@5.57.1)(eslint@8.37.0)(typescript@5.0.3):
+  /@typescript-eslint/eslint-plugin@5.57.1(@typescript-eslint/parser@5.57.1)(eslint@8.37.0)(typescript@5.0.4):
     resolution: {integrity: sha512-1MeobQkQ9tztuleT3v72XmY0XuKXVXusAhryoLuU5YZ+mXoYKZP9SQ7Flulh1NX4DTjpGTc2b/eMu4u7M7dhnQ==}
     engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}
     peerDependencies:
@@ -920,23 +920,23 @@ packages:
         optional: true
     dependencies:
       '@eslint-community/regexpp': 4.5.0
-      '@typescript-eslint/parser': 5.57.1(eslint@8.37.0)(typescript@5.0.3)
+      '@typescript-eslint/parser': 5.57.1(eslint@8.37.0)(typescript@5.0.4)
       '@typescript-eslint/scope-manager': 5.57.1
-      '@typescript-eslint/type-utils': 5.57.1(eslint@8.37.0)(typescript@5.0.3)
-      '@typescript-eslint/utils': 5.57.1(eslint@8.37.0)(typescript@5.0.3)
+      '@typescript-eslint/type-utils': 5.57.1(eslint@8.37.0)(typescript@5.0.4)
+      '@typescript-eslint/utils': 5.57.1(eslint@8.37.0)(typescript@5.0.4)
       debug: 4.3.4(supports-color@8.1.1)
       eslint: 8.37.0
       grapheme-splitter: 1.0.4
       ignore: 5.2.4
       natural-compare-lite: 1.4.0
       semver: 7.3.8
-      tsutils: 3.21.0(typescript@5.0.3)
-      typescript: 5.0.3
+      tsutils: 3.21.0(typescript@5.0.4)
+      typescript: 5.0.4
     transitivePeerDependencies:
       - supports-color
     dev: true
 
-  /@typescript-eslint/parser@5.57.1(eslint@8.37.0)(typescript@5.0.3):
+  /@typescript-eslint/parser@5.57.1(eslint@8.37.0)(typescript@5.0.4):
     resolution: {integrity: sha512-hlA0BLeVSA/wBPKdPGxoVr9Pp6GutGoY380FEhbVi0Ph4WNe8kLvqIRx76RSQt1lynZKfrXKs0/XeEk4zZycuA==}
     engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}
     peerDependencies:
@@ -948,10 +948,10 @@ packages:
     dependencies:
       '@typescript-eslint/scope-manager': 5.57.1
       '@typescript-eslint/types': 5.57.1
-      '@typescript-eslint/typescript-estree': 5.57.1(typescript@5.0.3)
+      '@typescript-eslint/typescript-estree': 5.57.1(typescript@5.0.4)
       debug: 4.3.4(supports-color@8.1.1)
       eslint: 8.37.0
-      typescript: 5.0.3
+      typescript: 5.0.4
     transitivePeerDependencies:
       - supports-color
     dev: true
@@ -964,7 +964,7 @@ packages:
       '@typescript-eslint/visitor-keys': 5.57.1
     dev: true
 
-  /@typescript-eslint/type-utils@5.57.1(eslint@8.37.0)(typescript@5.0.3):
+  /@typescript-eslint/type-utils@5.57.1(eslint@8.37.0)(typescript@5.0.4):
     resolution: {integrity: sha512-/RIPQyx60Pt6ga86hKXesXkJ2WOS4UemFrmmq/7eOyiYjYv/MUSHPlkhU6k9T9W1ytnTJueqASW+wOmW4KrViw==}
     engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}
     peerDependencies:
@@ -974,12 +974,12 @@ packages:
       typescript:
         optional: true
     dependencies:
-      '@typescript-eslint/typescript-estree': 5.57.1(typescript@5.0.3)
-      '@typescript-eslint/utils': 5.57.1(eslint@8.37.0)(typescript@5.0.3)
+      '@typescript-eslint/typescript-estree': 5.57.1(typescript@5.0.4)
+      '@typescript-eslint/utils': 5.57.1(eslint@8.37.0)(typescript@5.0.4)
       debug: 4.3.4(supports-color@8.1.1)
       eslint: 8.37.0
-      tsutils: 3.21.0(typescript@5.0.3)
-      typescript: 5.0.3
+      tsutils: 3.21.0(typescript@5.0.4)
+      typescript: 5.0.4
     transitivePeerDependencies:
       - supports-color
     dev: true
@@ -989,7 +989,7 @@ packages:
     engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}
     dev: true
 
-  /@typescript-eslint/typescript-estree@5.57.1(typescript@5.0.3):
+  /@typescript-eslint/typescript-estree@5.57.1(typescript@5.0.4):
     resolution: {integrity: sha512-A2MZqD8gNT0qHKbk2wRspg7cHbCDCk2tcqt6ScCFLr5Ru8cn+TCfM786DjPhqwseiS+PrYwcXht5ztpEQ6TFTw==}
     engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}
     peerDependencies:
@@ -1004,13 +1004,13 @@ packages:
       globby: 11.1.0
       is-glob: 4.0.3
       semver: 7.3.8
-      tsutils: 3.21.0(typescript@5.0.3)
-      typescript: 5.0.3
+      tsutils: 3.21.0(typescript@5.0.4)
+      typescript: 5.0.4
     transitivePeerDependencies:
       - supports-color
     dev: true
 
-  /@typescript-eslint/utils@5.57.1(eslint@8.37.0)(typescript@5.0.3):
+  /@typescript-eslint/utils@5.57.1(eslint@8.37.0)(typescript@5.0.4):
     resolution: {integrity: sha512-kN6vzzf9NkEtawECqze6v99LtmDiUJCVpvieTFA1uL7/jDghiJGubGZ5csicYHU1Xoqb3oH/R5cN5df6W41Nfg==}
     engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}
     peerDependencies:
@@ -1021,7 +1021,7 @@ packages:
       '@types/semver': 7.3.13
       '@typescript-eslint/scope-manager': 5.57.1
       '@typescript-eslint/types': 5.57.1
-      '@typescript-eslint/typescript-estree': 5.57.1(typescript@5.0.3)
+      '@typescript-eslint/typescript-estree': 5.57.1(typescript@5.0.4)
       eslint: 8.37.0
       eslint-scope: 5.1.1
       semver: 7.3.8
@@ -1708,7 +1708,7 @@ packages:
     resolution: {integrity: sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==}
     dev: true
 
-  /cosmiconfig-typescript-loader@4.3.0(@types/node@18.15.11)(cosmiconfig@8.1.3)(ts-node@10.9.1)(typescript@5.0.3):
+  /cosmiconfig-typescript-loader@4.3.0(@types/node@18.15.11)(cosmiconfig@8.1.3)(ts-node@10.9.1)(typescript@5.0.4):
     resolution: {integrity: sha512-NTxV1MFfZDLPiBMjxbHRwSh5LaLcPMwNdCutmnHJCKoVnlvldPWlllonKwrsRJ5pYZBIBGRWWU2tfvzxgeSW5Q==}
     engines: {node: '>=12', npm: '>=6'}
     peerDependencies:
@@ -1719,8 +1719,8 @@ packages:
     dependencies:
       '@types/node': 18.15.11
       cosmiconfig: 8.1.3
-      ts-node: 10.9.1(@types/node@18.15.11)(typescript@5.0.3)
-      typescript: 5.0.3
+      ts-node: 10.9.1(@types/node@18.15.11)(typescript@5.0.4)
+      typescript: 5.0.4
     dev: true
 
   /cosmiconfig@8.1.3:
@@ -2140,7 +2140,7 @@ packages:
       eslint-plugin-react: 7.32.2(eslint@8.37.0)
     dev: true
 
-  /eslint-config-standard-with-typescript@23.0.0(@typescript-eslint/eslint-plugin@5.57.1)(eslint-plugin-import@2.27.5)(eslint-plugin-n@15.7.0)(eslint-plugin-promise@6.1.1)(eslint@8.37.0)(typescript@5.0.3):
+  /eslint-config-standard-with-typescript@23.0.0(@typescript-eslint/eslint-plugin@5.57.1)(eslint-plugin-import@2.27.5)(eslint-plugin-n@15.7.0)(eslint-plugin-promise@6.1.1)(eslint@8.37.0)(typescript@5.0.4):
     resolution: {integrity: sha512-iaaWifImn37Z1OXbNW1es7KI+S7D408F9ys0bpaQf2temeBWlvb0Nc5qHkOgYaRb5QxTZT32GGeN1gtswASOXA==}
     peerDependencies:
       '@typescript-eslint/eslint-plugin': ^5.0.0
@@ -2150,19 +2150,19 @@ packages:
       eslint-plugin-promise: ^6.0.0
       typescript: '*'
     dependencies:
-      '@typescript-eslint/eslint-plugin': 5.57.1(@typescript-eslint/parser@5.57.1)(eslint@8.37.0)(typescript@5.0.3)
-      '@typescript-eslint/parser': 5.57.1(eslint@8.37.0)(typescript@5.0.3)
+      '@typescript-eslint/eslint-plugin': 5.57.1(@typescript-eslint/parser@5.57.1)(eslint@8.37.0)(typescript@5.0.4)
+      '@typescript-eslint/parser': 5.57.1(eslint@8.37.0)(typescript@5.0.4)
       eslint: 8.37.0
       eslint-config-standard: 17.0.0(eslint-plugin-import@2.27.5)(eslint-plugin-n@15.7.0)(eslint-plugin-promise@6.1.1)(eslint@8.37.0)
       eslint-plugin-import: 2.27.5(@typescript-eslint/parser@5.57.1)(eslint-import-resolver-typescript@3.5.5)(eslint@8.37.0)
       eslint-plugin-n: 15.7.0(eslint@8.37.0)
       eslint-plugin-promise: 6.1.1(eslint@8.37.0)
-      typescript: 5.0.3
+      typescript: 5.0.4
     transitivePeerDependencies:
       - supports-color
     dev: true
 
-  /eslint-config-standard-with-typescript@34.0.1(@typescript-eslint/eslint-plugin@5.57.1)(eslint-plugin-import@2.27.5)(eslint-plugin-n@15.7.0)(eslint-plugin-promise@6.1.1)(eslint@8.37.0)(typescript@5.0.3):
+  /eslint-config-standard-with-typescript@34.0.1(@typescript-eslint/eslint-plugin@5.57.1)(eslint-plugin-import@2.27.5)(eslint-plugin-n@15.7.0)(eslint-plugin-promise@6.1.1)(eslint@8.37.0)(typescript@5.0.4):
     resolution: {integrity: sha512-J7WvZeLtd0Vr9F+v4dZbqJCLD16cbIy4U+alJMq4MiXdpipdBM3U5NkXaGUjePc4sb1ZE01U9g6VuTBpHHz1fg==}
     peerDependencies:
       '@typescript-eslint/eslint-plugin': ^5.43.0
@@ -2172,14 +2172,14 @@ packages:
       eslint-plugin-promise: ^6.0.0
       typescript: '*'
     dependencies:
-      '@typescript-eslint/eslint-plugin': 5.57.1(@typescript-eslint/parser@5.57.1)(eslint@8.37.0)(typescript@5.0.3)
-      '@typescript-eslint/parser': 5.57.1(eslint@8.37.0)(typescript@5.0.3)
+      '@typescript-eslint/eslint-plugin': 5.57.1(@typescript-eslint/parser@5.57.1)(eslint@8.37.0)(typescript@5.0.4)
+      '@typescript-eslint/parser': 5.57.1(eslint@8.37.0)(typescript@5.0.4)
       eslint: 8.37.0
       eslint-config-standard: 17.0.0(eslint-plugin-import@2.27.5)(eslint-plugin-n@15.7.0)(eslint-plugin-promise@6.1.1)(eslint@8.37.0)
       eslint-plugin-import: 2.27.5(@typescript-eslint/parser@5.57.1)(eslint-import-resolver-typescript@3.5.5)(eslint@8.37.0)
       eslint-plugin-n: 15.7.0(eslint@8.37.0)
       eslint-plugin-promise: 6.1.1(eslint@8.37.0)
-      typescript: 5.0.3
+      typescript: 5.0.4
     transitivePeerDependencies:
       - supports-color
     dev: true
@@ -2258,7 +2258,7 @@ packages:
       eslint-import-resolver-webpack:
         optional: true
     dependencies:
-      '@typescript-eslint/parser': 5.57.1(eslint@8.37.0)(typescript@5.0.3)
+      '@typescript-eslint/parser': 5.57.1(eslint@8.37.0)(typescript@5.0.4)
       debug: 3.2.7
       eslint: 8.37.0
       eslint-import-resolver-node: 0.3.7
@@ -2288,7 +2288,7 @@ packages:
       '@typescript-eslint/parser':
         optional: true
     dependencies:
-      '@typescript-eslint/parser': 5.57.1(eslint@8.37.0)(typescript@5.0.3)
+      '@typescript-eslint/parser': 5.57.1(eslint@8.37.0)(typescript@5.0.4)
       array-includes: 3.1.6
       array.prototype.flat: 1.3.1
       array.prototype.flatmap: 1.3.1
@@ -4752,7 +4752,7 @@ packages:
     engines: {node: '>= 0.8.0'}
     dev: true
 
-  /prettier-plugin-organize-imports@3.2.2(prettier@2.8.7)(typescript@5.0.3):
+  /prettier-plugin-organize-imports@3.2.2(prettier@2.8.7)(typescript@5.0.4):
     resolution: {integrity: sha512-e97lE6odGSiHonHJMTYC0q0iLXQyw0u5z/PJpvP/3vRy6/Zi9kLBwFAbEGjDzIowpjQv8b+J04PDamoUSQbzGA==}
     peerDependencies:
       '@volar/vue-language-plugin-pug': ^1.0.4
@@ -4766,7 +4766,7 @@ packages:
         optional: true
     dependencies:
       prettier: 2.8.7
-      typescript: 5.0.3
+      typescript: 5.0.4
     dev: true
 
   /prettier@2.8.7:
@@ -5669,7 +5669,7 @@ packages:
     engines: {node: '>=8'}
     dev: true
 
-  /ts-node@10.9.1(@types/node@18.15.11)(typescript@5.0.3):
+  /ts-node@10.9.1(@types/node@18.15.11)(typescript@5.0.4):
     resolution: {integrity: sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==}
     hasBin: true
     peerDependencies:
@@ -5695,23 +5695,23 @@ packages:
       create-require: 1.1.1
       diff: 4.0.2
       make-error: 1.3.6
-      typescript: 5.0.3
+      typescript: 5.0.4
       v8-compile-cache-lib: 3.0.1
       yn: 3.1.1
     dev: true
 
-  /ts-standard@12.0.2(eslint-import-resolver-typescript@3.5.5)(typescript@5.0.3):
+  /ts-standard@12.0.2(eslint-import-resolver-typescript@3.5.5)(typescript@5.0.4):
     resolution: {integrity: sha512-XX2wrB9fKKTfBj4yD3ABm9iShzZcS2iWcPK8XzlBvuL20+wMiLgiz/k5tXgZwTaYq5wRhbks1Y9PelhujF/9ag==}
     engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}
     hasBin: true
     peerDependencies:
       typescript: '*'
     dependencies:
-      '@typescript-eslint/eslint-plugin': 5.57.1(@typescript-eslint/parser@5.57.1)(eslint@8.37.0)(typescript@5.0.3)
-      '@typescript-eslint/parser': 5.57.1(eslint@8.37.0)(typescript@5.0.3)
+      '@typescript-eslint/eslint-plugin': 5.57.1(@typescript-eslint/parser@5.57.1)(eslint@8.37.0)(typescript@5.0.4)
+      '@typescript-eslint/parser': 5.57.1(eslint@8.37.0)(typescript@5.0.4)
       eslint: 8.37.0
       eslint-config-standard-jsx: 11.0.0(eslint-plugin-react@7.32.2)(eslint@8.37.0)
-      eslint-config-standard-with-typescript: 23.0.0(@typescript-eslint/eslint-plugin@5.57.1)(eslint-plugin-import@2.27.5)(eslint-plugin-n@15.7.0)(eslint-plugin-promise@6.1.1)(eslint@8.37.0)(typescript@5.0.3)
+      eslint-config-standard-with-typescript: 23.0.0(@typescript-eslint/eslint-plugin@5.57.1)(eslint-plugin-import@2.27.5)(eslint-plugin-n@15.7.0)(eslint-plugin-promise@6.1.1)(eslint@8.37.0)(typescript@5.0.4)
       eslint-plugin-import: 2.27.5(@typescript-eslint/parser@5.57.1)(eslint-import-resolver-typescript@3.5.5)(eslint@8.37.0)
       eslint-plugin-n: 15.7.0(eslint@8.37.0)
       eslint-plugin-promise: 6.1.1(eslint@8.37.0)
@@ -5719,7 +5719,7 @@ packages:
       minimist: 1.2.8
       pkg-conf: 4.0.0
       standard-engine: 15.0.0
-      typescript: 5.0.3
+      typescript: 5.0.4
     transitivePeerDependencies:
       - eslint-import-resolver-typescript
       - eslint-import-resolver-webpack
@@ -5743,14 +5743,14 @@ packages:
     resolution: {integrity: sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg==}
     dev: true
 
-  /tsutils@3.21.0(typescript@5.0.3):
+  /tsutils@3.21.0(typescript@5.0.4):
     resolution: {integrity: sha512-mHKK3iUXL+3UF6xL5k0PEhKRUBKPBCv/+RkEOpjRWxxx27KKRBmmA60A9pgOUvMi8GKhRMPEmjBRPzs2W7O1OA==}
     engines: {node: '>= 6'}
     peerDependencies:
       typescript: '>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta'
     dependencies:
       tslib: 1.14.1
-      typescript: 5.0.3
+      typescript: 5.0.4
     dev: true
 
   /type-check@0.3.2:
@@ -5831,7 +5831,7 @@ packages:
       is-typedarray: 1.0.0
     dev: true
 
-  /typedoc@0.23.28(typescript@5.0.3):
+  /typedoc@0.23.28(typescript@5.0.4):
     resolution: {integrity: sha512-9x1+hZWTHEQcGoP7qFmlo4unUoVJLB0H/8vfO/7wqTnZxg4kPuji9y3uRzEu0ZKez63OJAUmiGhUrtukC6Uj3w==}
     engines: {node: '>= 14.14'}
     hasBin: true
@@ -5842,11 +5842,11 @@ packages:
       marked: 4.3.0
       minimatch: 7.4.5
       shiki: 0.14.1
-      typescript: 5.0.3
+      typescript: 5.0.4
     dev: true
 
-  /typescript@5.0.3:
-    resolution: {integrity: sha512-xv8mOEDnigb/tN9PSMTwSEqAnUvkoXMQlicOb0IUVDBSQCgBSaAAROUZYy2IcUy5qU6XajK5jjjO7TMWqBTKZA==}
+  /typescript@5.0.4:
+    resolution: {integrity: sha512-cW9T5W9xY37cc+jfEnaUvX91foxtHkza3Nw3wkoF4sSlKn0MONdkdEndig/qPBWXNkmplh3NzayQzCiHM4/hqw==}
     engines: {node: '>=12.20'}
     hasBin: true
     dev: true
index 92ed93a11cf52c2fe27a8ca034a58c46d0755ca0..349af12c369e207713af37bfac8f18efb3c929ef 100644 (file)
@@ -1,7 +1,8 @@
 export { DynamicClusterPool } from './pools/cluster/dynamic'
 export { FixedClusterPool } from './pools/cluster/fixed'
 export type { ClusterPoolOptions } from './pools/cluster/fixed'
-export type { IPool, PoolEmitter, PoolOptions } from './pools/pool'
+export { PoolEvents } from './pools/pool'
+export type { IPool, PoolEmitter, PoolOptions, PoolEvent } from './pools/pool'
 export type {
   ErrorHandler,
   ExitHandler,
index 0d583217f9edb3ac079894bd5f8cbc05d0fb7e9c..9ac81ce56558ec76fbd66d7789b2399e910a2e69 100644 (file)
@@ -2,7 +2,7 @@ import crypto from 'node:crypto'
 import type { MessageValue, PromiseResponseWrapper } from '../utility-types'
 import { EMPTY_FUNCTION } from '../utils'
 import { KillBehaviors, isKillBehavior } from '../worker/worker-options'
-import type { PoolOptions } from './pool'
+import { PoolEvents, type PoolOptions } from './pool'
 import { PoolEmitter } from './pool'
 import type { IPoolInternal, TasksUsage, WorkerType } from './pool-internal'
 import { PoolType } from './pool-internal'
@@ -126,16 +126,18 @@ export abstract class AbstractPool<
   private checkPoolOptions (opts: PoolOptions<Worker>): void {
     this.opts.workerChoiceStrategy =
       opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN
-    if (
-      !Object.values(WorkerChoiceStrategies).includes(
-        this.opts.workerChoiceStrategy
-      )
-    ) {
+    this.checkValidWorkerChoiceStrategy(this.opts.workerChoiceStrategy)
+    this.opts.enableEvents = opts.enableEvents ?? true
+  }
+
+  private checkValidWorkerChoiceStrategy (
+    workerChoiceStrategy: WorkerChoiceStrategy
+  ): void {
+    if (!Object.values(WorkerChoiceStrategies).includes(workerChoiceStrategy)) {
       throw new Error(
-        `Invalid worker choice strategy '${this.opts.workerChoiceStrategy}'`
+        `Invalid worker choice strategy '${workerChoiceStrategy}'`
       )
     }
-    this.opts.enableEvents = opts.enableEvents ?? true
   }
 
   /** @inheritDoc */
@@ -162,6 +164,7 @@ export abstract class AbstractPool<
   public setWorkerChoiceStrategy (
     workerChoiceStrategy: WorkerChoiceStrategy
   ): void {
+    this.checkValidWorkerChoiceStrategy(workerChoiceStrategy)
     this.opts.workerChoiceStrategy = workerChoiceStrategy
     for (const [index, workerItem] of this.workers.entries()) {
       this.setWorker(index, workerItem.worker, {
@@ -273,7 +276,7 @@ export abstract class AbstractPool<
       ++workerTasksUsage.error
     }
     if (this.workerChoiceStrategyContext.getRequiredStatistics().runTime) {
-      workerTasksUsage.runTime += message.taskRunTime ?? 0
+      workerTasksUsage.runTime += message.runTime ?? 0
       if (
         this.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime &&
         workerTasksUsage.run !== 0
@@ -305,7 +308,7 @@ export abstract class AbstractPool<
           (message.kill != null &&
             this.getWorkerTasksUsage(createdWorker)?.running === 0)
         ) {
-          // Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime)
+          // Kill message received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime)
           void this.destroyWorker(createdWorker)
         }
       })
@@ -389,6 +392,7 @@ export abstract class AbstractPool<
   protected workerListener (): (message: MessageValue<Response>) => void {
     return message => {
       if (message.id != null) {
+        // Task response received
         const promiseResponse = this.promiseResponseMap.get(message.id)
         if (promiseResponse != null) {
           if (message.error != null) {
@@ -416,7 +420,7 @@ export abstract class AbstractPool<
 
   private checkAndEmitBusy (): void {
     if (this.opts.enableEvents === true && this.busy) {
-      this.emitter?.emit('busy')
+      this.emitter?.emit(PoolEvents.busy)
     }
   }
 
@@ -426,7 +430,7 @@ export abstract class AbstractPool<
       this.opts.enableEvents === true &&
       this.full
     ) {
-      this.emitter?.emit('full')
+      this.emitter?.emit(PoolEvents.full)
     }
   }
 
index 600cb4228b0b33da08a6cea5c13093ca790f1835..f1da5356a2e5310d799b6e595a898a3aad783600 100644 (file)
@@ -12,6 +12,19 @@ import type { WorkerChoiceStrategy } from './selection-strategies/selection-stra
  */
 export class PoolEmitter extends EventEmitter {}
 
+/**
+ * Enumeration of pool events.
+ */
+export const PoolEvents = Object.freeze({
+  full: 'full',
+  busy: 'busy'
+} as const)
+
+/**
+ * Pool event.
+ */
+export type PoolEvent = keyof typeof PoolEvents
+
 /**
  * Options for a poolifier pool.
  */
index 66aafad6bf89b0164d2fab5f89c5aca0a4bea0c5..826a89d9d6ce19c3f362f918785653c3056ba42e 100644 (file)
@@ -89,11 +89,10 @@ export class WorkerChoiceStrategyContext<
   public setWorkerChoiceStrategy (
     workerChoiceStrategy: WorkerChoiceStrategy
   ): void {
-    if (this.workerChoiceStrategyType === workerChoiceStrategy) {
-      this.workerChoiceStrategies.get(workerChoiceStrategy)?.reset()
-    } else {
+    if (this.workerChoiceStrategyType !== workerChoiceStrategy) {
       this.workerChoiceStrategyType = workerChoiceStrategy
     }
+    this.workerChoiceStrategies.get(this.workerChoiceStrategyType)?.reset()
   }
 
   /**
index ec5ff44d861baeff4b0779a83e19f2b869ee560b..05af4e3d396b7c89d22ba0c895ceb6c4649e5f42 100644 (file)
@@ -32,9 +32,9 @@ export interface MessageValue<
    */
   readonly error?: string
   /**
-   * Task runtime.
+   * Runtime.
    */
-  readonly taskRunTime?: number
+  readonly runTime?: number
   /**
    * Reference to main worker.
    *
index 475595eccf5f2e2c85b4eac8ba251f306f472c5d..10a0c82d0565de18529343f163b295688c22bc70 100644 (file)
@@ -26,7 +26,7 @@ export abstract class AbstractWorker<
    */
   protected lastTaskTimestamp!: number
   /**
-   * Handler Id of the `aliveInterval` worker alive check.
+   * Handler id of the `aliveInterval` worker alive check.
    */
   protected readonly aliveInterval?: NodeJS.Timeout
   /**
@@ -45,7 +45,7 @@ export abstract class AbstractWorker<
     protected mainWorker: MainWorker | undefined | null,
     protected readonly opts: WorkerOptions = {
       /**
-       * The kill behavior option on this Worker or its default value.
+       * The kill behavior option on this worker or its default value.
        */
       killBehavior: DEFAULT_KILL_BEHAVIOR,
       /**
@@ -67,28 +67,36 @@ export abstract class AbstractWorker<
       this.checkAlive.bind(this)()
     }
 
-    this.mainWorker?.on('message', (value: MessageValue<Data, MainWorker>) => {
-      this.messageListener(value, fn)
-    })
+    this.mainWorker?.on(
+      'message',
+      (message: MessageValue<Data, MainWorker>) => {
+        this.messageListener(message, fn)
+      }
+    )
   }
 
+  /**
+   * Worker message listener.
+   *
+   * @param message - Message received.
+   * @param fn - Function processed by the worker when the pool's `execution` function is invoked.
+   */
   protected messageListener (
-    value: MessageValue<Data, MainWorker>,
+    message: MessageValue<Data, MainWorker>,
     fn: (data: Data) => Response
   ): void {
-    if (value.data != null && value.id != null) {
-      // Here you will receive messages
+    if (message.data != null && message.id != null) {
+      // Task message received
       if (this.opts.async === true) {
-        this.runInAsyncScope(this.runAsync.bind(this), this, fn, value)
+        this.runInAsyncScope(this.runAsync.bind(this), this, fn, message)
       } else {
-        this.runInAsyncScope(this.run.bind(this), this, fn, value)
+        this.runInAsyncScope(this.run.bind(this), this, fn, message)
       }
-    } else if (value.parent != null) {
-      // Save a reference of the main worker to communicate with it
-      // This will be received once
-      this.mainWorker = value.parent
-    } else if (value.kill != null) {
-      // Here is time to kill this worker, just clearing the interval
+    } else if (message.parent != null) {
+      // Main worker reference message received
+      this.mainWorker = message.parent
+    } else if (message.kill != null) {
+      // Kill message received
       this.aliveInterval != null && clearInterval(this.aliveInterval)
       this.emitDestroy()
     }
@@ -158,20 +166,20 @@ export abstract class AbstractWorker<
    * Runs the given function synchronously.
    *
    * @param fn - Function that will be executed.
-   * @param value - Input data for the given function.
+   * @param message - Input data for the given function.
    */
   protected run (
     fn: (data?: Data) => Response,
-    value: MessageValue<Data>
+    message: MessageValue<Data>
   ): void {
     try {
-      const startTaskTimestamp = Date.now()
-      const res = fn(value.data)
-      const taskRunTime = Date.now() - startTaskTimestamp
-      this.sendToMainWorker({ data: res, id: value.id, taskRunTime })
+      const startTimestamp = Date.now()
+      const res = fn(message.data)
+      const runTime = Date.now() - startTimestamp
+      this.sendToMainWorker({ data: res, id: message.id, runTime })
     } catch (e) {
       const err = this.handleError(e as Error)
-      this.sendToMainWorker({ error: err, id: value.id })
+      this.sendToMainWorker({ error: err, id: message.id })
     } finally {
       !this.isMain && (this.lastTaskTimestamp = Date.now())
     }
@@ -181,22 +189,22 @@ export abstract class AbstractWorker<
    * Runs the given function asynchronously.
    *
    * @param fn - Function that will be executed.
-   * @param value - Input data for the given function.
+   * @param message - Input data for the given function.
    */
   protected runAsync (
     fn: (data?: Data) => Promise<Response>,
-    value: MessageValue<Data>
+    message: MessageValue<Data>
   ): void {
-    const startTaskTimestamp = Date.now()
-    fn(value.data)
+    const startTimestamp = Date.now()
+    fn(message.data)
       .then(res => {
-        const taskRunTime = Date.now() - startTaskTimestamp
-        this.sendToMainWorker({ data: res, id: value.id, taskRunTime })
+        const runTime = Date.now() - startTimestamp
+        this.sendToMainWorker({ data: res, id: message.id, runTime })
         return null
       })
       .catch(e => {
         const err = this.handleError(e as Error)
-        this.sendToMainWorker({ error: err, id: value.id })
+        this.sendToMainWorker({ error: err, id: message.id })
       })
       .finally(() => {
         !this.isMain && (this.lastTaskTimestamp = Date.now())
index d9b5695e53f369bd45f889a28c4a4846d6ab351f..658be6a9ef99ed63afb692ba864a4745ebf66f89 100644 (file)
@@ -1,8 +1,9 @@
 const { expect } = require('expect')
 const {
-  FixedClusterPool,
   DynamicThreadPool,
+  FixedClusterPool,
   FixedThreadPool,
+  PoolEvents,
   WorkerChoiceStrategies
 } = require('../../../lib/index')
 
@@ -218,7 +219,7 @@ describe('Abstract pool test suite', () => {
     )
     const promises = []
     let poolFull = 0
-    pool.emitter.on('full', () => ++poolFull)
+    pool.emitter.on(PoolEvents.full, () => ++poolFull)
     for (let i = 0; i < numberOfWorkers * 2; i++) {
       promises.push(pool.execute())
     }
@@ -236,7 +237,7 @@ describe('Abstract pool test suite', () => {
     )
     const promises = []
     let poolBusy = 0
-    pool.emitter.on('busy', () => ++poolBusy)
+    pool.emitter.on(PoolEvents.busy, () => ++poolBusy)
     for (let i = 0; i < numberOfWorkers * 2; i++) {
       promises.push(pool.execute())
     }
index 6dd9f506fd78b8e606ed7d3d99906a2cc64465e0..74cb5d8ff646d78c121793a665d06409ea181ce5 100644 (file)
@@ -1,5 +1,5 @@
 const { expect } = require('expect')
-const { DynamicClusterPool } = require('../../../lib/index')
+const { DynamicClusterPool, PoolEvents } = require('../../../lib/index')
 const { WorkerFunctions } = require('../../test-types')
 const TestUtils = require('../../test-utils')
 
@@ -28,7 +28,7 @@ describe('Dynamic cluster pool test suite', () => {
 
   it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => {
     let poolBusy = 0
-    pool.emitter.on('busy', () => ++poolBusy)
+    pool.emitter.on(PoolEvents.busy, () => ++poolBusy)
     for (let i = 0; i < max * 2; i++) {
       pool.execute()
     }
index ae899625dd9341dc4f9cfad3f40fab109587eb27..908acbfe84530f59d3facb59cfec315df7333cd1 100644 (file)
@@ -1,5 +1,5 @@
 const { expect } = require('expect')
-const { FixedClusterPool } = require('../../../lib/index')
+const { FixedClusterPool, PoolEvents } = require('../../../lib/index')
 const { WorkerFunctions } = require('../../test-types')
 const TestUtils = require('../../test-utils')
 
@@ -65,9 +65,9 @@ describe('Fixed cluster pool test suite', () => {
     expect(result).toBe(false)
   })
 
-  it('Verify that busy event is emitted', async () => {
+  it("Verify that 'busy' event is emitted", async () => {
     let poolBusy = 0
-    pool.emitter.on('busy', () => ++poolBusy)
+    pool.emitter.on(PoolEvents.busy, () => ++poolBusy)
     for (let i = 0; i < numberOfWorkers * 2; i++) {
       pool.execute()
     }
index d8c68e90c51100886bc8fe83ebfba8ec2ef094f1..9f69860aa408e42c444434dadcaf6f6ca7e1bfe5 100644 (file)
@@ -1,5 +1,5 @@
 const { expect } = require('expect')
-const { DynamicThreadPool } = require('../../../lib/index')
+const { DynamicThreadPool, PoolEvents } = require('../../../lib/index')
 const { WorkerFunctions } = require('../../test-types')
 const TestUtils = require('../../test-utils')
 
@@ -28,7 +28,7 @@ describe('Dynamic thread pool test suite', () => {
 
   it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => {
     let poolBusy = 0
-    pool.emitter.on('busy', () => ++poolBusy)
+    pool.emitter.on(PoolEvents.busy, () => ++poolBusy)
     for (let i = 0; i < max * 2; i++) {
       pool.execute()
     }
index fd9ce5dfe799a7f8e707bcdb843f875dfe0fbd5a..a42b5d37ede902bd5c6a490ce7ff832e6025d1fc 100644 (file)
@@ -1,5 +1,5 @@
 const { expect } = require('expect')
-const { FixedThreadPool } = require('../../../lib/index')
+const { FixedThreadPool, PoolEvents } = require('../../../lib/index')
 const { WorkerFunctions } = require('../../test-types')
 const TestUtils = require('../../test-utils')
 
@@ -65,9 +65,9 @@ describe('Fixed thread pool test suite', () => {
     expect(result).toBe(false)
   })
 
-  it('Verify that busy event is emitted', async () => {
+  it("Verify that 'busy' event is emitted", async () => {
     let poolBusy = 0
-    pool.emitter.on('busy', () => ++poolBusy)
+    pool.emitter.on(PoolEvents.busy, () => ++poolBusy)
     for (let i = 0; i < numberOfThreads * 2; i++) {
       pool.execute()
     }