@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/es/index.d.mts
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/es/index.mjs
CHANGED
|
@@ -31,9 +31,8 @@ function throwResolutionError(tokenInfo, aliases, cause) {
|
|
|
31
31
|
// @internal
|
|
32
32
|
function throwParameterResolutionError(ctor, methodKey, dependency, cause) {
|
|
33
33
|
const location = getLocation(ctor, methodKey);
|
|
34
|
-
const [token] = dependency.tokenRef.getRefTokens();
|
|
35
34
|
const tokenName = getFullTokenName([
|
|
36
|
-
|
|
35
|
+
dependency.tokenRef.getRefToken(),
|
|
37
36
|
dependency.name
|
|
38
37
|
]);
|
|
39
38
|
const msg = tag(`failed to resolve dependency for ${location}(parameter #${dependency.index}: ${tokenName})`);
|
|
@@ -217,21 +216,21 @@ function injectBy(thisArg, token, name) {
|
|
|
217
216
|
|
|
218
217
|
/**
|
|
219
218
|
* Allows referencing a class declared later in the file by wrapping it
|
|
220
|
-
*
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
*/ function classRef(Class) {
|
|
219
|
+
* into a lazily evaluated function.
|
|
220
|
+
*/ // @__NO_SIDE_EFFECTS__
|
|
221
|
+
function classRef(Class) {
|
|
224
222
|
return {
|
|
225
223
|
getRefClass: ()=>Class()
|
|
226
224
|
};
|
|
227
225
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
* into a lazily evaluated function.
|
|
231
|
-
*
|
|
232
|
-
* @__NO_SIDE_EFFECTS__
|
|
233
|
-
*/ function tokenRef(token) {
|
|
226
|
+
// @__NO_SIDE_EFFECTS__
|
|
227
|
+
function tokenRef(token) {
|
|
234
228
|
return {
|
|
229
|
+
getRefToken: ()=>{
|
|
230
|
+
const tokenOrTokens = token();
|
|
231
|
+
check(!Array.isArray(tokenOrTokens), "internal error: single token expected");
|
|
232
|
+
return tokenOrTokens;
|
|
233
|
+
},
|
|
235
234
|
getRefTokens: ()=>{
|
|
236
235
|
// Normalize the single token here so that we don't have to do it at every getRefTokens call site
|
|
237
236
|
const tokenOrTokens = token();
|
|
@@ -248,7 +247,7 @@ function isClassRef(value) {
|
|
|
248
247
|
}
|
|
249
248
|
// @internal
|
|
250
249
|
function isTokenRef(value) {
|
|
251
|
-
return value != null && typeof value === "object" && typeof value.
|
|
250
|
+
return value != null && typeof value === "object" && typeof value.getRefToken === "function";
|
|
252
251
|
}
|
|
253
252
|
|
|
254
253
|
// @internal
|
|
@@ -259,6 +258,10 @@ class Metadata {
|
|
|
259
258
|
methods: new Map()
|
|
260
259
|
};
|
|
261
260
|
this.tokenRef = {
|
|
261
|
+
// prettier-ignore
|
|
262
|
+
getRefToken: ()=>{
|
|
263
|
+
check(false, "internal error: unexpected call");
|
|
264
|
+
},
|
|
262
265
|
getRefTokens: ()=>new Set()
|
|
263
266
|
};
|
|
264
267
|
this.provider = {
|
|
@@ -1053,7 +1056,7 @@ function isDisposable(value) {
|
|
|
1053
1056
|
}
|
|
1054
1057
|
// Call context: decorator-based injection
|
|
1055
1058
|
resolveDependency(dependency, instance) {
|
|
1056
|
-
const
|
|
1059
|
+
const token = dependency.tokenRef.getRefToken();
|
|
1057
1060
|
check(token, `token passed to @${dependency.appliedBy} was undefined (possible circular imports)`);
|
|
1058
1061
|
const name = dependency.name;
|
|
1059
1062
|
switch(dependency.appliedBy){
|
|
@@ -1092,7 +1095,7 @@ function isDisposable(value) {
|
|
|
1092
1095
|
* Class decorator that enables auto-registration of an unregistered class
|
|
1093
1096
|
* when the class is first resolved from the container.
|
|
1094
1097
|
*
|
|
1095
|
-
*
|
|
1098
|
+
* Example:
|
|
1096
1099
|
* ```ts
|
|
1097
1100
|
* @AutoRegister()
|
|
1098
1101
|
* class Wizard {}
|
|
@@ -1100,9 +1103,8 @@ function isDisposable(value) {
|
|
|
1100
1103
|
* const wizard = container.resolve(Wizard);
|
|
1101
1104
|
* container.isRegistered(Wizard); // => true
|
|
1102
1105
|
* ```
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
*/ function AutoRegister() {
|
|
1106
|
+
*/ // @__NO_SIDE_EFFECTS__
|
|
1107
|
+
function AutoRegister() {
|
|
1106
1108
|
return function(Class) {
|
|
1107
1109
|
const metadata = getMetadata(Class);
|
|
1108
1110
|
metadata.autoRegister = true;
|
|
@@ -1116,7 +1118,7 @@ function isDisposable(value) {
|
|
|
1116
1118
|
* This causes the container to immediately create and cache the instance of the class,
|
|
1117
1119
|
* instead of deferring instantiation until the first resolution.
|
|
1118
1120
|
*
|
|
1119
|
-
*
|
|
1121
|
+
* Example:
|
|
1120
1122
|
* ```ts
|
|
1121
1123
|
* @EagerInstantiate()
|
|
1122
1124
|
* class Wizard {}
|
|
@@ -1125,9 +1127,8 @@ function isDisposable(value) {
|
|
|
1125
1127
|
* // is immediately created and cached by the container
|
|
1126
1128
|
* container.register(Wizard);
|
|
1127
1129
|
* ```
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
*/ function EagerInstantiate() {
|
|
1130
|
+
*/ // @__NO_SIDE_EFFECTS__
|
|
1131
|
+
function EagerInstantiate() {
|
|
1131
1132
|
return function(Class) {
|
|
1132
1133
|
const metadata = getMetadata(Class);
|
|
1133
1134
|
const currentScope = metadata.scope;
|
|
@@ -1161,10 +1162,11 @@ function Injectable(...args) {
|
|
|
1161
1162
|
const metadata = getMetadata(Class);
|
|
1162
1163
|
const arg0 = args[0];
|
|
1163
1164
|
const ref = isTokenRef(arg0) ? arg0 : tokenRef(()=>args);
|
|
1164
|
-
const
|
|
1165
|
+
const currentRef = metadata.tokenRef;
|
|
1165
1166
|
metadata.tokenRef = {
|
|
1167
|
+
getRefToken: ()=>currentRef.getRefToken(),
|
|
1166
1168
|
getRefTokens: ()=>{
|
|
1167
|
-
const existingTokens =
|
|
1169
|
+
const existingTokens = currentRef.getRefTokens();
|
|
1168
1170
|
for (const token of ref.getRefTokens()){
|
|
1169
1171
|
existingTokens.add(token);
|
|
1170
1172
|
}
|
|
@@ -1192,7 +1194,7 @@ function InjectAll(token) {
|
|
|
1192
1194
|
* This allows the container to distinguish between multiple implementations
|
|
1193
1195
|
* of the same interface or type during registration and injection.
|
|
1194
1196
|
*
|
|
1195
|
-
*
|
|
1197
|
+
* Example:
|
|
1196
1198
|
* ```ts
|
|
1197
1199
|
* @Named("dumbledore")
|
|
1198
1200
|
* class Dumbledore implements Wizard {}
|
|
@@ -1201,9 +1203,8 @@ function InjectAll(token) {
|
|
|
1201
1203
|
* container.register(IWizard, { useClass: Dumbledore });
|
|
1202
1204
|
* const dumbledore = container.resolve(IWizard, "dumbledore");
|
|
1203
1205
|
* ```
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
*/ function Named(name) {
|
|
1206
|
+
*/ // @__NO_SIDE_EFFECTS__
|
|
1207
|
+
function Named(name) {
|
|
1207
1208
|
check(name.trim(), "@Named qualifier must not be empty");
|
|
1208
1209
|
return function(target, propertyKey, parameterIndex) {
|
|
1209
1210
|
if (parameterIndex === undefined) {
|
|
@@ -1255,7 +1256,7 @@ function OptionalAll(token) {
|
|
|
1255
1256
|
*
|
|
1256
1257
|
* The scope set by this decorator can be overridden by explicit registration options, if provided.
|
|
1257
1258
|
*
|
|
1258
|
-
*
|
|
1259
|
+
* Example:
|
|
1259
1260
|
* ```ts
|
|
1260
1261
|
* @Scoped("Container")
|
|
1261
1262
|
* class Wizard {}
|
|
@@ -1269,9 +1270,8 @@ function OptionalAll(token) {
|
|
|
1269
1270
|
* { scope: "Container" },
|
|
1270
1271
|
* );
|
|
1271
1272
|
* ```
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
*/ function Scoped(scope) {
|
|
1273
|
+
*/ // @__NO_SIDE_EFFECTS__
|
|
1274
|
+
function Scoped(scope) {
|
|
1275
1275
|
return function(Class) {
|
|
1276
1276
|
const metadata = getMetadata(Class);
|
|
1277
1277
|
const currentScope = metadata.scope;
|
|
@@ -1291,7 +1291,7 @@ function OptionalAll(token) {
|
|
|
1291
1291
|
/**
|
|
1292
1292
|
* Allows performing injections outside the normal injection context window.
|
|
1293
1293
|
*
|
|
1294
|
-
*
|
|
1294
|
+
* Example:
|
|
1295
1295
|
* ```ts
|
|
1296
1296
|
* class Wizard {
|
|
1297
1297
|
* private injector = inject(Injector);
|
|
@@ -1301,13 +1301,13 @@ function OptionalAll(token) {
|
|
|
1301
1301
|
*
|
|
1302
1302
|
* getWand(): Wand {
|
|
1303
1303
|
* // An injection context does not exist here, but the
|
|
1304
|
-
* // Injector instance retains and
|
|
1304
|
+
* // Injector instance retains and reuses the context
|
|
1305
1305
|
* // that was present at the time of its injection
|
|
1306
1306
|
* return (this.wand ??= this.injector.inject(Wand));
|
|
1307
1307
|
* }
|
|
1308
1308
|
* }
|
|
1309
1309
|
* ```
|
|
1310
|
-
*/ const Injector =
|
|
1310
|
+
*/ const Injector = /* @__PURE__ */ build(()=>{
|
|
1311
1311
|
const context = ensureInjectionContext("Injector factory");
|
|
1312
1312
|
const runInContext = (fn)=>{
|
|
1313
1313
|
if (useInjectionContext()) {
|