@lppedd/di-wise-neo 0.5.0 → 0.5.2

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/dist/es/index.mjs CHANGED
@@ -154,16 +154,35 @@ function injectAll(token) {
154
154
  return context.container.resolveAll(token);
155
155
  }
156
156
 
157
+ /**
158
+ * Registers a mapping between a generated (e.g., decorated or proxied) constructor
159
+ * and its original, underlying constructor.
160
+ *
161
+ * This allows libraries or consumers that manipulate constructors, such as through
162
+ * class decorators, to inform the DI system about the real "identity" of a class.
163
+ *
164
+ * @param transformedClass The constructor function returned by a class decorator or factory.
165
+ * @param originalClass The original constructor function.
166
+ *
167
+ * @remarks
168
+ * This API affects the core class identity resolution mechanism of the DI system.
169
+ * Incorrect usage may cause metadata to be misassociated, leading to subtle errors.
170
+ * Use only when manipulating constructors (e.g., via decorators or proxies),
171
+ * and ensure the mapping is correct.
172
+ */ function setClassIdentityMapping(transformedClass, originalClass) {
173
+ classIdentityMap.set(transformedClass, originalClass);
174
+ }
157
175
  // @internal
158
176
  function getMetadata(Class) {
159
- let metadata = metadataMap.get(Class);
177
+ const originalClass = classIdentityMap.get(Class) ?? Class;
178
+ let metadata = metadataMap.get(originalClass);
160
179
  if (!metadata) {
161
- metadataMap.set(Class, metadata = {
180
+ metadataMap.set(originalClass, metadata = {
162
181
  tokensRef: {
163
182
  getRefTokens: ()=>new Set()
164
183
  },
165
184
  provider: {
166
- useClass: Class
185
+ useClass: originalClass
167
186
  },
168
187
  dependencies: {
169
188
  constructor: [],
@@ -173,6 +192,7 @@ function getMetadata(Class) {
173
192
  }
174
193
  return metadata;
175
194
  }
195
+ const classIdentityMap = new WeakMap();
176
196
  const metadataMap = new WeakMap();
177
197
 
178
198
  function optional(token) {
@@ -639,8 +659,16 @@ function isDisposable(value) {
639
659
  instantiateClass(Class, optional) {
640
660
  const metadata = getMetadata(Class);
641
661
  if (metadata.autoRegister ?? this.myOptions.autoRegister) {
642
- this.register(Class);
643
- return this.resolve(Class);
662
+ // Temporarily set eagerInstantiate to false to avoid resolving the class two times:
663
+ // one inside register(), and the other just below
664
+ const eagerInstantiate = metadata.eagerInstantiate;
665
+ metadata.eagerInstantiate = false;
666
+ try {
667
+ this.register(Class);
668
+ return this.resolve(Class);
669
+ } finally{
670
+ metadata.eagerInstantiate = eagerInstantiate;
671
+ }
644
672
  }
645
673
  const scope = this.resolveScope(metadata.scope);
646
674
  if (optional && scope === Scope.Container) {
@@ -1092,5 +1120,5 @@ function OptionalAll(token) {
1092
1120
  return container;
1093
1121
  }
1094
1122
 
1095
- export { AutoRegister, EagerInstantiate, Inject, InjectAll, Injectable, Injector, Optional, OptionalAll, Scope, Scoped, applyMiddleware, build, createContainer, createType, forwardRef, inject, injectAll, injectBy };
1123
+ export { AutoRegister, EagerInstantiate, Inject, InjectAll, Injectable, Injector, Optional, OptionalAll, Scope, Scoped, applyMiddleware, build, createContainer, createType, forwardRef, inject, injectAll, injectBy, setClassIdentityMapping };
1096
1124
  //# sourceMappingURL=index.mjs.map