@tsed/config 8.9.1 → 8.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@tsed/config",
3
3
  "description": "Configuration management for Ts.ED",
4
4
  "type": "module",
5
- "version": "8.9.1",
5
+ "version": "8.9.3",
6
6
  "keywords": [
7
7
  "Ts.ED",
8
8
  "Configuration",
@@ -61,11 +61,11 @@
61
61
  "tslib": "2.7.0"
62
62
  },
63
63
  "devDependencies": {
64
- "@tsed/ajv": "8.9.1",
65
- "@tsed/barrels": "8.9.1",
66
- "@tsed/core": "8.9.1",
67
- "@tsed/di": "8.9.1",
68
- "@tsed/typescript": "8.9.1",
64
+ "@tsed/ajv": "8.9.3",
65
+ "@tsed/barrels": "8.9.3",
66
+ "@tsed/core": "8.9.3",
67
+ "@tsed/di": "8.9.3",
68
+ "@tsed/typescript": "8.9.3",
69
69
  "@types/js-yaml": "4.0.9",
70
70
  "dotenv": "^16.5.0",
71
71
  "dotenv-expand": "^12.0.2",
@@ -1,7 +1,7 @@
1
1
  import "@tsed/ajv";
2
2
 
3
3
  import {configuration, constant, inject, injectable, logger} from "@tsed/di";
4
- import {$on} from "@tsed/hooks";
4
+ import {$off, $on, $once} from "@tsed/hooks";
5
5
 
6
6
  import {CONFIG_SOURCES} from "../constants/constants.js";
7
7
  import type {ConfigurationExtends} from "../interfaces/ConfigSource.js";
@@ -13,6 +13,8 @@ export async function afterResolveConfiguration() {
13
13
 
14
14
  for (const source of sources.values()) {
15
15
  const {instance, watch, refreshOn, name, validationSchema} = source;
16
+ const seed = Math.floor(Math.random() * 10000);
17
+ const ref = name + "-" + seed;
16
18
 
17
19
  if (instance?.$onInit) {
18
20
  await instance.$onInit();
@@ -41,19 +43,17 @@ export async function afterResolveConfiguration() {
41
43
  if (watch && instance?.watch) {
42
44
  const closer = await instance.watch(refresh);
43
45
 
44
- closer && $on("$onDestroy", closer);
46
+ closer && $once("$onDestroy", ref, closer);
45
47
  }
46
48
 
47
49
  // manager refresh on request
48
50
  if (refreshOn === "request") {
49
- $on("$onRequest", () => {
50
- return refresh();
51
- });
51
+ $on("$onRequest", ref, refresh);
52
52
  }
53
53
 
54
54
  // manager refresh on response
55
55
  if (refreshOn === "response") {
56
- $on("$onResponse", () => {
56
+ const onResponse = () => {
57
57
  refresh().catch((error) => {
58
58
  logger().error({
59
59
  event: "CONFIG_SOURCE_REFRESH_ERROR",
@@ -63,8 +63,19 @@ export async function afterResolveConfiguration() {
63
63
  error_stack: error.stack
64
64
  });
65
65
  });
66
- });
66
+ };
67
+
68
+ $on("$onResponse", ref, onResponse);
67
69
  }
70
+
71
+ $once("$onDestroy", ref, () => {
72
+ if (instance?.$onDestroy) {
73
+ // clean all event related to the same ref
74
+ $off(ref);
75
+
76
+ return instance.$onDestroy();
77
+ }
78
+ });
68
79
  }
69
80
 
70
81
  const configs = Object.fromEntries(
@@ -77,12 +88,3 @@ export async function afterResolveConfiguration() {
77
88
  }
78
89
 
79
90
  $on("$afterResolveConfiguration", afterResolveConfiguration);
80
- $on("$onDestroy", () => {
81
- const configs = inject<CONFIG_SOURCES>(CONFIG_SOURCES);
82
-
83
- const promises = Object.values(configs).map((config) => {
84
- return config.$onDestroy?.();
85
- });
86
-
87
- return Promise.all(promises);
88
- });