@lppedd/di-wise-neo 0.5.3 → 0.6.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.
@@ -422,7 +422,7 @@ declare function createContainer(options?: Partial<ContainerOptions>): Container
422
422
  *
423
423
  * @example
424
424
  * ```ts
425
- * @AutoRegister
425
+ * @AutoRegister()
426
426
  * class Wizard {}
427
427
  *
428
428
  * const wizard = container.resolve(Wizard);
@@ -431,28 +431,28 @@ declare function createContainer(options?: Partial<ContainerOptions>): Container
431
431
  *
432
432
  * @__NO_SIDE_EFFECTS__
433
433
  */
434
- declare function AutoRegister<Ctor extends Constructor<any>>(Class: Ctor): void;
434
+ declare function AutoRegister(): ClassDecorator;
435
435
 
436
436
  /**
437
- * Class decorator that enables eager instantiation of a class when it is registered
438
- * in the container with `Scope.Container`.
437
+ * Class decorator that sets the class scope to **Container** and enables
438
+ * eager instantiation when the class is registered in the container.
439
439
  *
440
440
  * This causes the container to immediately create and cache the instance of the class,
441
441
  * instead of deferring instantiation until the first resolution.
442
442
  *
443
443
  * @example
444
444
  * ```ts
445
- * @EagerInstantiate
446
- * @Scoped(Scope.Container)
445
+ * @EagerInstantiate()
447
446
  * class Wizard {}
448
447
  *
449
- * // A Wizard instance is immediately created and cached by the container
448
+ * // Wizard is registered with Container scope, and an instance
449
+ * // is immediately created and cached by the container
450
450
  * const wizard = container.register(Wizard);
451
451
  * ```
452
452
  *
453
453
  * @__NO_SIDE_EFFECTS__
454
454
  */
455
- declare function EagerInstantiate<Ctor extends Constructor<any>>(Class: Ctor): void;
455
+ declare function EagerInstantiate(): ClassDecorator;
456
456
 
457
457
  interface TokensRef<Value = any> {
458
458
  readonly getRefTokens: () => Set<Token<Value>>;
package/dist/es/index.mjs CHANGED
@@ -534,7 +534,7 @@ function isDisposable(value) {
534
534
  // The provider is of type ClassProvider, initialized by getMetadata
535
535
  provider: metadata.provider,
536
536
  options: {
537
- scope: metadata.scope ?? this.myOptions.defaultScope
537
+ scope: metadata.scope?.value ?? this.myOptions.defaultScope
538
538
  },
539
539
  dependencies: metadata.dependencies
540
540
  };
@@ -551,7 +551,7 @@ function isDisposable(value) {
551
551
  }
552
552
  // Eager-instantiate only if the class is container-scoped
553
553
  if (metadata.eagerInstantiate && registration.options?.scope === Scope.Container) {
554
- this.resolve(Class);
554
+ this.resolveProviderValue(registration, registration.provider);
555
555
  }
556
556
  } else {
557
557
  const [token, provider, options] = args;
@@ -562,7 +562,7 @@ function isDisposable(value) {
562
562
  options: {
563
563
  // The explicit registration options override what is specified
564
564
  // via class decorators (e.g., @Scoped)
565
- scope: metadata.scope ?? this.myOptions.defaultScope,
565
+ scope: metadata.scope?.value ?? this.myOptions.defaultScope,
566
566
  ...options
567
567
  },
568
568
  dependencies: metadata.dependencies
@@ -570,7 +570,7 @@ function isDisposable(value) {
570
570
  this.myTokenRegistry.set(token, registration);
571
571
  // Eager-instantiate only if the provided class is container-scoped
572
572
  if (metadata.eagerInstantiate && registration.options?.scope === Scope.Container) {
573
- this.resolve(token);
573
+ this.resolveProviderValue(registration, registration.provider);
574
574
  }
575
575
  } else {
576
576
  if (isExistingProvider(provider)) {
@@ -686,7 +686,7 @@ function isDisposable(value) {
686
686
  metadata.eagerInstantiate = eagerInstantiate;
687
687
  }
688
688
  }
689
- const scope = this.resolveScope(metadata.scope);
689
+ const scope = this.resolveScope(metadata.scope?.value);
690
690
  if (optional && scope === Scope.Container) {
691
691
  // It would not be possible to resolve the class in container scope,
692
692
  // as that would require prior registration.
@@ -876,7 +876,7 @@ function isDisposable(value) {
876
876
  *
877
877
  * @example
878
878
  * ```ts
879
- * @AutoRegister
879
+ * @AutoRegister()
880
880
  * class Wizard {}
881
881
  *
882
882
  * const wizard = container.resolve(Wizard);
@@ -884,32 +884,45 @@ function isDisposable(value) {
884
884
  * ```
885
885
  *
886
886
  * @__NO_SIDE_EFFECTS__
887
- */ function AutoRegister(Class) {
888
- const metadata = getMetadata(Class);
889
- metadata.autoRegister = true;
887
+ */ function AutoRegister() {
888
+ return function(Class) {
889
+ const metadata = getMetadata(Class);
890
+ metadata.autoRegister = true;
891
+ };
890
892
  }
891
893
 
892
894
  /**
893
- * Class decorator that enables eager instantiation of a class when it is registered
894
- * in the container with `Scope.Container`.
895
+ * Class decorator that sets the class scope to **Container** and enables
896
+ * eager instantiation when the class is registered in the container.
895
897
  *
896
898
  * This causes the container to immediately create and cache the instance of the class,
897
899
  * instead of deferring instantiation until the first resolution.
898
900
  *
899
901
  * @example
900
902
  * ```ts
901
- * @EagerInstantiate
902
- * @Scoped(Scope.Container)
903
+ * @EagerInstantiate()
903
904
  * class Wizard {}
904
905
  *
905
- * // A Wizard instance is immediately created and cached by the container
906
+ * // Wizard is registered with Container scope, and an instance
907
+ * // is immediately created and cached by the container
906
908
  * const wizard = container.register(Wizard);
907
909
  * ```
908
910
  *
909
911
  * @__NO_SIDE_EFFECTS__
910
- */ function EagerInstantiate(Class) {
911
- const metadata = getMetadata(Class);
912
- metadata.eagerInstantiate = true;
912
+ */ function EagerInstantiate() {
913
+ return function(Class) {
914
+ const metadata = getMetadata(Class);
915
+ const currentScope = metadata.scope;
916
+ assert(!currentScope || currentScope.value === Scope.Container, ()=>{
917
+ const { value, appliedBy } = currentScope;
918
+ return `class ${Class.name}: Scope.${value} was already set by @${appliedBy},\n ` + `but @EagerInstantiate is trying to set a conflicting Scope.Container.\n ` + `Only one decorator should set the class scope, or all must agree on the same value.`;
919
+ });
920
+ metadata.eagerInstantiate = true;
921
+ metadata.scope = {
922
+ value: Scope.Container,
923
+ appliedBy: "EagerInstantiate"
924
+ };
925
+ };
913
926
  }
914
927
 
915
928
  function forwardRef(token) {
@@ -1041,7 +1054,16 @@ function OptionalAll(token) {
1041
1054
  */ function Scoped(scope) {
1042
1055
  return function(Class) {
1043
1056
  const metadata = getMetadata(Class);
1044
- metadata.scope = scope;
1057
+ const currentScope = metadata.scope;
1058
+ assert(!currentScope || currentScope.value === scope, ()=>{
1059
+ const { value, appliedBy } = currentScope;
1060
+ const by = appliedBy === "Scoped" ? `another @${appliedBy} decorator` : `@${appliedBy}`;
1061
+ return `class ${Class.name}: Scope.${value} was already set by ${by},\n ` + `but @Scoped is trying to set a conflicting Scope.${scope}.\n ` + `Only one decorator should set the class scope, or all must agree on the same value.`;
1062
+ });
1063
+ metadata.scope = {
1064
+ value: scope,
1065
+ appliedBy: "Scoped"
1066
+ };
1045
1067
  };
1046
1068
  }
1047
1069