@lppedd/di-wise-neo 0.14.0 → 0.14.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.
@@ -55,18 +55,14 @@ interface Type<T> {
55
55
  * The name of the type.
56
56
  */
57
57
  readonly name: string;
58
- /**
59
- * Returns the stringified representation of the type.
60
- */
61
- readonly toString: () => string;
62
58
  /**
63
59
  * Ensures that different `Type<T>` types are not structurally compatible.
64
60
  *
65
- * This property is never used at runtime.
61
+ * This property is always `undefined` and is never used at runtime.
66
62
  *
67
63
  * @private
68
64
  */
69
- readonly __type?: T;
65
+ readonly __type: T | undefined;
70
66
  }
71
67
  /**
72
68
  * An injectable type `T` with a default {@link Provider} and optional default registration options.
@@ -91,7 +87,7 @@ interface Constructor<Instance extends object> {
91
87
  /**
92
88
  * Token type.
93
89
  */
94
- type Token<Value = any> = [Value] extends [object] ? Type<Value> | Constructor<Value> : Type<Value>;
90
+ type Token<Value = any> = [Value] extends [object] ? Type<Value> | Constructor<Value & object> : Type<Value>;
95
91
  /**
96
92
  * Describes a {@link Token} array with at least one element.
97
93
  */
@@ -621,13 +617,19 @@ declare function forwardRef<Value>(token: () => Token<Value>): TokenRef<Value>;
621
617
  /**
622
618
  * Parameter decorator that injects the instance associated with the given class.
623
619
  *
624
- * Throws an error if the class is not registered in the container.
620
+ * Throws an error if:
621
+ * - The class is not registered in the container.
622
+ * - A circular dependency is detected. Use function injection with {@link injectBy}
623
+ * if resolving circular dependencies is necessary.
625
624
  */
626
625
  declare function Inject<Instance extends object>(Class: Constructor<Instance>): ParameterDecorator;
627
626
  /**
628
627
  * Parameter decorator that injects the value associated with the given token.
629
628
  *
630
- * Throws an error if the token is not registered in the container.
629
+ * Throws an error if:
630
+ * - The token is not registered in the container.
631
+ * - A circular dependency is detected. Use function injection with {@link injectBy}
632
+ * if resolving circular dependencies is necessary.
631
633
  */
632
634
  declare function Inject<Value>(token: Token<Value>): ParameterDecorator;
633
635
  /**
@@ -636,7 +638,10 @@ declare function Inject<Value>(token: Token<Value>): ParameterDecorator;
636
638
  * Allows referencing a token declared later in the file by using the
637
639
  * {@link forwardRef} helper function.
638
640
  *
639
- * Throws an error if the token is not registered in the container.
641
+ * Throws an error if:
642
+ * - The token is not registered in the container.
643
+ * - A circular dependency is detected. Use function injection with {@link injectBy}
644
+ * if resolving circular dependencies is necessary.
640
645
  *
641
646
  * @example
642
647
  * ```ts
@@ -685,14 +690,18 @@ declare function Injectable<This extends object, Value extends This>(tokens: Tok
685
690
  * Parameter decorator that injects all instances provided by the registrations
686
691
  * associated with the given class.
687
692
  *
688
- * Throws an error if the class is not registered in the container.
693
+ * Throws an error if:
694
+ * - The class is not registered in the container.
695
+ * - A circular dependency is detected.
689
696
  */
690
697
  declare function InjectAll<Instance extends object>(Class: Constructor<Instance>): ParameterDecorator;
691
698
  /**
692
699
  * Parameter decorator that injects all values provided by the registrations
693
700
  * associated with the given token.
694
701
  *
695
- * Throws an error if the token is not registered in the container.
702
+ * Throws an error if:
703
+ * - The token is not registered in the container.
704
+ * - A circular dependency is detected.
696
705
  */
697
706
  declare function InjectAll<Value>(token: Token<Value>): ParameterDecorator;
698
707
  /**
@@ -702,7 +711,9 @@ declare function InjectAll<Value>(token: Token<Value>): ParameterDecorator;
702
711
  * Allows referencing a token declared later in the file by using the
703
712
  * {@link forwardRef} helper function.
704
713
  *
705
- * Throws an error if the token is not registered in the container.
714
+ * Throws an error if:
715
+ * - The token is not registered in the container.
716
+ * - A circular dependency is detected.
706
717
  *
707
718
  * @example
708
719
  * ```ts
@@ -738,11 +749,17 @@ declare function Named(name: string): ClassDecorator & ParameterDecorator;
738
749
  /**
739
750
  * Parameter decorator that injects the instance associated with the given class,
740
751
  * or `undefined` if the class is not registered in the container.
752
+ *
753
+ * Throws an error if a circular dependency is detected. Use function injection
754
+ * with {@link optionalBy} if resolving circular dependencies is necessary.
741
755
  */
742
756
  declare function Optional<Instance extends object>(Class: Constructor<Instance>): ParameterDecorator;
743
757
  /**
744
758
  * Parameter decorator that injects the value associated with the given token,
745
759
  * or `undefined` if the token is not registered in the container.
760
+ *
761
+ * Throws an error if a circular dependency is detected. Use function injection
762
+ * with {@link optionalBy} if resolving circular dependencies is necessary.
746
763
  */
747
764
  declare function Optional<Value>(token: Token<Value>): ParameterDecorator;
748
765
  /**
@@ -752,6 +769,9 @@ declare function Optional<Value>(token: Token<Value>): ParameterDecorator;
752
769
  * Allows referencing a token declared later in the file by using the
753
770
  * {@link forwardRef} helper function.
754
771
  *
772
+ * Throws an error if a circular dependency is detected. Use function injection
773
+ * with {@link optionalBy} if resolving circular dependencies is necessary.
774
+ *
755
775
  * @example
756
776
  * ```ts
757
777
  * class Wizard {
@@ -767,12 +787,16 @@ declare function Optional<Value>(tokens: TokenRef<Value>): ParameterDecorator;
767
787
  * Parameter decorator that injects all instances provided by the registrations
768
788
  * associated with the given class or an empty array if the class is not registered
769
789
  * in the container.
790
+ *
791
+ * Throws an error if a circular dependency is detected.
770
792
  */
771
793
  declare function OptionalAll<Instance extends object>(Class: Constructor<Instance>): ParameterDecorator;
772
794
  /**
773
795
  * Parameter decorator that injects all values provided by the registrations
774
796
  * associated with the given token or an empty array if the token is not registered
775
797
  * in the container.
798
+ *
799
+ * Throws an error if a circular dependency is detected.
776
800
  */
777
801
  declare function OptionalAll<Value>(token: Token<Value>): ParameterDecorator;
778
802
  /**
@@ -783,6 +807,8 @@ declare function OptionalAll<Value>(token: Token<Value>): ParameterDecorator;
783
807
  * Allows referencing a token declared later in the file by using the
784
808
  * {@link forwardRef} helper function.
785
809
  *
810
+ * Throws an error if a circular dependency is detected.
811
+ *
786
812
  * @example
787
813
  * ```ts
788
814
  * class Wizard {
@@ -821,13 +847,19 @@ declare function Scoped(scope: Scope): ClassDecorator;
821
847
  /**
822
848
  * Injects the instance associated with the given class.
823
849
  *
824
- * Throws an error if the class is not registered in the container.
850
+ * Throws an error if:
851
+ * - The class is not registered in the container.
852
+ * - A circular dependency is detected. Use {@link injectBy} if resolving
853
+ * circular dependencies is necessary.
825
854
  */
826
855
  declare function inject<Instance extends object>(Class: Constructor<Instance>, name?: string): Instance;
827
856
  /**
828
857
  * Injects the value associated with the given token.
829
858
  *
830
- * Throws an error if the token is not registered in the container.
859
+ * Throws an error if:
860
+ * - The token is not registered in the container.
861
+ * - A circular dependency is detected. Use {@link injectBy} if resolving
862
+ * circular dependencies is necessary.
831
863
  */
832
864
  declare function inject<Value>(token: Token<Value>, name?: string): Value;
833
865
  /**
@@ -836,7 +868,7 @@ declare function inject<Value>(token: Token<Value>, name?: string): Value;
836
868
  * Throws an error if the class is not registered in the container.
837
869
  *
838
870
  * Compared to {@link inject}, `injectBy` accepts a `thisArg` argument
839
- * (the containing class) which is used to resolve circular dependencies.
871
+ * (e.g., the containing class instance) which is used to resolve circular dependencies.
840
872
  *
841
873
  * @example
842
874
  * ```ts
@@ -860,7 +892,7 @@ declare function injectBy<Instance extends object>(thisArg: any, Class: Construc
860
892
  * Throws an error if the token is not registered in the container.
861
893
  *
862
894
  * Compared to {@link inject}, `injectBy` accepts a `thisArg` argument
863
- * (the containing class) which is used to resolve circular dependencies.
895
+ * (e.g., the containing class instance) which is used to resolve circular dependencies.
864
896
  *
865
897
  * @example
866
898
  * ```ts
@@ -882,13 +914,17 @@ declare function injectBy<Value>(thisArg: any, token: Token<Value>, name?: strin
882
914
  /**
883
915
  * Injects all instances provided by the registrations associated with the given class.
884
916
  *
885
- * Throws an error if the class is not registered in the container.
917
+ * Throws an error if:
918
+ * - The class is not registered in the container.
919
+ * - A circular dependency is detected.
886
920
  */
887
921
  declare function injectAll<Instance extends object>(Class: Constructor<Instance>): Instance[];
888
922
  /**
889
923
  * Injects all values provided by the registrations associated with the given token.
890
924
  *
891
- * Throws an error if the token is not registered in the container.
925
+ * Throws an error if:
926
+ * - The token is not registered in the container.
927
+ * - A circular dependency is detected.
892
928
  */
893
929
  declare function injectAll<Value>(token: Token<Value>): Value[];
894
930
 
@@ -1048,11 +1084,17 @@ declare function applyMiddleware(container: Container, middlewares: Middleware[]
1048
1084
  /**
1049
1085
  * Injects the instance associated with the given class,
1050
1086
  * or `undefined` if the class is not registered in the container.
1087
+ *
1088
+ * Throws an error if a circular dependency is detected.
1089
+ * Use {@link optionalBy} if resolving circular dependencies is necessary.
1051
1090
  */
1052
1091
  declare function optional<Instance extends object>(Class: Constructor<Instance>, name?: string): Instance | undefined;
1053
1092
  /**
1054
1093
  * Injects the value associated with the given token,
1055
1094
  * or `undefined` if the token is not registered in the container.
1095
+ *
1096
+ * Throws an error if a circular dependency is detected.
1097
+ * Use {@link optionalBy} if resolving circular dependencies is necessary.
1056
1098
  */
1057
1099
  declare function optional<Value>(token: Token<Value>, name?: string): Value | undefined;
1058
1100
  /**
@@ -1060,7 +1102,7 @@ declare function optional<Value>(token: Token<Value>, name?: string): Value | un
1060
1102
  * or `undefined` if the class is not registered in the container.
1061
1103
  *
1062
1104
  * Compared to {@link optional}, `optionalBy` accepts a `thisArg` argument
1063
- * (the containing class) which is used to resolve circular dependencies.
1105
+ * (e.g., the containing class instance) which is used to resolve circular dependencies.
1064
1106
  *
1065
1107
  * @param thisArg - The containing instance, used to help resolve circular dependencies.
1066
1108
  * @param Class - The class to resolve.
@@ -1072,7 +1114,7 @@ declare function optionalBy<Instance extends object>(thisArg: any, Class: Constr
1072
1114
  * or `undefined` if the token is not registered in the container.
1073
1115
  *
1074
1116
  * Compared to {@link optional}, `optionalBy` accepts a `thisArg` argument
1075
- * (the containing class) which is used to resolve circular dependencies.
1117
+ * (e.g., the containing class instance) which is used to resolve circular dependencies.
1076
1118
  *
1077
1119
  * @param thisArg - The containing instance, used to help resolve circular dependencies.
1078
1120
  * @param token - The token to resolve.
@@ -1083,11 +1125,15 @@ declare function optionalBy<Value>(thisArg: any, token: Token<Value>, name?: str
1083
1125
  /**
1084
1126
  * Injects all instances provided by the registrations associated with the given class
1085
1127
  * or an empty array if the class is not registered in the container.
1128
+ *
1129
+ * Throws an error if a circular dependency is detected.
1086
1130
  */
1087
1131
  declare function optionalAll<Instance extends object>(Class: Constructor<Instance>): Instance[];
1088
1132
  /**
1089
1133
  * Injects all values provided by the registrations associated with the given token
1090
1134
  * or an empty array if the token is not registered in the container.
1135
+ *
1136
+ * Throws an error if a circular dependency is detected.
1091
1137
  */
1092
1138
  declare function optionalAll<Value>(token: Token<Value>): Value[];
1093
1139
 
package/dist/cjs/index.js CHANGED
@@ -337,7 +337,6 @@ const Scope = {
337
337
  */ Container: "Container"
338
338
  };
339
339
 
340
- // @internal
341
340
  // @__NO_SIDE_EFFECTS__
342
341
  function createType(typeName, provider, options) {
343
342
  const name = `Type<${typeName}>`;
@@ -575,13 +574,66 @@ function isDisposable(value) {
575
574
  if (isConstructor(token)) {
576
575
  this.registerClass(token);
577
576
  } else {
578
- this.registerType(token, token.provider, token.options);
577
+ this.registerToken(token, token.provider, token.options);
579
578
  }
580
579
  } else {
581
- this.registerType(...args);
580
+ this.registerToken(...args);
582
581
  }
583
582
  return this;
584
583
  }
584
+ unregister(token, name) {
585
+ this.checkDisposed();
586
+ const registrations = this.myTokenRegistry.delete(token, name);
587
+ const values = new Set();
588
+ for (const registration of registrations){
589
+ const value = registration.value;
590
+ if (value) {
591
+ values.add(value.current);
592
+ }
593
+ }
594
+ return Array.from(values);
595
+ }
596
+ resolve(token, name) {
597
+ this.checkDisposed();
598
+ return this.resolveToken(token, name, false);
599
+ }
600
+ tryResolve(token, name) {
601
+ this.checkDisposed();
602
+ return this.resolveToken(token, name, true);
603
+ }
604
+ resolveAll(token) {
605
+ this.checkDisposed();
606
+ return this.resolveAllToken(token, false);
607
+ }
608
+ tryResolveAll(token) {
609
+ this.checkDisposed();
610
+ return this.resolveAllToken(token, true);
611
+ }
612
+ dispose() {
613
+ if (this.myDisposed) {
614
+ return;
615
+ }
616
+ // Dispose children containers first
617
+ for (const child of this.myChildren){
618
+ child.dispose();
619
+ }
620
+ this.myChildren.clear();
621
+ // Remove ourselves from our parent container
622
+ this.myParent?.myChildren?.delete(this);
623
+ this.myDisposed = true;
624
+ const [, registrations] = this.myTokenRegistry.deleteAll();
625
+ const disposedRefs = new Set();
626
+ // Dispose all resolved (aka instantiated) tokens that implement the Disposable interface
627
+ for (const registration of registrations){
628
+ const value = registration.value?.current;
629
+ if (isDisposable(value) && !disposedRefs.has(value)) {
630
+ disposedRefs.add(value);
631
+ value.dispose();
632
+ }
633
+ }
634
+ // Allow values to be GCed
635
+ disposedRefs.clear();
636
+ }
585
637
  registerClass(Class) {
586
638
  const metadata = getMetadata(Class);
587
639
  const name = metadata.name;
@@ -614,7 +666,7 @@ function isDisposable(value) {
614
666
  this.resolveProviderValue(Class, registration);
615
667
  }
616
668
  }
617
- registerType(token, provider, options) {
669
+ registerToken(token, provider, options) {
618
670
  const name = provider.name;
619
671
  check(name === undefined || name.trim(), `name qualifier for token ${getTokenName(token)} must not be empty`);
620
672
  if (isClassProvider(provider)) {
@@ -647,59 +699,6 @@ function isDisposable(value) {
647
699
  });
648
700
  }
649
701
  }
650
- unregister(token, name) {
651
- this.checkDisposed();
652
- const registrations = this.myTokenRegistry.delete(token, name);
653
- const values = new Set();
654
- for (const registration of registrations){
655
- const value = registration.value;
656
- if (value) {
657
- values.add(value.current);
658
- }
659
- }
660
- return Array.from(values);
661
- }
662
- resolve(token, name) {
663
- this.checkDisposed();
664
- return this.resolveToken(token, name, false);
665
- }
666
- tryResolve(token, name) {
667
- this.checkDisposed();
668
- return this.resolveToken(token, name, true);
669
- }
670
- resolveAll(token) {
671
- this.checkDisposed();
672
- return this.resolveAllToken(token, false);
673
- }
674
- tryResolveAll(token) {
675
- this.checkDisposed();
676
- return this.resolveAllToken(token, true);
677
- }
678
- dispose() {
679
- if (this.myDisposed) {
680
- return;
681
- }
682
- // Dispose children containers first
683
- for (const child of this.myChildren){
684
- child.dispose();
685
- }
686
- this.myChildren.clear();
687
- // Remove ourselves from our parent container
688
- this.myParent?.myChildren?.delete(this);
689
- this.myDisposed = true;
690
- const [, registrations] = this.myTokenRegistry.deleteAll();
691
- const disposedRefs = new Set();
692
- // Dispose all resolved (aka instantiated) tokens that implement the Disposable interface
693
- for (const registration of registrations){
694
- const value = registration.value?.current;
695
- if (isDisposable(value) && !disposedRefs.has(value)) {
696
- disposedRefs.add(value);
697
- value.dispose();
698
- }
699
- }
700
- // Allow values to be GCed
701
- disposedRefs.clear();
702
- }
703
702
  resolveToken(token, name, optional) {
704
703
  let registration = this.myTokenRegistry.get(token, name);
705
704
  if (!registration && isConstructor(token)) {
@@ -1008,6 +1007,7 @@ function isDisposable(value) {
1008
1007
  };
1009
1008
  }
1010
1009
 
1010
+ // @__NO_SIDE_EFFECTS__
1011
1011
  function forwardRef(token) {
1012
1012
  return {
1013
1013
  getRefTokens: ()=>{
@@ -1083,6 +1083,7 @@ function describeParam(target, methodKey, parameterIndex) {
1083
1083
  return `${location}(parameter #${parameterIndex})`;
1084
1084
  }
1085
1085
 
1086
+ // @__NO_SIDE_EFFECTS__
1086
1087
  function Inject(token) {
1087
1088
  return function(target, propertyKey, parameterIndex) {
1088
1089
  updateParameterMetadata("Inject", target, propertyKey, parameterIndex, (dependency)=>{
@@ -1093,9 +1094,8 @@ function Inject(token) {
1093
1094
  };
1094
1095
  }
1095
1096
 
1096
- /**
1097
- * @__NO_SIDE_EFFECTS__
1098
- */ function Injectable(...args) {
1097
+ // @__NO_SIDE_EFFECTS__
1098
+ function Injectable(...args) {
1099
1099
  return function(Class) {
1100
1100
  const metadata = getMetadata(Class);
1101
1101
  const arg0 = args[0];
@@ -1113,6 +1113,7 @@ function Inject(token) {
1113
1113
  };
1114
1114
  }
1115
1115
 
1116
+ // @__NO_SIDE_EFFECTS__
1116
1117
  function InjectAll(token) {
1117
1118
  return function(target, propertyKey, parameterIndex) {
1118
1119
  updateParameterMetadata("InjectAll", target, propertyKey, parameterIndex, (dependency)=>{
@@ -1165,6 +1166,7 @@ function InjectAll(token) {
1165
1166
  };
1166
1167
  }
1167
1168
 
1169
+ // @__NO_SIDE_EFFECTS__
1168
1170
  function Optional(token) {
1169
1171
  return function(target, propertyKey, parameterIndex) {
1170
1172
  updateParameterMetadata("Optional", target, propertyKey, parameterIndex, (dependency)=>{
@@ -1175,6 +1177,7 @@ function Optional(token) {
1175
1177
  };
1176
1178
  }
1177
1179
 
1180
+ // @__NO_SIDE_EFFECTS__
1178
1181
  function OptionalAll(token) {
1179
1182
  return function(target, propertyKey, parameterIndex) {
1180
1183
  updateParameterMetadata("OptionalAll", target, propertyKey, parameterIndex, (dependency)=>{