@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/README.md CHANGED
@@ -442,7 +442,7 @@ export class ExtensionContext {
442
442
 
443
443
  ## Behavioral decorators
444
444
 
445
- The library includes two behavioral decorators that influence how classes are registered in the container.
445
+ The library includes three behavioral decorators that influence how classes are registered in the container.
446
446
  These decorators attach metadata to the class type, which is then interpreted by the container during registration.
447
447
 
448
448
  ### `@Scoped`
@@ -503,6 +503,10 @@ export class ExtensionContext {
503
503
  container.register(ExtensionContext);
504
504
  ```
505
505
 
506
+ > [!WARNING]
507
+ > Eager instantiation requires that all dependencies of the class are already registered in the container.
508
+ > If they are not, registration will fail.
509
+
506
510
  ## Testing support
507
511
 
508
512
  Testing is an important part of software development, and dependency injection is meant to make it easier.
@@ -792,6 +792,24 @@ interface Injector {
792
792
  */
793
793
  declare const Injector: Type<Injector>;
794
794
 
795
+ /**
796
+ * Registers a mapping between a generated (e.g., decorated or proxied) constructor
797
+ * and its original, underlying constructor.
798
+ *
799
+ * This allows libraries or consumers that manipulate constructors, such as through
800
+ * class decorators, to inform the DI system about the real "identity" of a class.
801
+ *
802
+ * @param transformedClass The constructor function returned by a class decorator or factory.
803
+ * @param originalClass The original constructor function.
804
+ *
805
+ * @remarks
806
+ * This API affects the core class identity resolution mechanism of the DI system.
807
+ * Incorrect usage may cause metadata to be misassociated, leading to subtle errors.
808
+ * Use only when manipulating constructors (e.g., via decorators or proxies),
809
+ * and ensure the mapping is correct.
810
+ */
811
+ declare function setClassIdentityMapping<T extends object>(transformedClass: Constructor<T>, originalClass: Constructor<T>): void;
812
+
795
813
  /**
796
814
  * Composer API for middleware functions.
797
815
  */
@@ -847,5 +865,5 @@ interface Middleware {
847
865
  */
848
866
  declare function applyMiddleware(container: Container, middlewares: Middleware[]): Container;
849
867
 
850
- export { AutoRegister, EagerInstantiate, Inject, InjectAll, Injectable, Injector, Optional, OptionalAll, Scope, Scoped, applyMiddleware, build, createContainer, createType, forwardRef, inject, injectAll, injectBy };
868
+ export { AutoRegister, EagerInstantiate, Inject, InjectAll, Injectable, Injector, Optional, OptionalAll, Scope, Scoped, applyMiddleware, build, createContainer, createType, forwardRef, inject, injectAll, injectBy, setClassIdentityMapping };
851
869
  export type { ClassProvider, Constructor, Container, ContainerOptions, ExistingProvider, FactoryProvider, Middleware, MiddlewareComposer, Provider, RegistrationOptions, Token, TokenRef, Tokens, TokensRef, Type, ValueProvider };
package/dist/cjs/index.js CHANGED
@@ -156,16 +156,35 @@ function injectAll(token) {
156
156
  return context.container.resolveAll(token);
157
157
  }
158
158
 
159
+ /**
160
+ * Registers a mapping between a generated (e.g., decorated or proxied) constructor
161
+ * and its original, underlying constructor.
162
+ *
163
+ * This allows libraries or consumers that manipulate constructors, such as through
164
+ * class decorators, to inform the DI system about the real "identity" of a class.
165
+ *
166
+ * @param transformedClass The constructor function returned by a class decorator or factory.
167
+ * @param originalClass The original constructor function.
168
+ *
169
+ * @remarks
170
+ * This API affects the core class identity resolution mechanism of the DI system.
171
+ * Incorrect usage may cause metadata to be misassociated, leading to subtle errors.
172
+ * Use only when manipulating constructors (e.g., via decorators or proxies),
173
+ * and ensure the mapping is correct.
174
+ */ function setClassIdentityMapping(transformedClass, originalClass) {
175
+ classIdentityMap.set(transformedClass, originalClass);
176
+ }
159
177
  // @internal
160
178
  function getMetadata(Class) {
161
- let metadata = metadataMap.get(Class);
179
+ const originalClass = classIdentityMap.get(Class) ?? Class;
180
+ let metadata = metadataMap.get(originalClass);
162
181
  if (!metadata) {
163
- metadataMap.set(Class, metadata = {
182
+ metadataMap.set(originalClass, metadata = {
164
183
  tokensRef: {
165
184
  getRefTokens: ()=>new Set()
166
185
  },
167
186
  provider: {
168
- useClass: Class
187
+ useClass: originalClass
169
188
  },
170
189
  dependencies: {
171
190
  constructor: [],
@@ -175,6 +194,7 @@ function getMetadata(Class) {
175
194
  }
176
195
  return metadata;
177
196
  }
197
+ const classIdentityMap = new WeakMap();
178
198
  const metadataMap = new WeakMap();
179
199
 
180
200
  function optional(token) {
@@ -641,8 +661,16 @@ function isDisposable(value) {
641
661
  instantiateClass(Class, optional) {
642
662
  const metadata = getMetadata(Class);
643
663
  if (metadata.autoRegister ?? this.myOptions.autoRegister) {
644
- this.register(Class);
645
- return this.resolve(Class);
664
+ // Temporarily set eagerInstantiate to false to avoid resolving the class two times:
665
+ // one inside register(), and the other just below
666
+ const eagerInstantiate = metadata.eagerInstantiate;
667
+ metadata.eagerInstantiate = false;
668
+ try {
669
+ this.register(Class);
670
+ return this.resolve(Class);
671
+ } finally{
672
+ metadata.eagerInstantiate = eagerInstantiate;
673
+ }
646
674
  }
647
675
  const scope = this.resolveScope(metadata.scope);
648
676
  if (optional && scope === Scope.Container) {
@@ -1112,4 +1140,5 @@ exports.forwardRef = forwardRef;
1112
1140
  exports.inject = inject;
1113
1141
  exports.injectAll = injectAll;
1114
1142
  exports.injectBy = injectBy;
1143
+ exports.setClassIdentityMapping = setClassIdentityMapping;
1115
1144
  //# sourceMappingURL=index.js.map