@lppedd/di-wise-neo 0.14.2 → 0.15.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/README.md CHANGED
@@ -435,11 +435,11 @@ Normally, attempting to do that would result in a `ReferenceError`:
435
435
 
436
436
  > ReferenceError: Cannot access 'IStore' before initialization
437
437
 
438
- We can work around this problem by using the `forwardRef` helper function:
438
+ We can work around this problem by using the `tokenRef` helper function:
439
439
 
440
440
  ```ts
441
441
  export class ExtensionContext {
442
- constructor(@OptionalAll(forwardRef(() => IStore)) readonly stores: Store[]) {}
442
+ constructor(@OptionalAll(tokenRef(() => IStore)) readonly stores: Store[]) {}
443
443
 
444
444
  /* ... */
445
445
  }
@@ -119,6 +119,33 @@ declare function createType<T>(typeName: string): Type<T>;
119
119
  */
120
120
  declare function createType<T>(typeName: string, provider: Provider<T>, options?: RegistrationOptions): ProviderType<T>;
121
121
 
122
+ interface ClassRef<Instance extends object> {
123
+ readonly getRefClass: () => Constructor<Instance>;
124
+ }
125
+ interface TokensRef<Value = any> {
126
+ readonly getRefTokens: () => Set<Token<Value>>;
127
+ }
128
+ interface TokenRef<Value = any> {
129
+ readonly getRefToken: () => Token<Value>;
130
+ }
131
+ /**
132
+ * Allows referencing a class declared later in the file by wrapping it
133
+ * in a lazily evaluated function.
134
+ *
135
+ * @__NO_SIDE_EFFECTS__
136
+ */
137
+ declare function classRef<Instance extends object>(Class: () => Constructor<Instance>): ClassRef<Instance>;
138
+ /**
139
+ * Allows referencing tokens declared later in the file by wrapping them
140
+ * in a lazily evaluated function.
141
+ */
142
+ declare function tokenRef<Value>(token: () => Tokens<Value>): TokensRef<Value>;
143
+ /**
144
+ * Allows referencing a token declared later in the file by wrapping it
145
+ * in a lazily evaluated function.
146
+ */
147
+ declare function tokenRef<Value>(token: () => Token<Value>): TokenRef<Value>;
148
+
122
149
  /**
123
150
  * Provides a class instance for a token via a class constructor.
124
151
  */
@@ -126,7 +153,7 @@ interface ClassProvider<Instance extends object> {
126
153
  /**
127
154
  * The class to instantiate for the token.
128
155
  */
129
- readonly useClass: Constructor<Instance>;
156
+ readonly useClass: Constructor<Instance> | ClassRef<Instance>;
130
157
  /**
131
158
  * An optional name to qualify this provider.
132
159
  * If specified, the token must be resolved using the same name.
@@ -597,23 +624,6 @@ declare function AutoRegister(): ClassDecorator;
597
624
  */
598
625
  declare function EagerInstantiate(): ClassDecorator;
599
626
 
600
- interface TokensRef<Value = any> {
601
- readonly getRefTokens: () => Set<Token<Value>>;
602
- }
603
- interface TokenRef<Value = any> {
604
- readonly getRefToken: () => Token<Value>;
605
- }
606
- /**
607
- * Allows referencing tokens declared later in the file by wrapping them
608
- * in a lazily evaluated function.
609
- */
610
- declare function forwardRef<Value>(token: () => Tokens<Value>): TokensRef<Value>;
611
- /**
612
- * Allows referencing a token declared later in the file by wrapping it
613
- * in a lazily evaluated function.
614
- */
615
- declare function forwardRef<Value>(token: () => Token<Value>): TokenRef<Value>;
616
-
617
627
  /**
618
628
  * Parameter decorator that injects the instance associated with the given class.
619
629
  *
@@ -636,7 +646,7 @@ declare function Inject<Value>(token: Token<Value>): ParameterDecorator;
636
646
  * Parameter decorator that injects the value associated with the given token.
637
647
  *
638
648
  * Allows referencing a token declared later in the file by using the
639
- * {@link forwardRef} helper function.
649
+ * {@link tokenRef} helper function.
640
650
  *
641
651
  * Throws an error if:
642
652
  * - The token is not registered in the container.
@@ -646,7 +656,7 @@ declare function Inject<Value>(token: Token<Value>): ParameterDecorator;
646
656
  * @example
647
657
  * ```ts
648
658
  * class Wizard {
649
- * constructor(@Inject(forwardRef(() => Wand)) readonly wand: Wand) {}
659
+ * constructor(@Inject(tokenRef(() => Wand)) readonly wand: Wand) {}
650
660
  * }
651
661
  * // Other code...
652
662
  * class Wand {}
@@ -674,11 +684,11 @@ declare function Injectable<This extends object, Value extends This>(...tokens:
674
684
  * The container uses {@link ExistingProvider} under the hood.
675
685
  *
676
686
  * Allows referencing tokens that are declared later in the file by using
677
- * the {@link forwardRef} helper function.
687
+ * the {@link tokenRef} helper function.
678
688
  *
679
689
  * @example
680
690
  * ```ts
681
- * @Injectable(forwardRef() => Weapon) // Weapon is declared after Wand
691
+ * @Injectable(tokenRef() => Weapon) // Weapon is declared after Wand
682
692
  * class Wizard {}
683
693
  * // Other code...
684
694
  * class Weapon {}
@@ -709,7 +719,7 @@ declare function InjectAll<Value>(token: Token<Value>): ParameterDecorator;
709
719
  * associated with the given token.
710
720
  *
711
721
  * Allows referencing a token declared later in the file by using the
712
- * {@link forwardRef} helper function.
722
+ * {@link tokenRef} helper function.
713
723
  *
714
724
  * Throws an error if:
715
725
  * - The token is not registered in the container.
@@ -718,7 +728,7 @@ declare function InjectAll<Value>(token: Token<Value>): ParameterDecorator;
718
728
  * @example
719
729
  * ```ts
720
730
  * class Wizard {
721
- * constructor(@InjectAll(forwardRef(() => Wand)) readonly wands: Wand[]) {}
731
+ * constructor(@InjectAll(tokenRef(() => Wand)) readonly wands: Wand[]) {}
722
732
  * }
723
733
  * // Other code...
724
734
  * class Wand {}
@@ -767,7 +777,7 @@ declare function Optional<Value>(token: Token<Value>): ParameterDecorator;
767
777
  * or `undefined` if the token is not registered in the container.
768
778
  *
769
779
  * Allows referencing a token declared later in the file by using the
770
- * {@link forwardRef} helper function.
780
+ * {@link tokenRef} helper function.
771
781
  *
772
782
  * Throws an error if a circular dependency is detected. Use function injection
773
783
  * with {@link optionalBy} if resolving circular dependencies is necessary.
@@ -775,7 +785,7 @@ declare function Optional<Value>(token: Token<Value>): ParameterDecorator;
775
785
  * @example
776
786
  * ```ts
777
787
  * class Wizard {
778
- * constructor(@Optional(forwardRef(() => Wand)) readonly wand: Wand | undefined) {}
788
+ * constructor(@Optional(tokenRef(() => Wand)) readonly wand: Wand | undefined) {}
779
789
  * }
780
790
  * // Other code...
781
791
  * class Wand {}
@@ -805,14 +815,14 @@ declare function OptionalAll<Value>(token: Token<Value>): ParameterDecorator;
805
815
  * in the container.
806
816
  *
807
817
  * Allows referencing a token declared later in the file by using the
808
- * {@link forwardRef} helper function.
818
+ * {@link tokenRef} helper function.
809
819
  *
810
820
  * Throws an error if a circular dependency is detected.
811
821
  *
812
822
  * @example
813
823
  * ```ts
814
824
  * class Wizard {
815
- * constructor(@OptionalAll(forwardRef(() => Wand)) readonly wands: Wand[]) {}
825
+ * constructor(@OptionalAll(tokenRef(() => Wand)) readonly wands: Wand[]) {}
816
826
  * }
817
827
  * // Other code...
818
828
  * class Wand {}
@@ -1137,5 +1147,5 @@ declare function optionalAll<Instance extends object>(Class: Constructor<Instanc
1137
1147
  */
1138
1148
  declare function optionalAll<Value>(token: Token<Value>): Value[];
1139
1149
 
1140
- export { AutoRegister, EagerInstantiate, Inject, InjectAll, Injectable, Injector, Named, Optional, OptionalAll, Scope, Scoped, applyMiddleware, build, createContainer, createType, forwardRef, inject, injectAll, injectBy, optional, optionalAll, optionalBy, setClassIdentityMapping };
1141
- export type { ClassProvider, Constructor, Container, ContainerOptions, ExistingProvider, FactoryProvider, Middleware, MiddlewareComposer, Provider, ProviderType, RegistrationOptions, Token, TokenRef, Tokens, TokensRef, Type, ValueProvider };
1150
+ export { AutoRegister, EagerInstantiate, Inject, InjectAll, Injectable, Injector, Named, Optional, OptionalAll, Scope, Scoped, applyMiddleware, build, classRef, createContainer, createType, inject, injectAll, injectBy, optional, optionalAll, optionalBy, setClassIdentityMapping, tokenRef };
1151
+ export type { ClassProvider, ClassRef, Constructor, Container, ContainerOptions, ExistingProvider, FactoryProvider, Middleware, MiddlewareComposer, Provider, ProviderType, RegistrationOptions, Token, TokenRef, Tokens, TokensRef, Type, ValueProvider };
package/dist/cjs/index.js CHANGED
@@ -186,6 +186,47 @@ function injectAll(token) {
186
186
  return context.container.resolveAll(token);
187
187
  }
188
188
 
189
+ /**
190
+ * Allows referencing a class declared later in the file by wrapping it
191
+ * in a lazily evaluated function.
192
+ *
193
+ * @__NO_SIDE_EFFECTS__
194
+ */ function classRef(Class) {
195
+ return {
196
+ getRefClass: ()=>Class()
197
+ };
198
+ }
199
+ // @__NO_SIDE_EFFECTS__
200
+ function tokenRef(token) {
201
+ return {
202
+ getRefTokens: ()=>{
203
+ // Normalize the single token here so that we don't have to do it at every getRefTokens call site
204
+ const tokenOrTokens = token();
205
+ const tokensArray = Array.isArray(tokenOrTokens) ? tokenOrTokens : [
206
+ tokenOrTokens
207
+ ];
208
+ return new Set(tokensArray);
209
+ },
210
+ getRefToken: ()=>{
211
+ const tokenOrTokens = token();
212
+ check(!Array.isArray(tokenOrTokens), "internal error: ref tokens should be a single token");
213
+ return tokenOrTokens;
214
+ }
215
+ };
216
+ }
217
+ // @internal
218
+ function isClassRef(value) {
219
+ return value && typeof value === "object" && typeof value.getRefClass === "function";
220
+ }
221
+ // @internal
222
+ function isTokensRef(value) {
223
+ return value && typeof value === "object" && typeof value.getRefTokens === "function";
224
+ }
225
+ // @internal
226
+ function isTokenRef(value) {
227
+ return value && typeof value === "object" && typeof value.getRefToken === "function";
228
+ }
229
+
189
230
  // @internal
190
231
  class Metadata {
191
232
  constructor(Class){
@@ -234,7 +275,8 @@ class Metadata {
234
275
  }
235
276
  }
236
277
  // @internal
237
- function getMetadata(Class) {
278
+ function getMetadata(classOrRef) {
279
+ const Class = isClassRef(classOrRef) ? classOrRef.getRefClass() : classOrRef;
238
280
  const originalClass = classIdentityMap.get(Class) ?? Class;
239
281
  let metadata = metadataMap.get(originalClass);
240
282
  if (!metadata) {
@@ -1007,33 +1049,6 @@ function isDisposable(value) {
1007
1049
  };
1008
1050
  }
1009
1051
 
1010
- // @__NO_SIDE_EFFECTS__
1011
- function forwardRef(token) {
1012
- return {
1013
- getRefTokens: ()=>{
1014
- // Normalize the single token here so that we don't have to do it at every getRefTokens call site
1015
- const tokenOrTokens = token();
1016
- const tokensArray = Array.isArray(tokenOrTokens) ? tokenOrTokens : [
1017
- tokenOrTokens
1018
- ];
1019
- return new Set(tokensArray);
1020
- },
1021
- getRefToken: ()=>{
1022
- const tokenOrTokens = token();
1023
- check(!Array.isArray(tokenOrTokens), "internal error: ref tokens should be a single token");
1024
- return tokenOrTokens;
1025
- }
1026
- };
1027
- }
1028
- // @internal
1029
- function isTokensRef(value) {
1030
- return value && typeof value === "object" && typeof value.getRefTokens === "function";
1031
- }
1032
- // @internal
1033
- function isTokenRef(value) {
1034
- return value && typeof value === "object" && typeof value.getRefToken === "function";
1035
- }
1036
-
1037
1052
  // @internal
1038
1053
  function updateParameterMetadata(decorator, target, methodKey, parameterIndex, updateFn) {
1039
1054
  // Error out immediately if the decorator has been applied to a static method
@@ -1089,7 +1104,7 @@ function Inject(token) {
1089
1104
  updateParameterMetadata("Inject", target, propertyKey, parameterIndex, (dependency)=>{
1090
1105
  checkSingleDecorator(dependency, target, propertyKey, parameterIndex);
1091
1106
  dependency.appliedBy = "Inject";
1092
- dependency.tokenRef = isTokenRef(token) ? token : forwardRef(()=>token);
1107
+ dependency.tokenRef = isTokenRef(token) ? token : tokenRef(()=>token);
1093
1108
  });
1094
1109
  };
1095
1110
  }
@@ -1099,7 +1114,7 @@ function Injectable(...args) {
1099
1114
  return function(Class) {
1100
1115
  const metadata = getMetadata(Class);
1101
1116
  const arg0 = args[0];
1102
- const tokensRef = isTokensRef(arg0) ? arg0 : forwardRef(()=>args);
1117
+ const tokensRef = isTokensRef(arg0) ? arg0 : tokenRef(()=>args);
1103
1118
  const existingTokensRef = metadata.tokensRef;
1104
1119
  metadata.tokensRef = {
1105
1120
  getRefTokens: ()=>{
@@ -1119,7 +1134,7 @@ function InjectAll(token) {
1119
1134
  updateParameterMetadata("InjectAll", target, propertyKey, parameterIndex, (dependency)=>{
1120
1135
  checkSingleDecorator(dependency, target, propertyKey, parameterIndex);
1121
1136
  dependency.appliedBy = "InjectAll";
1122
- dependency.tokenRef = isTokenRef(token) ? token : forwardRef(()=>token);
1137
+ dependency.tokenRef = isTokenRef(token) ? token : tokenRef(()=>token);
1123
1138
  checkNamedDecorator(dependency, target, propertyKey, parameterIndex);
1124
1139
  });
1125
1140
  };
@@ -1172,7 +1187,7 @@ function Optional(token) {
1172
1187
  updateParameterMetadata("Optional", target, propertyKey, parameterIndex, (dependency)=>{
1173
1188
  checkSingleDecorator(dependency, target, propertyKey, parameterIndex);
1174
1189
  dependency.appliedBy = "Optional";
1175
- dependency.tokenRef = isTokenRef(token) ? token : forwardRef(()=>token);
1190
+ dependency.tokenRef = isTokenRef(token) ? token : tokenRef(()=>token);
1176
1191
  });
1177
1192
  };
1178
1193
  }
@@ -1183,7 +1198,7 @@ function OptionalAll(token) {
1183
1198
  updateParameterMetadata("OptionalAll", target, propertyKey, parameterIndex, (dependency)=>{
1184
1199
  checkSingleDecorator(dependency, target, propertyKey, parameterIndex);
1185
1200
  dependency.appliedBy = "OptionalAll";
1186
- dependency.tokenRef = isTokenRef(token) ? token : forwardRef(()=>token);
1201
+ dependency.tokenRef = isTokenRef(token) ? token : tokenRef(()=>token);
1187
1202
  checkNamedDecorator(dependency, target, propertyKey, parameterIndex);
1188
1203
  });
1189
1204
  };
@@ -1321,9 +1336,9 @@ exports.Scope = Scope;
1321
1336
  exports.Scoped = Scoped;
1322
1337
  exports.applyMiddleware = applyMiddleware;
1323
1338
  exports.build = build;
1339
+ exports.classRef = classRef;
1324
1340
  exports.createContainer = createContainer;
1325
1341
  exports.createType = createType;
1326
- exports.forwardRef = forwardRef;
1327
1342
  exports.inject = inject;
1328
1343
  exports.injectAll = injectAll;
1329
1344
  exports.injectBy = injectBy;
@@ -1331,4 +1346,5 @@ exports.optional = optional;
1331
1346
  exports.optionalAll = optionalAll;
1332
1347
  exports.optionalBy = optionalBy;
1333
1348
  exports.setClassIdentityMapping = setClassIdentityMapping;
1349
+ exports.tokenRef = tokenRef;
1334
1350
  //# sourceMappingURL=index.js.map