@lppedd/di-wise-neo 0.4.0 → 0.5.1

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
@@ -494,15 +494,16 @@ function isDisposable(value) {
494
494
  if (args.length == 1) {
495
495
  const Class = args[0];
496
496
  const metadata = getMetadata(Class);
497
- // Register the class itself
498
- this.myTokenRegistry.set(Class, {
497
+ const registration = {
499
498
  // The provider is of type ClassProvider, initialized by getMetadata
500
499
  provider: metadata.provider,
501
500
  options: {
502
- scope: metadata.scope
501
+ scope: metadata.scope ?? this.myOptions.defaultScope
503
502
  },
504
503
  dependencies: metadata.dependencies
505
- });
504
+ };
505
+ // Register the class itself
506
+ this.myTokenRegistry.set(Class, registration);
506
507
  // Register the additional tokens specified via class decorators.
507
508
  // These tokens will point to the original Class token and will have the same scope.
508
509
  for (const token of metadata.tokensRef.getRefTokens()){
@@ -512,21 +513,29 @@ function isDisposable(value) {
512
513
  }
513
514
  });
514
515
  }
516
+ // Eager-instantiate only if the class is container-scoped
517
+ if (metadata.eagerInstantiate && registration.options?.scope === Scope.Container) {
518
+ this.resolve(Class);
519
+ }
515
520
  } else {
516
521
  const [token, provider, options] = args;
517
522
  if (isClassProvider(provider)) {
518
- const Class = provider.useClass;
519
- const metadata = getMetadata(Class);
520
- this.myTokenRegistry.set(token, {
523
+ const metadata = getMetadata(provider.useClass);
524
+ const registration = {
521
525
  provider: metadata.provider,
522
526
  options: {
523
527
  // The explicit registration options override what is specified
524
528
  // via class decorators (e.g., @Scoped)
525
- scope: metadata.scope,
529
+ scope: metadata.scope ?? this.myOptions.defaultScope,
526
530
  ...options
527
531
  },
528
532
  dependencies: metadata.dependencies
529
- });
533
+ };
534
+ this.myTokenRegistry.set(token, registration);
535
+ // Eager-instantiate only if the provided class is container-scoped
536
+ if (metadata.eagerInstantiate && registration.options?.scope === Scope.Container) {
537
+ this.resolve(token);
538
+ }
530
539
  } else {
531
540
  if (isExistingProvider(provider)) {
532
541
  assert(token !== provider.useExisting, `the useExisting token ${token.name} cannot be the same as the token being registered`);
@@ -630,8 +639,16 @@ function isDisposable(value) {
630
639
  instantiateClass(Class, optional) {
631
640
  const metadata = getMetadata(Class);
632
641
  if (metadata.autoRegister ?? this.myOptions.autoRegister) {
633
- this.register(Class);
634
- return this.resolve(Class);
642
+ // Temporarily set eagerInstantiate to false to avoid resolving the class two times:
643
+ // one inside register(), and the other just below
644
+ const eagerInstantiate = metadata.eagerInstantiate;
645
+ metadata.eagerInstantiate = false;
646
+ try {
647
+ this.register(Class);
648
+ return this.resolve(Class);
649
+ } finally{
650
+ metadata.eagerInstantiate = eagerInstantiate;
651
+ }
635
652
  }
636
653
  const scope = this.resolveScope(metadata.scope);
637
654
  if (optional && scope === Scope.Container) {
@@ -823,7 +840,7 @@ function isDisposable(value) {
823
840
  *
824
841
  * @example
825
842
  * ```ts
826
- * @AutoRegister()
843
+ * @AutoRegister
827
844
  * class Wizard {}
828
845
  *
829
846
  * const wizard = container.resolve(Wizard);
@@ -831,11 +848,32 @@ function isDisposable(value) {
831
848
  * ```
832
849
  *
833
850
  * @__NO_SIDE_EFFECTS__
834
- */ function AutoRegister(enable = true) {
835
- return function(Class) {
836
- const metadata = getMetadata(Class);
837
- metadata.autoRegister = enable;
838
- };
851
+ */ function AutoRegister(Class) {
852
+ const metadata = getMetadata(Class);
853
+ metadata.autoRegister = true;
854
+ }
855
+
856
+ /**
857
+ * Class decorator that enables eager instantiation of a class when it is registered
858
+ * in the container with `Scope.Container`.
859
+ *
860
+ * This causes the container to immediately create and cache the instance of the class,
861
+ * instead of deferring instantiation until the first resolution.
862
+ *
863
+ * @example
864
+ * ```ts
865
+ * @EagerInstantiate
866
+ * @Scoped(Scope.Container)
867
+ * class Wizard {}
868
+ *
869
+ * // A Wizard instance is immediately created and cached by the container
870
+ * const wizard = container.register(Wizard);
871
+ * ```
872
+ *
873
+ * @__NO_SIDE_EFFECTS__
874
+ */ function EagerInstantiate(Class) {
875
+ const metadata = getMetadata(Class);
876
+ metadata.eagerInstantiate = true;
839
877
  }
840
878
 
841
879
  function forwardRef(token) {
@@ -1062,5 +1100,5 @@ function OptionalAll(token) {
1062
1100
  return container;
1063
1101
  }
1064
1102
 
1065
- export { AutoRegister, Inject, InjectAll, Injectable, Injector, Optional, OptionalAll, Scope, Scoped, applyMiddleware, build, createContainer, createType, forwardRef, inject, injectAll, injectBy };
1103
+ export { AutoRegister, EagerInstantiate, Inject, InjectAll, Injectable, Injector, Optional, OptionalAll, Scope, Scoped, applyMiddleware, build, createContainer, createType, forwardRef, inject, injectAll, injectBy };
1066
1104
  //# sourceMappingURL=index.mjs.map