@lppedd/di-wise-neo 0.18.1 → 0.20.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/cjs/index.d.ts +69 -65
- package/dist/cjs/index.js +35 -35
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/index.d.mts +69 -65
- package/dist/es/index.mjs +35 -35
- package/dist/es/index.mjs.map +1 -1
- package/package.json +9 -9
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
type ClassDecorator<Class extends object> = (target:
|
|
1
|
+
type ClassDecorator<Class extends object> = <T extends Constructor<Class>>(target: T) => T | void;
|
|
2
2
|
type ParameterDecorator = (target: object, propertyKey: string | symbol | undefined, parameterIndex: number) => void;
|
|
3
3
|
|
|
4
4
|
declare const Scope: {
|
|
@@ -34,7 +34,7 @@ interface RegistrationOptions {
|
|
|
34
34
|
/**
|
|
35
35
|
* Create a one-off type token from a factory function.
|
|
36
36
|
*
|
|
37
|
-
*
|
|
37
|
+
* Example:
|
|
38
38
|
* ```ts
|
|
39
39
|
* class Wizard {
|
|
40
40
|
* wand = inject(
|
|
@@ -102,7 +102,7 @@ type Tokens<Value> = [Token<Value>, ...Token<Value>[]];
|
|
|
102
102
|
/**
|
|
103
103
|
* Creates a type token.
|
|
104
104
|
*
|
|
105
|
-
*
|
|
105
|
+
* Example:
|
|
106
106
|
* ```ts
|
|
107
107
|
* const ISpell = createType<Spell>("Spell");
|
|
108
108
|
* container.register(ISpell, {
|
|
@@ -114,7 +114,7 @@ declare function createType<T>(typeName: string): Type<T>;
|
|
|
114
114
|
/**
|
|
115
115
|
* Creates a type token with a default {@link Provider} and optional default registration options.
|
|
116
116
|
*
|
|
117
|
-
*
|
|
117
|
+
* Example:
|
|
118
118
|
* ```ts
|
|
119
119
|
* // Notice how we pass in a Provider directly at type creation site
|
|
120
120
|
* const ISpell = createType<Spell>("Spell", {
|
|
@@ -130,22 +130,18 @@ interface ClassRef<Instance extends object> {
|
|
|
130
130
|
readonly getRefClass: () => Constructor<Instance>;
|
|
131
131
|
}
|
|
132
132
|
interface TokenRef<Value> {
|
|
133
|
-
readonly
|
|
133
|
+
readonly getRefToken: () => Token<Value>;
|
|
134
134
|
}
|
|
135
135
|
/**
|
|
136
136
|
* Allows referencing a class declared later in the file by wrapping it
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
* @__NO_SIDE_EFFECTS__
|
|
137
|
+
* into a lazily evaluated function.
|
|
140
138
|
*/
|
|
141
139
|
declare function classRef<Instance extends object>(Class: () => Constructor<Instance>): ClassRef<Instance>;
|
|
142
140
|
/**
|
|
143
|
-
* Allows referencing
|
|
141
|
+
* Allows referencing a token declared later in the file by wrapping it
|
|
144
142
|
* into a lazily evaluated function.
|
|
145
|
-
*
|
|
146
|
-
* @__NO_SIDE_EFFECTS__
|
|
147
143
|
*/
|
|
148
|
-
declare function tokenRef<Value>(token: () => Token<Value>
|
|
144
|
+
declare function tokenRef<Value>(token: () => Token<Value>): TokenRef<Value>;
|
|
149
145
|
|
|
150
146
|
/**
|
|
151
147
|
* Provides a class instance for a token via a class constructor.
|
|
@@ -161,7 +157,7 @@ interface ClassProvider<Instance extends object> {
|
|
|
161
157
|
*
|
|
162
158
|
* Equivalent to decorating the class with `@Named(...)`.
|
|
163
159
|
*
|
|
164
|
-
*
|
|
160
|
+
* Example:
|
|
165
161
|
* ```ts
|
|
166
162
|
* export class ExtensionContext {
|
|
167
163
|
* // Decorator-based injection
|
|
@@ -189,7 +185,7 @@ interface FactoryProvider<Value> {
|
|
|
189
185
|
* An optional name to qualify this provider.
|
|
190
186
|
* If specified, the token must be resolved using the same name.
|
|
191
187
|
*
|
|
192
|
-
*
|
|
188
|
+
* Example:
|
|
193
189
|
* ```ts
|
|
194
190
|
* export class ExtensionContext {
|
|
195
191
|
* // Decorator-based injection
|
|
@@ -214,7 +210,7 @@ interface ValueProvider<Value> {
|
|
|
214
210
|
* An optional name to qualify this provider.
|
|
215
211
|
* If specified, the token must be resolved using the same name.
|
|
216
212
|
*
|
|
217
|
-
*
|
|
213
|
+
* Example:
|
|
218
214
|
* ```ts
|
|
219
215
|
* export class ExtensionContext {
|
|
220
216
|
* // Decorator-based injection
|
|
@@ -236,7 +232,7 @@ interface ExistingProvider<Value> {
|
|
|
236
232
|
/**
|
|
237
233
|
* The existing token to alias, with an optional name qualifier.
|
|
238
234
|
*
|
|
239
|
-
*
|
|
235
|
+
* Example:
|
|
240
236
|
* ```ts
|
|
241
237
|
* container.register(ISecretStorage, {
|
|
242
238
|
* useExisting: PersistentSecretStorage,
|
|
@@ -253,7 +249,7 @@ interface ExistingProvider<Value> {
|
|
|
253
249
|
* An optional name to qualify this provider.
|
|
254
250
|
* If specified, the token must be resolved using the same name.
|
|
255
251
|
*
|
|
256
|
-
*
|
|
252
|
+
* Example:
|
|
257
253
|
* ```ts
|
|
258
254
|
* export class ExtensionContext {
|
|
259
255
|
* // Decorator-based injection
|
|
@@ -352,7 +348,7 @@ interface Container {
|
|
|
352
348
|
*/
|
|
353
349
|
createChild(options?: Partial<ChildContainerOptions>): Container;
|
|
354
350
|
/**
|
|
355
|
-
* Clears and returns all distinct values
|
|
351
|
+
* Clears and returns all distinct values cached by this container.
|
|
356
352
|
* Values from {@link ValueProvider} registrations are not included, as they are never cached.
|
|
357
353
|
*
|
|
358
354
|
* Note that only this container is affected. Parent or child containers, if any, remain unchanged.
|
|
@@ -646,7 +642,7 @@ declare function createContainer(options?: Partial<ContainerOptions>): Container
|
|
|
646
642
|
* Class decorator that enables auto-registration of an unregistered class
|
|
647
643
|
* when the class is first resolved from the container.
|
|
648
644
|
*
|
|
649
|
-
*
|
|
645
|
+
* Example:
|
|
650
646
|
* ```ts
|
|
651
647
|
* @AutoRegister()
|
|
652
648
|
* class Wizard {}
|
|
@@ -654,8 +650,6 @@ declare function createContainer(options?: Partial<ContainerOptions>): Container
|
|
|
654
650
|
* const wizard = container.resolve(Wizard);
|
|
655
651
|
* container.isRegistered(Wizard); // => true
|
|
656
652
|
* ```
|
|
657
|
-
*
|
|
658
|
-
* @__NO_SIDE_EFFECTS__
|
|
659
653
|
*/
|
|
660
654
|
declare function AutoRegister<This extends object>(): ClassDecorator<This>;
|
|
661
655
|
|
|
@@ -666,7 +660,7 @@ declare function AutoRegister<This extends object>(): ClassDecorator<This>;
|
|
|
666
660
|
* This causes the container to immediately create and cache the instance of the class,
|
|
667
661
|
* instead of deferring instantiation until the first resolution.
|
|
668
662
|
*
|
|
669
|
-
*
|
|
663
|
+
* Example:
|
|
670
664
|
* ```ts
|
|
671
665
|
* @EagerInstantiate()
|
|
672
666
|
* class Wizard {}
|
|
@@ -675,8 +669,6 @@ declare function AutoRegister<This extends object>(): ClassDecorator<This>;
|
|
|
675
669
|
* // is immediately created and cached by the container
|
|
676
670
|
* container.register(Wizard);
|
|
677
671
|
* ```
|
|
678
|
-
*
|
|
679
|
-
* @__NO_SIDE_EFFECTS__
|
|
680
672
|
*/
|
|
681
673
|
declare function EagerInstantiate<This extends object>(): ClassDecorator<This>;
|
|
682
674
|
|
|
@@ -701,15 +693,15 @@ declare function Inject<Value>(token: Token<Value>): ParameterDecorator;
|
|
|
701
693
|
/**
|
|
702
694
|
* Parameter decorator that injects the value associated with the given token.
|
|
703
695
|
*
|
|
704
|
-
*
|
|
705
|
-
* {@link tokenRef} helper function.
|
|
696
|
+
* This overload allows referencing a token declared later in the file by using
|
|
697
|
+
* the {@link tokenRef} helper function.
|
|
706
698
|
*
|
|
707
699
|
* Throws an error if:
|
|
708
700
|
* - The token is not registered in the container.
|
|
709
701
|
* - A circular dependency is detected. Use function injection with {@link injectBy}
|
|
710
702
|
* if resolving circular dependencies is necessary.
|
|
711
703
|
*
|
|
712
|
-
*
|
|
704
|
+
* Example:
|
|
713
705
|
* ```ts
|
|
714
706
|
* class Wizard {
|
|
715
707
|
* constructor(@Inject(tokenRef(() => Wand)) readonly wand: Wand) {}
|
|
@@ -721,40 +713,56 @@ declare function Inject<Value>(token: Token<Value>): ParameterDecorator;
|
|
|
721
713
|
declare function Inject<Value>(tokens: TokenRef<Value>): ParameterDecorator;
|
|
722
714
|
|
|
723
715
|
/**
|
|
724
|
-
* Class decorator that registers additional aliasing tokens for the decorated type
|
|
725
|
-
* when the type is first registered in the container.
|
|
716
|
+
* Class decorator that registers additional aliasing tokens for the decorated type.
|
|
726
717
|
*
|
|
727
|
-
* The
|
|
718
|
+
* The aliases are added using {@link ExistingProvider}(s) when the class is first
|
|
719
|
+
* registered in the container.
|
|
728
720
|
*
|
|
729
|
-
*
|
|
721
|
+
* Example:
|
|
730
722
|
* ```ts
|
|
731
723
|
* @Injectable(Weapon)
|
|
732
|
-
* class
|
|
724
|
+
* class Rifle {}
|
|
725
|
+
* ```
|
|
726
|
+
*
|
|
727
|
+
* Note that `@Injectable` decorators can be stacked to add multiple aliases.
|
|
728
|
+
*
|
|
729
|
+
* Example:
|
|
730
|
+
* ```ts
|
|
731
|
+
* @Injectable(Weapon)
|
|
732
|
+
* @Injectable(Gun) // Or just @Injectable(Weapon, Gun)
|
|
733
|
+
* class Rifle {}
|
|
733
734
|
* ```
|
|
734
735
|
*/
|
|
735
736
|
declare function Injectable<Value, This extends Value & object>(token: Token<Value>): ClassDecorator<This>;
|
|
736
|
-
declare function Injectable<VA, VB, This extends VA & VB & object>(tokenA: Token<VA>,
|
|
737
|
-
tokenB: Token<VB>): ClassDecorator<This>;
|
|
737
|
+
declare function Injectable<VA, VB, This extends VA & VB & object>(tokenA: Token<VA>, tokenB: Token<VB>): ClassDecorator<This>;
|
|
738
738
|
declare function Injectable<VA, VB, VC, This extends VA & VB & VC & object>(tokenA: Token<VA>, tokenB: Token<VB>, tokenC: Token<VC>): ClassDecorator<This>;
|
|
739
739
|
declare function Injectable<VA, VB, VC, VD, This extends VA & VB & VC & VD & object>(tokenA: Token<VA>, tokenB: Token<VB>, tokenC: Token<VC>, tokenD: Token<VD>): ClassDecorator<This>;
|
|
740
740
|
declare function Injectable<VA, VB, VC, VD, VE, This extends VA & VB & VC & VD & VE & object>(tokenA: Token<VA>, tokenB: Token<VB>, tokenC: Token<VC>, tokenD: Token<VD>, tokenE: Token<VE>): ClassDecorator<This>;
|
|
741
741
|
declare function Injectable<VA, VB, VC, VD, VE, VF, This extends VA & VB & VC & VD & VE & VF & object>(tokenA: Token<VA>, tokenB: Token<VB>, tokenC: Token<VC>, tokenD: Token<VD>, tokenE: Token<VE>, tokenF: Token<VF>): ClassDecorator<This>;
|
|
742
|
-
declare function Injectable<This extends object>(...tokens: Tokens<unknown>): ClassDecorator<This>;
|
|
743
742
|
/**
|
|
744
|
-
* Class decorator that registers additional aliasing
|
|
745
|
-
* when the type is first registered in the container.
|
|
743
|
+
* Class decorator that registers an additional aliasing token for the decorated type.
|
|
746
744
|
*
|
|
747
|
-
* The
|
|
745
|
+
* The alias is added using an {@link ExistingProvider} when the class is first
|
|
746
|
+
* registered in the container.
|
|
748
747
|
*
|
|
749
|
-
*
|
|
750
|
-
* the {@link tokenRef} helper function.
|
|
748
|
+
* This overload allows referencing tokens that are declared later in the file
|
|
749
|
+
* by using the {@link tokenRef} helper function.
|
|
751
750
|
*
|
|
752
|
-
*
|
|
751
|
+
* Example:
|
|
753
752
|
* ```ts
|
|
754
|
-
* @Injectable(tokenRef() => Weapon) // Weapon is declared after
|
|
755
|
-
* class
|
|
753
|
+
* @Injectable(tokenRef(() => Weapon)) // Weapon is declared after Rifle
|
|
754
|
+
* class Rifle {}
|
|
756
755
|
* // Other code...
|
|
757
|
-
*
|
|
756
|
+
* const Weapon = createType("Weapon");
|
|
757
|
+
* ```
|
|
758
|
+
*
|
|
759
|
+
* Note that `@Injectable` decorators can be stacked to add multiple aliases.
|
|
760
|
+
*
|
|
761
|
+
* Example:
|
|
762
|
+
* ```ts
|
|
763
|
+
* @Injectable(tokenRef(() => Weapon))
|
|
764
|
+
* @Injectable(Gun)
|
|
765
|
+
* class Rifle {}
|
|
758
766
|
* ```
|
|
759
767
|
*/
|
|
760
768
|
declare function Injectable<Value, This extends Value & object>(tokens: TokenRef<Value>): ClassDecorator<This>;
|
|
@@ -781,14 +789,14 @@ declare function InjectAll<Value>(token: Token<Value>): ParameterDecorator;
|
|
|
781
789
|
* Parameter decorator that injects all values provided by the registrations
|
|
782
790
|
* associated with the given token.
|
|
783
791
|
*
|
|
784
|
-
*
|
|
785
|
-
* {@link tokenRef} helper function.
|
|
792
|
+
* This overload allows referencing a token declared later in the file by using
|
|
793
|
+
* the {@link tokenRef} helper function.
|
|
786
794
|
*
|
|
787
795
|
* Throws an error if:
|
|
788
796
|
* - The token is not registered in the container.
|
|
789
797
|
* - A circular dependency is detected.
|
|
790
798
|
*
|
|
791
|
-
*
|
|
799
|
+
* Example:
|
|
792
800
|
* ```ts
|
|
793
801
|
* class Wizard {
|
|
794
802
|
* constructor(@InjectAll(tokenRef(() => Wand)) readonly wands: Wand[]) {}
|
|
@@ -805,7 +813,7 @@ declare function InjectAll<Value>(tokens: TokenRef<Value>): ParameterDecorator;
|
|
|
805
813
|
* This allows the container to distinguish between multiple implementations
|
|
806
814
|
* of the same interface or type during registration and injection.
|
|
807
815
|
*
|
|
808
|
-
*
|
|
816
|
+
* Example:
|
|
809
817
|
* ```ts
|
|
810
818
|
* @Named("dumbledore")
|
|
811
819
|
* class Dumbledore implements Wizard {}
|
|
@@ -814,8 +822,6 @@ declare function InjectAll<Value>(tokens: TokenRef<Value>): ParameterDecorator;
|
|
|
814
822
|
* container.register(IWizard, { useClass: Dumbledore });
|
|
815
823
|
* const dumbledore = container.resolve(IWizard, "dumbledore");
|
|
816
824
|
* ```
|
|
817
|
-
*
|
|
818
|
-
* @__NO_SIDE_EFFECTS__
|
|
819
825
|
*/
|
|
820
826
|
declare function Named<This extends object>(name: string): ClassDecorator<This> & ParameterDecorator;
|
|
821
827
|
|
|
@@ -839,13 +845,13 @@ declare function Optional<Value>(token: Token<Value>): ParameterDecorator;
|
|
|
839
845
|
* Parameter decorator that injects the value associated with the given token,
|
|
840
846
|
* or `undefined` if the token is not registered in the container.
|
|
841
847
|
*
|
|
842
|
-
*
|
|
843
|
-
* {@link tokenRef} helper function.
|
|
848
|
+
* This overload allows referencing a token declared later in the file by using
|
|
849
|
+
* the {@link tokenRef} helper function.
|
|
844
850
|
*
|
|
845
851
|
* Throws an error if a circular dependency is detected. Use function injection
|
|
846
852
|
* with {@link optionalBy} if resolving circular dependencies is necessary.
|
|
847
853
|
*
|
|
848
|
-
*
|
|
854
|
+
* Example:
|
|
849
855
|
* ```ts
|
|
850
856
|
* class Wizard {
|
|
851
857
|
* constructor(@Optional(tokenRef(() => Wand)) readonly wand: Wand | undefined) {}
|
|
@@ -877,12 +883,12 @@ declare function OptionalAll<Value>(token: Token<Value>): ParameterDecorator;
|
|
|
877
883
|
* associated with the given token or an empty array if the token is not registered
|
|
878
884
|
* in the container.
|
|
879
885
|
*
|
|
880
|
-
*
|
|
881
|
-
* {@link tokenRef} helper function.
|
|
886
|
+
* This overload allows referencing a token declared later in the file by using
|
|
887
|
+
* the {@link tokenRef} helper function.
|
|
882
888
|
*
|
|
883
889
|
* Throws an error if a circular dependency is detected.
|
|
884
890
|
*
|
|
885
|
-
*
|
|
891
|
+
* Example:
|
|
886
892
|
* ```ts
|
|
887
893
|
* class Wizard {
|
|
888
894
|
* constructor(@OptionalAll(tokenRef(() => Wand)) readonly wands: Wand[]) {}
|
|
@@ -898,7 +904,7 @@ declare function OptionalAll<Value>(tokens: TokenRef<Value>): ParameterDecorator
|
|
|
898
904
|
*
|
|
899
905
|
* The scope set by this decorator can be overridden by explicit registration options, if provided.
|
|
900
906
|
*
|
|
901
|
-
*
|
|
907
|
+
* Example:
|
|
902
908
|
* ```ts
|
|
903
909
|
* @Scoped("Container")
|
|
904
910
|
* class Wizard {}
|
|
@@ -912,8 +918,6 @@ declare function OptionalAll<Value>(tokens: TokenRef<Value>): ParameterDecorator
|
|
|
912
918
|
* { scope: "Container" },
|
|
913
919
|
* );
|
|
914
920
|
* ```
|
|
915
|
-
*
|
|
916
|
-
* @__NO_SIDE_EFFECTS__
|
|
917
921
|
*/
|
|
918
922
|
declare function Scoped<This extends object>(scope: Scope): ClassDecorator<This>;
|
|
919
923
|
|
|
@@ -961,7 +965,7 @@ declare function injectAll<Value>(token: Token<Value>): Value[];
|
|
|
961
965
|
* Compared to {@link inject}, `injectBy` accepts a `thisArg` argument
|
|
962
966
|
* (e.g., the containing class instance) which is used to resolve circular dependencies.
|
|
963
967
|
*
|
|
964
|
-
*
|
|
968
|
+
* Example:
|
|
965
969
|
* ```ts
|
|
966
970
|
* class Wand {
|
|
967
971
|
* owner = inject(Wizard);
|
|
@@ -985,7 +989,7 @@ declare function injectBy<Instance extends object>(thisArg: any, Class: Construc
|
|
|
985
989
|
* Compared to {@link inject}, `injectBy` accepts a `thisArg` argument
|
|
986
990
|
* (e.g., the containing class instance) which is used to resolve circular dependencies.
|
|
987
991
|
*
|
|
988
|
-
*
|
|
992
|
+
* Example:
|
|
989
993
|
* ```ts
|
|
990
994
|
* class Wand {
|
|
991
995
|
* owner = inject(Wizard);
|
|
@@ -1014,7 +1018,7 @@ declare function assertInjectionContext(fn: Function | string): void;
|
|
|
1014
1018
|
/**
|
|
1015
1019
|
* Allows performing injections outside the normal injection context window.
|
|
1016
1020
|
*
|
|
1017
|
-
*
|
|
1021
|
+
* Example:
|
|
1018
1022
|
* ```ts
|
|
1019
1023
|
* class Wizard {
|
|
1020
1024
|
* private injector = inject(Injector);
|
|
@@ -1024,7 +1028,7 @@ declare function assertInjectionContext(fn: Function | string): void;
|
|
|
1024
1028
|
*
|
|
1025
1029
|
* getWand(): Wand {
|
|
1026
1030
|
* // An injection context does not exist here, but the
|
|
1027
|
-
* // Injector instance retains and
|
|
1031
|
+
* // Injector instance retains and reuses the context
|
|
1028
1032
|
* // that was present at the time of its injection
|
|
1029
1033
|
* return (this.wand ??= this.injector.inject(Wand));
|
|
1030
1034
|
* }
|
|
@@ -1091,7 +1095,7 @@ interface Injector {
|
|
|
1091
1095
|
/**
|
|
1092
1096
|
* Allows performing injections outside the normal injection context window.
|
|
1093
1097
|
*
|
|
1094
|
-
*
|
|
1098
|
+
* Example:
|
|
1095
1099
|
* ```ts
|
|
1096
1100
|
* class Wizard {
|
|
1097
1101
|
* private injector = inject(Injector);
|
|
@@ -1101,7 +1105,7 @@ interface Injector {
|
|
|
1101
1105
|
*
|
|
1102
1106
|
* getWand(): Wand {
|
|
1103
1107
|
* // An injection context does not exist here, but the
|
|
1104
|
-
* // Injector instance retains and
|
|
1108
|
+
* // Injector instance retains and reuses the context
|
|
1105
1109
|
* // that was present at the time of its injection
|
|
1106
1110
|
* return (this.wand ??= this.injector.inject(Wand));
|
|
1107
1111
|
* }
|
package/dist/cjs/index.js
CHANGED
|
@@ -33,9 +33,8 @@ function throwResolutionError(tokenInfo, aliases, cause) {
|
|
|
33
33
|
// @internal
|
|
34
34
|
function throwParameterResolutionError(ctor, methodKey, dependency, cause) {
|
|
35
35
|
const location = getLocation(ctor, methodKey);
|
|
36
|
-
const [token] = dependency.tokenRef.getRefTokens();
|
|
37
36
|
const tokenName = getFullTokenName([
|
|
38
|
-
|
|
37
|
+
dependency.tokenRef.getRefToken(),
|
|
39
38
|
dependency.name
|
|
40
39
|
]);
|
|
41
40
|
const msg = tag(`failed to resolve dependency for ${location}(parameter #${dependency.index}: ${tokenName})`);
|
|
@@ -219,21 +218,21 @@ function injectBy(thisArg, token, name) {
|
|
|
219
218
|
|
|
220
219
|
/**
|
|
221
220
|
* Allows referencing a class declared later in the file by wrapping it
|
|
222
|
-
*
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
*/ function classRef(Class) {
|
|
221
|
+
* into a lazily evaluated function.
|
|
222
|
+
*/ // @__NO_SIDE_EFFECTS__
|
|
223
|
+
function classRef(Class) {
|
|
226
224
|
return {
|
|
227
225
|
getRefClass: ()=>Class()
|
|
228
226
|
};
|
|
229
227
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
* into a lazily evaluated function.
|
|
233
|
-
*
|
|
234
|
-
* @__NO_SIDE_EFFECTS__
|
|
235
|
-
*/ function tokenRef(token) {
|
|
228
|
+
// @__NO_SIDE_EFFECTS__
|
|
229
|
+
function tokenRef(token) {
|
|
236
230
|
return {
|
|
231
|
+
getRefToken: ()=>{
|
|
232
|
+
const tokenOrTokens = token();
|
|
233
|
+
check(!Array.isArray(tokenOrTokens), "internal error: single token expected");
|
|
234
|
+
return tokenOrTokens;
|
|
235
|
+
},
|
|
237
236
|
getRefTokens: ()=>{
|
|
238
237
|
// Normalize the single token here so that we don't have to do it at every getRefTokens call site
|
|
239
238
|
const tokenOrTokens = token();
|
|
@@ -250,7 +249,7 @@ function isClassRef(value) {
|
|
|
250
249
|
}
|
|
251
250
|
// @internal
|
|
252
251
|
function isTokenRef(value) {
|
|
253
|
-
return value != null && typeof value === "object" && typeof value.
|
|
252
|
+
return value != null && typeof value === "object" && typeof value.getRefToken === "function";
|
|
254
253
|
}
|
|
255
254
|
|
|
256
255
|
// @internal
|
|
@@ -261,6 +260,10 @@ class Metadata {
|
|
|
261
260
|
methods: new Map()
|
|
262
261
|
};
|
|
263
262
|
this.tokenRef = {
|
|
263
|
+
// prettier-ignore
|
|
264
|
+
getRefToken: ()=>{
|
|
265
|
+
check(false, "internal error: unexpected call");
|
|
266
|
+
},
|
|
264
267
|
getRefTokens: ()=>new Set()
|
|
265
268
|
};
|
|
266
269
|
this.provider = {
|
|
@@ -1055,7 +1058,7 @@ function isDisposable(value) {
|
|
|
1055
1058
|
}
|
|
1056
1059
|
// Call context: decorator-based injection
|
|
1057
1060
|
resolveDependency(dependency, instance) {
|
|
1058
|
-
const
|
|
1061
|
+
const token = dependency.tokenRef.getRefToken();
|
|
1059
1062
|
check(token, `token passed to @${dependency.appliedBy} was undefined (possible circular imports)`);
|
|
1060
1063
|
const name = dependency.name;
|
|
1061
1064
|
switch(dependency.appliedBy){
|
|
@@ -1094,7 +1097,7 @@ function isDisposable(value) {
|
|
|
1094
1097
|
* Class decorator that enables auto-registration of an unregistered class
|
|
1095
1098
|
* when the class is first resolved from the container.
|
|
1096
1099
|
*
|
|
1097
|
-
*
|
|
1100
|
+
* Example:
|
|
1098
1101
|
* ```ts
|
|
1099
1102
|
* @AutoRegister()
|
|
1100
1103
|
* class Wizard {}
|
|
@@ -1102,9 +1105,8 @@ function isDisposable(value) {
|
|
|
1102
1105
|
* const wizard = container.resolve(Wizard);
|
|
1103
1106
|
* container.isRegistered(Wizard); // => true
|
|
1104
1107
|
* ```
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
*/ function AutoRegister() {
|
|
1108
|
+
*/ // @__NO_SIDE_EFFECTS__
|
|
1109
|
+
function AutoRegister() {
|
|
1108
1110
|
return function(Class) {
|
|
1109
1111
|
const metadata = getMetadata(Class);
|
|
1110
1112
|
metadata.autoRegister = true;
|
|
@@ -1118,7 +1120,7 @@ function isDisposable(value) {
|
|
|
1118
1120
|
* This causes the container to immediately create and cache the instance of the class,
|
|
1119
1121
|
* instead of deferring instantiation until the first resolution.
|
|
1120
1122
|
*
|
|
1121
|
-
*
|
|
1123
|
+
* Example:
|
|
1122
1124
|
* ```ts
|
|
1123
1125
|
* @EagerInstantiate()
|
|
1124
1126
|
* class Wizard {}
|
|
@@ -1127,9 +1129,8 @@ function isDisposable(value) {
|
|
|
1127
1129
|
* // is immediately created and cached by the container
|
|
1128
1130
|
* container.register(Wizard);
|
|
1129
1131
|
* ```
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
*/ function EagerInstantiate() {
|
|
1132
|
+
*/ // @__NO_SIDE_EFFECTS__
|
|
1133
|
+
function EagerInstantiate() {
|
|
1133
1134
|
return function(Class) {
|
|
1134
1135
|
const metadata = getMetadata(Class);
|
|
1135
1136
|
const currentScope = metadata.scope;
|
|
@@ -1163,10 +1164,11 @@ function Injectable(...args) {
|
|
|
1163
1164
|
const metadata = getMetadata(Class);
|
|
1164
1165
|
const arg0 = args[0];
|
|
1165
1166
|
const ref = isTokenRef(arg0) ? arg0 : tokenRef(()=>args);
|
|
1166
|
-
const
|
|
1167
|
+
const currentRef = metadata.tokenRef;
|
|
1167
1168
|
metadata.tokenRef = {
|
|
1169
|
+
getRefToken: ()=>currentRef.getRefToken(),
|
|
1168
1170
|
getRefTokens: ()=>{
|
|
1169
|
-
const existingTokens =
|
|
1171
|
+
const existingTokens = currentRef.getRefTokens();
|
|
1170
1172
|
for (const token of ref.getRefTokens()){
|
|
1171
1173
|
existingTokens.add(token);
|
|
1172
1174
|
}
|
|
@@ -1194,7 +1196,7 @@ function InjectAll(token) {
|
|
|
1194
1196
|
* This allows the container to distinguish between multiple implementations
|
|
1195
1197
|
* of the same interface or type during registration and injection.
|
|
1196
1198
|
*
|
|
1197
|
-
*
|
|
1199
|
+
* Example:
|
|
1198
1200
|
* ```ts
|
|
1199
1201
|
* @Named("dumbledore")
|
|
1200
1202
|
* class Dumbledore implements Wizard {}
|
|
@@ -1203,9 +1205,8 @@ function InjectAll(token) {
|
|
|
1203
1205
|
* container.register(IWizard, { useClass: Dumbledore });
|
|
1204
1206
|
* const dumbledore = container.resolve(IWizard, "dumbledore");
|
|
1205
1207
|
* ```
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
*/ function Named(name) {
|
|
1208
|
+
*/ // @__NO_SIDE_EFFECTS__
|
|
1209
|
+
function Named(name) {
|
|
1209
1210
|
check(name.trim(), "@Named qualifier must not be empty");
|
|
1210
1211
|
return function(target, propertyKey, parameterIndex) {
|
|
1211
1212
|
if (parameterIndex === undefined) {
|
|
@@ -1257,7 +1258,7 @@ function OptionalAll(token) {
|
|
|
1257
1258
|
*
|
|
1258
1259
|
* The scope set by this decorator can be overridden by explicit registration options, if provided.
|
|
1259
1260
|
*
|
|
1260
|
-
*
|
|
1261
|
+
* Example:
|
|
1261
1262
|
* ```ts
|
|
1262
1263
|
* @Scoped("Container")
|
|
1263
1264
|
* class Wizard {}
|
|
@@ -1271,9 +1272,8 @@ function OptionalAll(token) {
|
|
|
1271
1272
|
* { scope: "Container" },
|
|
1272
1273
|
* );
|
|
1273
1274
|
* ```
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
*/ function Scoped(scope) {
|
|
1275
|
+
*/ // @__NO_SIDE_EFFECTS__
|
|
1276
|
+
function Scoped(scope) {
|
|
1277
1277
|
return function(Class) {
|
|
1278
1278
|
const metadata = getMetadata(Class);
|
|
1279
1279
|
const currentScope = metadata.scope;
|
|
@@ -1293,7 +1293,7 @@ function OptionalAll(token) {
|
|
|
1293
1293
|
/**
|
|
1294
1294
|
* Allows performing injections outside the normal injection context window.
|
|
1295
1295
|
*
|
|
1296
|
-
*
|
|
1296
|
+
* Example:
|
|
1297
1297
|
* ```ts
|
|
1298
1298
|
* class Wizard {
|
|
1299
1299
|
* private injector = inject(Injector);
|
|
@@ -1303,13 +1303,13 @@ function OptionalAll(token) {
|
|
|
1303
1303
|
*
|
|
1304
1304
|
* getWand(): Wand {
|
|
1305
1305
|
* // An injection context does not exist here, but the
|
|
1306
|
-
* // Injector instance retains and
|
|
1306
|
+
* // Injector instance retains and reuses the context
|
|
1307
1307
|
* // that was present at the time of its injection
|
|
1308
1308
|
* return (this.wand ??= this.injector.inject(Wand));
|
|
1309
1309
|
* }
|
|
1310
1310
|
* }
|
|
1311
1311
|
* ```
|
|
1312
|
-
*/ const Injector =
|
|
1312
|
+
*/ const Injector = /* @__PURE__ */ build(()=>{
|
|
1313
1313
|
const context = ensureInjectionContext("Injector factory");
|
|
1314
1314
|
const runInContext = (fn)=>{
|
|
1315
1315
|
if (useInjectionContext()) {
|