@lppedd/di-wise-neo 0.4.0 → 0.5.0

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`);
@@ -823,7 +832,7 @@ function isDisposable(value) {
823
832
  *
824
833
  * @example
825
834
  * ```ts
826
- * @AutoRegister()
835
+ * @AutoRegister
827
836
  * class Wizard {}
828
837
  *
829
838
  * const wizard = container.resolve(Wizard);
@@ -831,11 +840,32 @@ function isDisposable(value) {
831
840
  * ```
832
841
  *
833
842
  * @__NO_SIDE_EFFECTS__
834
- */ function AutoRegister(enable = true) {
835
- return function(Class) {
836
- const metadata = getMetadata(Class);
837
- metadata.autoRegister = enable;
838
- };
843
+ */ function AutoRegister(Class) {
844
+ const metadata = getMetadata(Class);
845
+ metadata.autoRegister = true;
846
+ }
847
+
848
+ /**
849
+ * Class decorator that enables eager instantiation of a class when it is registered
850
+ * in the container with `Scope.Container`.
851
+ *
852
+ * This causes the container to immediately create and cache the instance of the class,
853
+ * instead of deferring instantiation until the first resolution.
854
+ *
855
+ * @example
856
+ * ```ts
857
+ * @EagerInstantiate
858
+ * @Scoped(Scope.Container)
859
+ * class Wizard {}
860
+ *
861
+ * // A Wizard instance is immediately created and cached by the container
862
+ * const wizard = container.register(Wizard);
863
+ * ```
864
+ *
865
+ * @__NO_SIDE_EFFECTS__
866
+ */ function EagerInstantiate(Class) {
867
+ const metadata = getMetadata(Class);
868
+ metadata.eagerInstantiate = true;
839
869
  }
840
870
 
841
871
  function forwardRef(token) {
@@ -1062,5 +1092,5 @@ function OptionalAll(token) {
1062
1092
  return container;
1063
1093
  }
1064
1094
 
1065
- export { AutoRegister, Inject, InjectAll, Injectable, Injector, Optional, OptionalAll, Scope, Scoped, applyMiddleware, build, createContainer, createType, forwardRef, inject, injectAll, injectBy };
1095
+ export { AutoRegister, EagerInstantiate, Inject, InjectAll, Injectable, Injector, Optional, OptionalAll, Scope, Scoped, applyMiddleware, build, createContainer, createType, forwardRef, inject, injectAll, injectBy };
1066
1096
  //# sourceMappingURL=index.mjs.map