async-injection 2.3.0 → 3.0.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 +133 -194
- package/lib/async-factory-provider.d.ts +1 -1
- package/lib/bindable-provider.d.ts +2 -2
- package/lib/binding.d.ts +80 -0
- package/lib/cjs/async-factory-provider.js.map +1 -1
- package/lib/cjs/bindable-provider.js +5 -18
- package/lib/cjs/bindable-provider.js.map +1 -1
- package/lib/cjs/{binder.js → binding.js} +1 -1
- package/lib/cjs/binding.js.map +1 -0
- package/lib/cjs/class-provider.js +142 -39
- package/lib/cjs/class-provider.js.map +1 -1
- package/lib/cjs/container.js +62 -22
- package/lib/cjs/container.js.map +1 -1
- package/lib/cjs/decorators.js +61 -17
- package/lib/cjs/decorators.js.map +1 -1
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/injector.js.map +1 -1
- package/lib/cjs/provider.js.map +1 -1
- package/lib/cjs/sync-factory-provider.js.map +1 -1
- package/lib/class-provider.d.ts +16 -2
- package/lib/container.d.ts +40 -14
- package/lib/decorators.d.ts +15 -2
- package/lib/esm/async-factory-provider.js.map +1 -1
- package/lib/esm/bindable-provider.js +5 -18
- package/lib/esm/bindable-provider.js.map +1 -1
- package/lib/esm/binding.js +2 -0
- package/lib/esm/binding.js.map +1 -0
- package/lib/esm/class-provider.js +143 -40
- package/lib/esm/class-provider.js.map +1 -1
- package/lib/esm/container.js +62 -22
- package/lib/esm/container.js.map +1 -1
- package/lib/esm/decorators.js +60 -18
- package/lib/esm/decorators.js.map +1 -1
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/injector.js.map +1 -1
- package/lib/esm/provider.js.map +1 -1
- package/lib/esm/sync-factory-provider.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/injector.d.ts +1 -1
- package/lib/sync-factory-provider.d.ts +1 -1
- package/package.json +1 -1
- package/lib/binder.d.ts +0 -110
- package/lib/cjs/binder.js.map +0 -1
- package/lib/esm/binder.js +0 -2
- package/lib/esm/binder.js.map +0 -1
package/lib/esm/decorators.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* These decorators all apply the information they collect (whether class, method, or parameter data) as tagged metadata on the class's constructor
|
|
3
3
|
*/
|
|
4
|
-
import { INJECT_METADATA_KEY, INJECTABLE_METADATA_KEY, OPTIONAL_METADATA_KEY, POSTCONSTRUCT_ASYNC_METADATA_KEY, POSTCONSTRUCT_SYNC_METADATA_KEY, REFLECT_RETURN, RELEASE_METADATA_KEY } from './constants.js';
|
|
4
|
+
import { INJECT_METADATA_KEY, INJECTABLE_METADATA_KEY, OPTIONAL_METADATA_KEY, POSTCONSTRUCT_ASYNC_METADATA_KEY, POSTCONSTRUCT_SYNC_METADATA_KEY, REFLECT_PARAMS, REFLECT_RETURN, RELEASE_METADATA_KEY } from './constants.js';
|
|
5
5
|
// Help user locate misapplied decorators.
|
|
6
6
|
function targetHint(target) {
|
|
7
7
|
let hint;
|
|
@@ -24,6 +24,10 @@ function isClassConstructor(target) {
|
|
|
24
24
|
function makeParamIdxKey(idx) {
|
|
25
25
|
return `index-${idx}`;
|
|
26
26
|
}
|
|
27
|
+
// Key for @Inject/@Optional metadata stored on a method parameter.
|
|
28
|
+
function makeMethodParamIdxKey(methodName, idx) {
|
|
29
|
+
return `${String(methodName)}:index-${idx}`;
|
|
30
|
+
}
|
|
27
31
|
// Validate that the specified target is a parameter of a class constructor
|
|
28
32
|
function validateConstructorParam(decorator, target, idx) {
|
|
29
33
|
if (!isClassConstructor(target)) {
|
|
@@ -31,6 +35,16 @@ function validateConstructorParam(decorator, target, idx) {
|
|
|
31
35
|
}
|
|
32
36
|
return makeParamIdxKey(idx);
|
|
33
37
|
}
|
|
38
|
+
// Validate that the target is an instance method parameter (non-constructor).
|
|
39
|
+
function validateMethodParam(decorator, target, methodName, idx) {
|
|
40
|
+
if (typeof target !== 'object' || typeof target.constructor !== 'function') {
|
|
41
|
+
throw new Error('@' + decorator + ' is not valid here [' + targetHint(target) + ']');
|
|
42
|
+
}
|
|
43
|
+
if (!Reflect.hasOwnMetadata(REFLECT_PARAMS, target, methodName)) {
|
|
44
|
+
// REFLECT_PARAMS not yet set (decoration order), so we cannot pre-validate the method; accept silently.
|
|
45
|
+
}
|
|
46
|
+
return makeMethodParamIdxKey(methodName, idx);
|
|
47
|
+
}
|
|
34
48
|
// Validate the decorator was only applied once.
|
|
35
49
|
function validateSingleConstructorParam(decorator, target, idx) {
|
|
36
50
|
const propKey = validateConstructorParam(decorator, target, idx);
|
|
@@ -56,15 +70,16 @@ export function Injectable() {
|
|
|
56
70
|
};
|
|
57
71
|
}
|
|
58
72
|
/**
|
|
59
|
-
* Placed just before a constructor parameter, this parameter decorator allows for specificity and control over the type of
|
|
73
|
+
* Placed just before a constructor parameter, this parameter decorator allows for specificity and control over the type of Object that will be injected into the parameter.
|
|
60
74
|
* In the absence of this decorator the container will use whatever is bound to a parameter's type (or throw an error if it is unable to recognize the type).
|
|
75
|
+
* This decorator may also be placed on a parameter of a method annotated with @PostConstruct, in which case the container will resolve and inject the value before invoking that method.
|
|
61
76
|
*
|
|
62
77
|
* @param id The identifier of the bound type that should be injected.
|
|
63
78
|
*/
|
|
64
79
|
export function Inject(id) {
|
|
65
80
|
/**
|
|
66
|
-
* @param target The constructor function of the class (
|
|
67
|
-
* @param parameterName The name of the parameter
|
|
81
|
+
* @param target The constructor function of the class (for constructor params), or the class prototype (for method params).
|
|
82
|
+
* @param parameterName The name of the parameter (undefined for constructor params, the method name for method params)
|
|
68
83
|
* @param parameterIndex The ordinal index of the parameter in the function’s parameter list
|
|
69
84
|
* @returns Undefined (nothing), as this decorator does not modify the parameter in any way.
|
|
70
85
|
*/
|
|
@@ -72,8 +87,16 @@ export function Inject(id) {
|
|
|
72
87
|
if (id === undefined) {
|
|
73
88
|
throw new Error('Undefined id passed to @Inject [' + targetHint(target) + ']');
|
|
74
89
|
}
|
|
75
|
-
|
|
76
|
-
|
|
90
|
+
if (parameterName === undefined) {
|
|
91
|
+
// Constructor parameter
|
|
92
|
+
const paramKey = validateSingleConstructorParam('Inject', target, parameterIndex);
|
|
93
|
+
Reflect.defineMetadata(INJECT_METADATA_KEY, id, target, paramKey);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
// Method parameter — silently accepted (intended for @PostConstruct methods)
|
|
97
|
+
const paramKey = validateMethodParam('Inject', target, parameterName, parameterIndex);
|
|
98
|
+
Reflect.defineMetadata(INJECT_METADATA_KEY, id, target, paramKey);
|
|
99
|
+
}
|
|
77
100
|
};
|
|
78
101
|
}
|
|
79
102
|
/**
|
|
@@ -86,20 +109,35 @@ export function Inject(id) {
|
|
|
86
109
|
export function _getInjectedIdAt(target, parameterIndex) {
|
|
87
110
|
return Reflect.getMetadata(INJECT_METADATA_KEY, target, makeParamIdxKey(parameterIndex));
|
|
88
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Retrieve the @Inject metadata for a specifically indexed parameter of a named method.
|
|
114
|
+
*/
|
|
115
|
+
export function _getInjectedIdForMethod(prototype, methodName, parameterIndex) {
|
|
116
|
+
return Reflect.getMetadata(INJECT_METADATA_KEY, prototype, makeMethodParamIdxKey(methodName, parameterIndex));
|
|
117
|
+
}
|
|
89
118
|
/**
|
|
90
119
|
* Placed just before a constructor parameter, this parameter decorator signals the container that it should supply the 'alt' constant value (undefined by default) if for *any* reason it is unable to otherwise resolve the type of the parameter.
|
|
120
|
+
* This decorator may also be placed on a parameter of a method annotated with @PostConstruct.
|
|
91
121
|
* WARNING! It is your responsibility to ensure that alt is of the appropriate type/value.
|
|
92
122
|
*/
|
|
93
123
|
export function Optional(alt) {
|
|
94
124
|
/**
|
|
95
|
-
* @param target The constructor function of the class (
|
|
96
|
-
* @param parameterName The name of the parameter
|
|
125
|
+
* @param target The constructor function of the class (for constructor params), or the class prototype (for method params).
|
|
126
|
+
* @param parameterName The name of the parameter (undefined for constructor params, the method name for method params)
|
|
97
127
|
* @param parameterIndex The ordinal index of the parameter in the function’s parameter list
|
|
98
128
|
* @returns Undefined (nothing), as this decorator does not modify the parameter in any way.
|
|
99
129
|
*/
|
|
100
130
|
return function (target, parameterName, parameterIndex) {
|
|
101
|
-
|
|
102
|
-
|
|
131
|
+
if (parameterName === undefined) {
|
|
132
|
+
// Constructor parameter
|
|
133
|
+
const paramKey = validateSingleConstructorParam('Optional', target, parameterIndex);
|
|
134
|
+
Reflect.defineMetadata(OPTIONAL_METADATA_KEY, { value: alt }, target, paramKey);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
// Method parameter — silently accepted (intended for @PostConstruct methods)
|
|
138
|
+
const paramKey = validateMethodParam('Optional', target, parameterName, parameterIndex);
|
|
139
|
+
Reflect.defineMetadata(OPTIONAL_METADATA_KEY, { value: alt }, target, paramKey);
|
|
140
|
+
}
|
|
103
141
|
};
|
|
104
142
|
}
|
|
105
143
|
/**
|
|
@@ -113,20 +151,26 @@ export function Optional(alt) {
|
|
|
113
151
|
export function _getOptionalDefaultAt(target, parameterIndex) {
|
|
114
152
|
return Reflect.getMetadata(OPTIONAL_METADATA_KEY, target, makeParamIdxKey(parameterIndex)); // See the @Optional decorator before making any changes here.
|
|
115
153
|
}
|
|
154
|
+
/**
|
|
155
|
+
* Retrieve the @Optional metadata for a specifically indexed parameter of a named method.
|
|
156
|
+
*/
|
|
157
|
+
export function _getOptionalDefaultForMethod(prototype, methodName, parameterIndex) {
|
|
158
|
+
return Reflect.getMetadata(OPTIONAL_METADATA_KEY, prototype, makeMethodParamIdxKey(methodName, parameterIndex));
|
|
159
|
+
}
|
|
116
160
|
/**
|
|
117
161
|
* Placed just before a class method, this method decorator flags a method that should be called after an object has been instantiated by the container, but before it is put into service.
|
|
118
|
-
* The method will be assumed to be synchronous unless the method signature explicitly declares
|
|
162
|
+
* The method will be assumed to be synchronous unless the method signature explicitly declares its return type to be ": Promise<something>".
|
|
163
|
+
* Parameters will be resolved by the container just as they are for constructors.
|
|
119
164
|
* This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.
|
|
120
165
|
*/
|
|
121
166
|
export function PostConstruct() {
|
|
122
167
|
/**
|
|
123
168
|
* @param prototypeOrConstructor The prototype of the class (we don't allow @PostConstruct on anything other than a class instance method.
|
|
124
169
|
* @param methodName The name of the method.
|
|
125
|
-
* @param
|
|
170
|
+
* @param _descriptor The Property Descriptor for the method.
|
|
126
171
|
* @returns Undefined (nothing), as this decorator does not modify the method in any way.
|
|
127
172
|
*/
|
|
128
|
-
|
|
129
|
-
return function (target, methodName, descriptor) {
|
|
173
|
+
return function (target, methodName, _descriptor) {
|
|
130
174
|
if (typeof target !== 'object' || typeof target.constructor !== 'function') {
|
|
131
175
|
throw new Error('@PostConstruct not applied to instance method [' + target.toString() + '/' + methodName.toString() + ']');
|
|
132
176
|
}
|
|
@@ -142,7 +186,6 @@ export function PostConstruct() {
|
|
|
142
186
|
}
|
|
143
187
|
};
|
|
144
188
|
}
|
|
145
|
-
// noinspection JSUnusedGlobalSymbols
|
|
146
189
|
/**
|
|
147
190
|
* Placed just before a class method, this decorator identifies a method which should be called when an object is removed from service.
|
|
148
191
|
* If invoked by the container, the container will drop any references it has to the object when the method returns.
|
|
@@ -159,11 +202,10 @@ export function Release() {
|
|
|
159
202
|
/**
|
|
160
203
|
* @param prototypeOrConstructor The prototype of the class (we don't allow @Release on anything other than a class instance method.
|
|
161
204
|
* @param methodName The name of the method.
|
|
162
|
-
* @param
|
|
205
|
+
* @param _descriptor The Property Descriptor for the method.
|
|
163
206
|
* @returns Undefined (nothing), as this decorator does not modify the method in any way.
|
|
164
207
|
*/
|
|
165
|
-
|
|
166
|
-
return function (target, methodName, descriptor) {
|
|
208
|
+
return function (target, methodName, _descriptor) {
|
|
167
209
|
if (typeof target !== 'object' || typeof target.constructor !== 'function') {
|
|
168
210
|
throw new Error('@Release not applied to instance method [' + target.toString() + '/' + methodName.toString() + ']');
|
|
169
211
|
}
|
|
@@ -1 +1 @@
|
|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the information they collect (whether class, method, or parameter data) as tagged metadata on the class's constructor\n */\nimport {INJECT_METADATA_KEY, INJECTABLE_METADATA_KEY, OPTIONAL_METADATA_KEY, POSTCONSTRUCT_ASYNC_METADATA_KEY, POSTCONSTRUCT_SYNC_METADATA_KEY, REFLECT_RETURN, RELEASE_METADATA_KEY} from './constants';\nimport {InjectableId} from './injector';\n\n// Help user locate misapplied decorators.\nfunction targetHint(target: Function): string {\n\tlet hint: string | undefined;\n\tif (target) {\n\t\thint = target.name;\n\t\tif ((!hint) && target.constructor) {\n\t\t\thint = target.constructor.name;\n\t\t}\n\t}\n\treturn hint ?? '';\n}\n\n// Validate that 'target' is a class constructor function.\nfunction isClassConstructor(target: any) {\n\tif (typeof target === 'function' && target.hasOwnProperty('prototype')) {\n\t\treturn target.prototype.constructor === target;\n\t}\n\treturn false;\n}\n\n// Ensure consistency in our meta-data name getting/setting.\nfunction makeParamIdxKey(idx: number): string {\n\treturn `index-${idx}`;\n}\n\n// Validate that the specified target is a parameter of a class constructor\nfunction validateConstructorParam(decorator: string, target: Function, idx: number): string {\n\tif (!isClassConstructor(target)) {\n\t\tthrow new Error('@' + decorator + ' is not valid here [' + targetHint(target) + ']');\n\t}\n\treturn makeParamIdxKey(idx);\n}\n\n// Validate the decorator was only applied once.\nfunction validateSingleConstructorParam(decorator: string, target: Function, idx: number): string {\n\tconst propKey = validateConstructorParam(decorator, target, idx);\n\tif (Reflect.hasOwnMetadata(decorator, target, propKey)) {\n\t\tthrow new Error('@' + decorator + ' applied multiple times [' + target.constructor.name + ']');\n\t}\n\treturn propKey;\n}\n\n/**\n * Placed just before the class declaration, this class decorator applies metadata to the class constructor indicating that the user intends to bind the class into the container.\n * This decorator will throw if not placed on a class declaration, or if placed more than once on a class declaration.\n */\nexport function Injectable(): ClassDecorator {\n\t/**\n\t * @param target The constructor function of the class that is being decorated\n\t * @returns Undefined (nothing), as this decorator does not modify the constructor in any way.\n\t */\n\treturn function (target: Function): void {\n\t\tif (Reflect.hasOwnMetadata(INJECTABLE_METADATA_KEY, target)) {\n\t\t\tthrow new Error('@Injectable applied multiple times [' + targetHint(target) + ']');\n\t\t}\n\t\tReflect.defineMetadata(INJECTABLE_METADATA_KEY, true, target);\n\t};\n}\n\n/**\n * Placed just before a constructor parameter, this parameter decorator allows for specificity and control over the type of the type of Object that will be injected into the parameter.\n * In the absence of this decorator the container will use whatever is bound to a parameter's type (or throw an error if it is unable to recognize the type).\n *\n * @param id The identifier of the bound type that should be injected.\n */\nexport function Inject(id: InjectableId<any>): ParameterDecorator {\n\t/**\n\t * @param target The constructor function of the class (we don't allow @Inject on anything else).\n\t * @param parameterName The name of the parameter\n\t * @param parameterIndex The ordinal index of the parameter in the function’s parameter list\n\t * @returns Undefined (nothing), as this decorator does not modify the parameter in any way.\n\t */\n\treturn function (target: object, parameterName: string | symbol | undefined, parameterIndex: number): void {\n\t\tif (id === undefined) {\n\t\t\tthrow new Error('Undefined id passed to @Inject [' + targetHint(target as Function) + ']');\n\t\t}\n\t\tconst paramKey = validateSingleConstructorParam('Inject', target as Function, parameterIndex);\n\t\tReflect.defineMetadata(INJECT_METADATA_KEY, id, target, paramKey);\n\t};\n}\n\n/**\n * This is a helper function used by the container to retrieve the @Inject metadata for a specifically indexed constructor parameter\n *\n * @param target The constructor function of the class (we don't allow @Inject on anything else).\n * @param parameterIndex The ordinal index of the parameter in the constructor’s parameter list\n * @see Inject\n */\nexport function _getInjectedIdAt(target: Function, parameterIndex: number): InjectableId<any> {\n\treturn Reflect.getMetadata(INJECT_METADATA_KEY, target, makeParamIdxKey(parameterIndex)) as InjectableId<any>;\n}\n\n/**\n * Placed just before a constructor parameter, this parameter decorator signals the container that it should supply the 'alt' constant value (undefined by default) if for *any* reason it is unable to otherwise resolve the type of the parameter.\n * WARNING! It is your responsibility to ensure that alt is of the appropriate type/value.\n */\nexport function Optional(alt?: any): ParameterDecorator {\n\t/**\n\t * @param target The constructor function of the class (we don't allow @Optional on anything else).\n\t * @param parameterName The name of the parameter\n\t * @param parameterIndex The ordinal index of the parameter in the function’s parameter list\n\t * @returns Undefined (nothing), as this decorator does not modify the parameter in any way.\n\t */\n\treturn function (target: object, parameterName: string | symbol | undefined, parameterIndex: number): void {\n\t\tconst paramKey = validateSingleConstructorParam('Optional', target as Function, parameterIndex);\n\t\tReflect.defineMetadata(OPTIONAL_METADATA_KEY, {value: alt}, target, paramKey);\n\t};\n}\n\n/**\n * This is a helper function used by the container to retrieve the @Optional metadata for a specifically indexed constructor parameter\n *\n * @param target The constructor function of the class (we don't allow @Optional on anything else).\n * @param parameterIndex The ordinal index of the parameter in the constructor’s parameter list\n * @see Optional\n * @returns an object containing the value provided in the decorator, or undefined if no annotation was present.\n */\nexport function _getOptionalDefaultAt(target: Function, parameterIndex: number): { value: any } {\n\treturn Reflect.getMetadata(OPTIONAL_METADATA_KEY, target, makeParamIdxKey(parameterIndex)) as { value: any }; // See the @Optional decorator before making any changes here.\n}\n\n/**\n * Placed just before a class method, this method decorator flags a method that should be called after an object has been instantiated by the container, but before it is put into service.\n * The method will be assumed to be synchronous unless the method signature explicitly declares it's return type to be \": Promise<something>\"\n * This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.\n */\nexport function PostConstruct(): MethodDecorator {\n\t/**\n\t * @param prototypeOrConstructor The prototype of the class (we don't allow @PostConstruct on anything other than a class instance method.\n\t * @param methodName The name of the method.\n\t * @param descriptor The Property Descriptor for the method.\n\t * @returns Undefined (nothing), as this decorator does not modify the method in any way.\n\t */\n\t// noinspection JSUnusedLocalSymbols\n\treturn function (target: Object, methodName: string | symbol, descriptor: PropertyDescriptor) {\n\t\tif (typeof target !== 'object' || typeof target.constructor !== 'function') {\n\t\t\tthrow new Error('@PostConstruct not applied to instance method [' + target.toString() + '/' + methodName.toString() + ']');\n\t\t}\n\t\tif (Reflect.hasOwnMetadata(POSTCONSTRUCT_SYNC_METADATA_KEY, target.constructor) || Reflect.hasOwnMetadata(POSTCONSTRUCT_ASYNC_METADATA_KEY, target.constructor)) {\n\t\t\tthrow new Error('@PostConstruct applied multiple times [' + targetHint(target.constructor) + ']');\n\t\t}\n\t\tconst rt = Reflect.getMetadata(REFLECT_RETURN, target, methodName);\n\t\tif (typeof rt === 'function') {\n\t\t\tReflect.defineMetadata(POSTCONSTRUCT_ASYNC_METADATA_KEY, methodName, target.constructor);\n\t\t}\n\t\telse {\n\t\t\tReflect.defineMetadata(POSTCONSTRUCT_SYNC_METADATA_KEY, methodName, target.constructor);\n\t\t}\n\t};\n}\n\n// noinspection JSUnusedGlobalSymbols\n/**\n * Placed just before a class method, this decorator identifies a method which should be called when an object is removed from service.\n * If invoked by the container, the container will drop any references it has to the object when the method returns.\n * Note that this decorator is *not* a guarantee (or even an implication) that the decorated method will be called (JavaScript has no mechanism to enforce such a contract).\n * This decorator simply serves as a flag to indicate a method which is intended to clean up resources allocated by the object *which would not otherwise be garbage collected*.\n * You should *not* use this decorator as a general \"object finalization\" method. It has very limited scope and purpose.\n * The decorated method must complete normally (no throwing), as \"release\" is not an abort-able process.\n * This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.\n * The @see InvokeReleaseMethod helper function can search for and invoke the @Release decorated method of an object.\n * Also @see Container.releaseSingletons for the intended usage of this decorator.\n * It is intended that after the @Release decorated method of an object is called, that object will not be used again, but this is of course not enforced).\n */\nexport function Release(): MethodDecorator {\n\t/**\n\t * @param prototypeOrConstructor The prototype of the class (we don't allow @Release on anything other than a class instance method.\n\t * @param methodName The name of the method.\n\t * @param descriptor The Property Descriptor for the method.\n\t * @returns Undefined (nothing), as this decorator does not modify the method in any way.\n\t */\n\t// noinspection JSUnusedLocalSymbols\n\treturn function (target: Object, methodName: string | symbol, descriptor: PropertyDescriptor) {\n\t\tif (typeof target !== 'object' || typeof target.constructor !== 'function') {\n\t\t\tthrow new Error('@Release not applied to instance method [' + target.toString() + '/' + methodName.toString() + ']');\n\t\t}\n\t\tif (Reflect.hasOwnMetadata(RELEASE_METADATA_KEY, target.constructor)) {\n\t\t\tthrow new Error('@Release applied multiple times [' + targetHint(target.constructor) + ']');\n\t\t}\n\t\tReflect.defineMetadata(RELEASE_METADATA_KEY, methodName, target.constructor);\n\t};\n}\n"]}
|
|
1
|
+
{"version":3,"file":"decorators.js","sourceRoot":"","sources":["../../src/decorators.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAC,mBAAmB,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,gCAAgC,EAAE,+BAA+B,EAAE,cAAc,EAAE,cAAc,EAAE,oBAAoB,EAAC,MAAM,aAAa,CAAC;AAGzN,0CAA0C;AAC1C,SAAS,UAAU,CAAC,MAAgB;IACnC,IAAI,IAAwB,CAAC;IAC7B,IAAI,MAAM,EAAE,CAAC;QACZ,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;QACnB,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,MAAM,CAAC,WAAW,EAAE,CAAC;YACnC,IAAI,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC;QAChC,CAAC;IACF,CAAC;IACD,OAAO,IAAI,aAAJ,IAAI,cAAJ,IAAI,GAAI,EAAE,CAAC;AACnB,CAAC;AAED,0DAA0D;AAC1D,SAAS,kBAAkB,CAAC,MAAW;IACtC,IAAI,OAAO,MAAM,KAAK,UAAU,IAAI,MAAM,CAAC,cAAc,CAAC,WAAW,CAAC,EAAE,CAAC;QACxE,OAAO,MAAM,CAAC,SAAS,CAAC,WAAW,KAAK,MAAM,CAAC;IAChD,CAAC;IACD,OAAO,KAAK,CAAC;AACd,CAAC;AAED,4DAA4D;AAC5D,SAAS,eAAe,CAAC,GAAW;IACnC,OAAO,SAAS,GAAG,EAAE,CAAC;AACvB,CAAC;AAED,mEAAmE;AACnE,SAAS,qBAAqB,CAAC,UAA2B,EAAE,GAAW;IACtE,OAAO,GAAG,MAAM,CAAC,UAAU,CAAC,UAAU,GAAG,EAAE,CAAC;AAC7C,CAAC;AAED,2EAA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,WAAW,KAAK,UAAU,EAAE,CAAC;YAC5E,MAAM,IAAI,KAAK,CAAC,2CAA2C,GAAG,MAAM,CAAC,QAAQ,EAAE,GAAG,GAAG,GAAG,UAAU,CAAC,QAAQ,EAAE,GAAG,GAAG,CAAC,CAAC;QACtH,CAAC;QACD,IAAI,OAAO,CAAC,cAAc,CAAC,oBAAoB,EAAE,MAAM,CAAC,WAAW,CAAC,EAAE,CAAC;YACtE,MAAM,IAAI,KAAK,CAAC,mCAAmC,GAAG,UAAU,CAAC,MAAM,CAAC,WAAW,CAAC,GAAG,GAAG,CAAC,CAAC;QAC7F,CAAC;QACD,OAAO,CAAC,cAAc,CAAC,oBAAoB,EAAE,UAAU,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC;IAC9E,CAAC,CAAC;AACH,CAAC","sourcesContent":["/**\n * These decorators all apply the information they collect (whether class, method, or parameter data) as tagged metadata on the class's constructor\n */\nimport {INJECT_METADATA_KEY, INJECTABLE_METADATA_KEY, OPTIONAL_METADATA_KEY, POSTCONSTRUCT_ASYNC_METADATA_KEY, POSTCONSTRUCT_SYNC_METADATA_KEY, REFLECT_PARAMS, REFLECT_RETURN, RELEASE_METADATA_KEY} from './constants';\nimport {InjectableId} from './injector';\n\n// Help user locate misapplied decorators.\nfunction targetHint(target: Function): string {\n\tlet hint: string | undefined;\n\tif (target) {\n\t\thint = target.name;\n\t\tif ((!hint) && target.constructor) {\n\t\t\thint = target.constructor.name;\n\t\t}\n\t}\n\treturn hint ?? '';\n}\n\n// Validate that 'target' is a class constructor function.\nfunction isClassConstructor(target: any) {\n\tif (typeof target === 'function' && target.hasOwnProperty('prototype')) {\n\t\treturn target.prototype.constructor === target;\n\t}\n\treturn false;\n}\n\n// Ensure consistency in our meta-data name getting/setting.\nfunction makeParamIdxKey(idx: number): string {\n\treturn `index-${idx}`;\n}\n\n// Key for @Inject/@Optional metadata stored on a method parameter.\nfunction makeMethodParamIdxKey(methodName: string | symbol, idx: number): string {\n\treturn `${String(methodName)}:index-${idx}`;\n}\n\n// Validate that the specified target is a parameter of a class constructor\nfunction validateConstructorParam(decorator: string, target: Function, idx: number): string {\n\tif (!isClassConstructor(target)) {\n\t\tthrow new Error('@' + decorator + ' is not valid here [' + targetHint(target) + ']');\n\t}\n\treturn makeParamIdxKey(idx);\n}\n\n// Validate that the target is an instance method parameter (non-constructor).\nfunction validateMethodParam(decorator: string, target: object, methodName: string | symbol, idx: number): string {\n\tif (typeof target !== 'object' || typeof (target as any).constructor !== 'function') {\n\t\tthrow new Error('@' + decorator + ' is not valid here [' + targetHint(target as Function) + ']');\n\t}\n\tif (!Reflect.hasOwnMetadata(REFLECT_PARAMS, target, methodName)) {\n\t\t// REFLECT_PARAMS not yet set (decoration order), so we cannot pre-validate the method; accept silently.\n\t}\n\treturn makeMethodParamIdxKey(methodName, idx);\n}\n\n// Validate the decorator was only applied once.\nfunction validateSingleConstructorParam(decorator: string, target: Function, idx: number): string {\n\tconst propKey = validateConstructorParam(decorator, target, idx);\n\tif (Reflect.hasOwnMetadata(decorator, target, propKey)) {\n\t\tthrow new Error('@' + decorator + ' applied multiple times [' + target.constructor.name + ']');\n\t}\n\treturn propKey;\n}\n\n/**\n * Placed just before the class declaration, this class decorator applies metadata to the class constructor indicating that the user intends to bind the class into the container.\n * This decorator will throw if not placed on a class declaration, or if placed more than once on a class declaration.\n */\nexport function Injectable(): ClassDecorator {\n\t/**\n\t * @param target The constructor function of the class that is being decorated\n\t * @returns Undefined (nothing), as this decorator does not modify the constructor in any way.\n\t */\n\treturn function (target: Function): void {\n\t\tif (Reflect.hasOwnMetadata(INJECTABLE_METADATA_KEY, target)) {\n\t\t\tthrow new Error('@Injectable applied multiple times [' + targetHint(target) + ']');\n\t\t}\n\t\tReflect.defineMetadata(INJECTABLE_METADATA_KEY, true, target);\n\t};\n}\n\n/**\n * Placed just before a constructor parameter, this parameter decorator allows for specificity and control over the type of Object that will be injected into the parameter.\n * In the absence of this decorator the container will use whatever is bound to a parameter's type (or throw an error if it is unable to recognize the type).\n * This decorator may also be placed on a parameter of a method annotated with @PostConstruct, in which case the container will resolve and inject the value before invoking that method.\n *\n * @param id The identifier of the bound type that should be injected.\n */\nexport function Inject(id: InjectableId<any>): ParameterDecorator {\n\t/**\n\t * @param target The constructor function of the class (for constructor params), or the class prototype (for method params).\n\t * @param parameterName The name of the parameter (undefined for constructor params, the method name for method params)\n\t * @param parameterIndex The ordinal index of the parameter in the function’s parameter list\n\t * @returns Undefined (nothing), as this decorator does not modify the parameter in any way.\n\t */\n\treturn function (target: object, parameterName: string | symbol | undefined, parameterIndex: number): void {\n\t\tif (id === undefined) {\n\t\t\tthrow new Error('Undefined id passed to @Inject [' + targetHint(target as Function) + ']');\n\t\t}\n\t\tif (parameterName === undefined) {\n\t\t\t// Constructor parameter\n\t\t\tconst paramKey = validateSingleConstructorParam('Inject', target as Function, parameterIndex);\n\t\t\tReflect.defineMetadata(INJECT_METADATA_KEY, id, target, paramKey);\n\t\t}\n\t\telse {\n\t\t\t// Method parameter — silently accepted (intended for @PostConstruct methods)\n\t\t\tconst paramKey = validateMethodParam('Inject', target, parameterName, parameterIndex);\n\t\t\tReflect.defineMetadata(INJECT_METADATA_KEY, id, target, paramKey);\n\t\t}\n\t};\n}\n\n/**\n * This is a helper function used by the container to retrieve the @Inject metadata for a specifically indexed constructor parameter\n *\n * @param target The constructor function of the class (we don't allow @Inject on anything else).\n * @param parameterIndex The ordinal index of the parameter in the constructor’s parameter list\n * @see Inject\n */\nexport function _getInjectedIdAt(target: Function, parameterIndex: number): InjectableId<any> {\n\treturn Reflect.getMetadata(INJECT_METADATA_KEY, target, makeParamIdxKey(parameterIndex)) as InjectableId<any>;\n}\n\n/**\n * Retrieve the @Inject metadata for a specifically indexed parameter of a named method.\n */\nexport function _getInjectedIdForMethod(prototype: object, methodName: string | symbol, parameterIndex: number): InjectableId<any> {\n\treturn Reflect.getMetadata(INJECT_METADATA_KEY, prototype, makeMethodParamIdxKey(methodName, parameterIndex)) as InjectableId<any>;\n}\n\n/**\n * Placed just before a constructor parameter, this parameter decorator signals the container that it should supply the 'alt' constant value (undefined by default) if for *any* reason it is unable to otherwise resolve the type of the parameter.\n * This decorator may also be placed on a parameter of a method annotated with @PostConstruct.\n * WARNING! It is your responsibility to ensure that alt is of the appropriate type/value.\n */\nexport function Optional(alt?: any): ParameterDecorator {\n\t/**\n\t * @param target The constructor function of the class (for constructor params), or the class prototype (for method params).\n\t * @param parameterName The name of the parameter (undefined for constructor params, the method name for method params)\n\t * @param parameterIndex The ordinal index of the parameter in the function’s parameter list\n\t * @returns Undefined (nothing), as this decorator does not modify the parameter in any way.\n\t */\n\treturn function (target: object, parameterName: string | symbol | undefined, parameterIndex: number): void {\n\t\tif (parameterName === undefined) {\n\t\t\t// Constructor parameter\n\t\t\tconst paramKey = validateSingleConstructorParam('Optional', target as Function, parameterIndex);\n\t\t\tReflect.defineMetadata(OPTIONAL_METADATA_KEY, {value: alt}, target, paramKey);\n\t\t}\n\t\telse {\n\t\t\t// Method parameter — silently accepted (intended for @PostConstruct methods)\n\t\t\tconst paramKey = validateMethodParam('Optional', target, parameterName, parameterIndex);\n\t\t\tReflect.defineMetadata(OPTIONAL_METADATA_KEY, {value: alt}, target, paramKey);\n\t\t}\n\t};\n}\n\n/**\n * This is a helper function used by the container to retrieve the @Optional metadata for a specifically indexed constructor parameter\n *\n * @param target The constructor function of the class (we don't allow @Optional on anything else).\n * @param parameterIndex The ordinal index of the parameter in the constructor’s parameter list\n * @see Optional\n * @returns an object containing the value provided in the decorator, or undefined if no annotation was present.\n */\nexport function _getOptionalDefaultAt(target: Function, parameterIndex: number): { value: any } {\n\treturn Reflect.getMetadata(OPTIONAL_METADATA_KEY, target, makeParamIdxKey(parameterIndex)) as { value: any }; // See the @Optional decorator before making any changes here.\n}\n\n/**\n * Retrieve the @Optional metadata for a specifically indexed parameter of a named method.\n */\nexport function _getOptionalDefaultForMethod(prototype: object, methodName: string | symbol, parameterIndex: number): { value: any } {\n\treturn Reflect.getMetadata(OPTIONAL_METADATA_KEY, prototype, makeMethodParamIdxKey(methodName, parameterIndex)) as { value: any };\n}\n\n/**\n * Placed just before a class method, this method decorator flags a method that should be called after an object has been instantiated by the container, but before it is put into service.\n * The method will be assumed to be synchronous unless the method signature explicitly declares its return type to be \": Promise<something>\".\n * Parameters will be resolved by the container just as they are for constructors.\n * This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.\n */\nexport function PostConstruct(): MethodDecorator {\n\t/**\n\t * @param prototypeOrConstructor The prototype of the class (we don't allow @PostConstruct on anything other than a class instance method.\n\t * @param methodName The name of the method.\n\t * @param _descriptor The Property Descriptor for the method.\n\t * @returns Undefined (nothing), as this decorator does not modify the method in any way.\n\t */\n\treturn function (target: Object, methodName: string | symbol, _descriptor: PropertyDescriptor) {\n\t\tif (typeof target !== 'object' || typeof target.constructor !== 'function') {\n\t\t\tthrow new Error('@PostConstruct not applied to instance method [' + target.toString() + '/' + methodName.toString() + ']');\n\t\t}\n\t\tif (Reflect.hasOwnMetadata(POSTCONSTRUCT_SYNC_METADATA_KEY, target.constructor) || Reflect.hasOwnMetadata(POSTCONSTRUCT_ASYNC_METADATA_KEY, target.constructor)) {\n\t\t\tthrow new Error('@PostConstruct applied multiple times [' + targetHint(target.constructor) + ']');\n\t\t}\n\t\tconst rt = Reflect.getMetadata(REFLECT_RETURN, target, methodName);\n\t\tif (typeof rt === 'function') {\n\t\t\tReflect.defineMetadata(POSTCONSTRUCT_ASYNC_METADATA_KEY, methodName, target.constructor);\n\t\t}\n\t\telse {\n\t\t\tReflect.defineMetadata(POSTCONSTRUCT_SYNC_METADATA_KEY, methodName, target.constructor);\n\t\t}\n\t};\n}\n\n/**\n * Placed just before a class method, this decorator identifies a method which should be called when an object is removed from service.\n * If invoked by the container, the container will drop any references it has to the object when the method returns.\n * Note that this decorator is *not* a guarantee (or even an implication) that the decorated method will be called (JavaScript has no mechanism to enforce such a contract).\n * This decorator simply serves as a flag to indicate a method which is intended to clean up resources allocated by the object *which would not otherwise be garbage collected*.\n * You should *not* use this decorator as a general \"object finalization\" method. It has very limited scope and purpose.\n * The decorated method must complete normally (no throwing), as \"release\" is not an abort-able process.\n * This decorator will throw if placed on a non-method or a static method of a class, or if placed on a method more than once, or if placed on more than one method for a class.\n * The @see InvokeReleaseMethod helper function can search for and invoke the @Release decorated method of an object.\n * Also @see Container.releaseSingletons for the intended usage of this decorator.\n * It is intended that after the @Release decorated method of an object is called, that object will not be used again, but this is of course not enforced).\n */\nexport function Release(): MethodDecorator {\n\t/**\n\t * @param prototypeOrConstructor The prototype of the class (we don't allow @Release on anything other than a class instance method.\n\t * @param methodName The name of the method.\n\t * @param _descriptor The Property Descriptor for the method.\n\t * @returns Undefined (nothing), as this decorator does not modify the method in any way.\n\t */\n\treturn function (target: Object, methodName: string | symbol, _descriptor: PropertyDescriptor) {\n\t\tif (typeof target !== 'object' || typeof target.constructor !== 'function') {\n\t\t\tthrow new Error('@Release not applied to instance method [' + target.toString() + '/' + methodName.toString() + ']');\n\t\t}\n\t\tif (Reflect.hasOwnMetadata(RELEASE_METADATA_KEY, target.constructor)) {\n\t\t\tthrow new Error('@Release applied multiple times [' + targetHint(target.constructor) + ']');\n\t\t}\n\t\tReflect.defineMetadata(RELEASE_METADATA_KEY, methodName, target.constructor);\n\t};\n}\n"]}
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,aAAa,CAAC;AACtC,OAAO,EAAC,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,aAAa,EAAE,OAAO,EAAC,MAAM,cAAc,CAAC;AAClF,OAAO,EAAiC,cAAc,EAAW,MAAM,YAAY,CAAC","sourcesContent":["export {Container} from './container';\nexport {Inject, Injectable, Optional, PostConstruct, Release} from './decorators';\nexport {ClassConstructor, InjectableId, InjectionToken, Injector} from './injector';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,aAAa,CAAC;AACtC,OAAO,EAAC,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,aAAa,EAAE,OAAO,EAAC,MAAM,cAAc,CAAC;AAClF,OAAO,EAAiC,cAAc,EAAW,MAAM,YAAY,CAAC","sourcesContent":["export {Container} from './container';\nexport {Inject, Injectable, Optional, PostConstruct, Release} from './decorators';\nexport {ClassConstructor, InjectableId, InjectionToken, Injector} from './injector';\nexport {AsyncFactory, OnErrorCallback, OnSuccessCallback, RegisterDescriptor, SyncFactory} from './binding';\n"]}
|
package/lib/esm/injector.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injector.js","sourceRoot":"","sources":["../../src/injector.ts"],"names":[],"mappings":"AASA;;;GAGG;AACH,MAAM,OAAO,cAAc;IAC1B,YAAoB,EAAmB;QAAnB,OAAE,GAAF,EAAE,CAAiB;IACvC,CAAC;IAED,IAAI,WAAW;QACd,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAC;IACxB,CAAC;IAED,QAAQ;QACP,qCAAqC;QACrC,IAAI,OAAO,IAAI,CAAC,EAAE,KAAK,QAAQ,EAAE,CAAC;YACjC,MAAM,IAAI,GAAI,IAAI,CAAC,EAA0C,CAAC,WAAW,CAAC;YAC1E,OAAO,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC;QACvD,CAAC;QACD,OAAO,IAAI,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC;IAC3B,CAAC;CACD","sourcesContent":["/**\n * This is about as close as we can get in Typescript\n */\nexport type AbstractConstructor<T> = Function & { prototype: T };\n/**\n * Standard definition of a constructor.\n */\nexport type ClassConstructor<T> = new (...args: any[]) => T;\n\n/**\n * Allow for implicit typing of constants and interfaces.\n * Inspired by Angular and some colleges at work.\n */\nexport class InjectionToken<T> {\n\tconstructor(private id: string | symbol) {\n\t}\n\n\tget description(): string {\n\t\treturn this.toString();\n\t}\n\n\ttoString(): string {\n\t\t// noinspection SuspiciousTypeOfGuard\n\t\tif (typeof this.id === 'symbol') {\n\t\t\tconst desc = (this.id as unknown as { description?: string }).description;\n\t\t\treturn desc !== undefined ? desc : this.id.toString();\n\t\t}\n\t\treturn this.id.toString();\n\t}\n}\n\n/**\n * Universal id that can be bound to a constant, class, or factories.\n */\nexport type InjectableId<T> = (string | symbol | AbstractConstructor<T> | ClassConstructor<T> | InjectionToken<T>) & { readonly description?: string };\n\n/**\n * Retrieve instances previously bound to the specified InjectableId.\n */\nexport interface Injector {\n\t/**\n\t * Check to see if the existing InjectableId is known (aka has been bound).\n\t * Error callbacks may wish to know if a particular InjectableId is available.\n\t * Also the
|
|
1
|
+
{"version":3,"file":"injector.js","sourceRoot":"","sources":["../../src/injector.ts"],"names":[],"mappings":"AASA;;;GAGG;AACH,MAAM,OAAO,cAAc;IAC1B,YAAoB,EAAmB;QAAnB,OAAE,GAAF,EAAE,CAAiB;IACvC,CAAC;IAED,IAAI,WAAW;QACd,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAC;IACxB,CAAC;IAED,QAAQ;QACP,qCAAqC;QACrC,IAAI,OAAO,IAAI,CAAC,EAAE,KAAK,QAAQ,EAAE,CAAC;YACjC,MAAM,IAAI,GAAI,IAAI,CAAC,EAA0C,CAAC,WAAW,CAAC;YAC1E,OAAO,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC;QACvD,CAAC;QACD,OAAO,IAAI,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC;IAC3B,CAAC;CACD","sourcesContent":["/**\n * This is about as close as we can get in Typescript\n */\nexport type AbstractConstructor<T> = Function & { prototype: T };\n/**\n * Standard definition of a constructor.\n */\nexport type ClassConstructor<T> = new (...args: any[]) => T;\n\n/**\n * Allow for implicit typing of constants and interfaces.\n * Inspired by Angular and some colleges at work.\n */\nexport class InjectionToken<T> {\n\tconstructor(private id: string | symbol) {\n\t}\n\n\tget description(): string {\n\t\treturn this.toString();\n\t}\n\n\ttoString(): string {\n\t\t// noinspection SuspiciousTypeOfGuard\n\t\tif (typeof this.id === 'symbol') {\n\t\t\tconst desc = (this.id as unknown as { description?: string }).description;\n\t\t\treturn desc !== undefined ? desc : this.id.toString();\n\t\t}\n\t\treturn this.id.toString();\n\t}\n}\n\n/**\n * Universal id that can be bound to a constant, class, or factories.\n */\nexport type InjectableId<T> = (string | symbol | AbstractConstructor<T> | ClassConstructor<T> | InjectionToken<T>) & { readonly description?: string };\n\n/**\n * Retrieve instances previously bound to the specified InjectableId.\n */\nexport interface Injector {\n\t/**\n\t * Check to see if the existing InjectableId is known (aka has been bound).\n\t * Error callbacks may wish to know if a particular InjectableId is available.\n\t * Also the Container's bindXXX calls always overwrite any previous bindings, so you may want to use this as a gate.\n\t *\n\t * @param id The id to check for.\n\t * @param ascending If true, this will search up the chain of parent containers (if they exist). Default is false (only checks if the id is bound within this container).\n\t */\n\tisIdKnown<T>(id: InjectableId<T> | AbstractConstructor<T>, ascending?: boolean): boolean;\n\n\t/**\n\t * Return an instance of <T> previously bound to 'id'.\n\t *\n\t * @throws Error if the InjectableId was never registered, OR if there are unresolved asynchronous dependencies in the dependency tree for 'id'.\n\t */\n\tget<T>(id: InjectableId<T> | AbstractConstructor<T>): T;\n\n\t/**\n\t * awaits the asynchronous resolution of all dependencies in the tree for 'id'.\n\t */\n\tresolve<T>(id?: InjectableId<T> | AbstractConstructor<T>): Promise<T>;\n}\n"]}
|
package/lib/esm/provider.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../../src/provider.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,mBAAmB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IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,CAAC;oBAClC,mBAAmB,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;oBACjC,OAAO,CAAC,CAAC,SAAS,CAAC;gBACpB,CAAC;gBACD,OAAO,IAAI,CAAC;YACb,CAAC;QACF,CAAC;QACD,OAAO,SAAS,CAAC;IAClB,CAAC;CACD","sourcesContent":["import {State} from './state';\nimport {InvokeReleaseMethod} from './utils';\n\n\n/**\n * Internally all InjectableIds are mapped to an abstract Provider<T>.\n * A Provider may choose to return a singleton or a new value each time it is queried.\n */\nexport abstract class Provider<T = any> {\n\tprotected constructor() {\n\t}\n\n\t/**\n\t * If the provider is configured as a singleton, this property will be the state of that singleton.\n\t * This value will be defined for resolved/resolving Singletons, null for Singletons that have not yet been queried, and will remain undefined for non-Singleton Providers.\n\t * Default value is undefined (e.g. not a Singleton).\n\t */\n\tprotected singleton?: State<T> | null;\n\n\t/**\n\t * This is the workhorse method of the Provider, and is invoked directly or indirectly by both Injector.get and Injector.resolve.\n\t * This method returns the current State<T> if it is already known (which it might be for Singleton scenarios).\n\t * Otherwise it resolves the State<T>.\n\t * IF the Provider<T> is a Singleton, it's State<T> is updated before returning.\n\t */\n\tabstract provideAsState(): State<T>;\n\n\t/**\n\t * Base method to initialize the state of this Provider *if* (and only if) it has been configured as a Singleton.\n\t * If this Provider has not been configured as a singleton, this method is essentially a noop that returns undefined.\n\t *\n\t * @param _asyncOnly This default implementation ignores this parameter.\n\t * @returns A completion Promise if initialization requires asynchronicity, otherwise the return value is undefined.\n\t */\n\tresolveIfSingleton(_asyncOnly: boolean): Promise<T> | undefined {\n\t\tif (this.singleton === null) {\n\t\t\tconst s = this.provideAsState();\n\t\t\tif (s.pending)\n\t\t\t\treturn s.promise!;\n\t\t\telse if (s.rejected)\n\t\t\t\treturn Promise.reject(s.rejected);\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * If (and only if) this Provider has been configured as a Singleton, and if it has been (or is being resolved), find and invoke the @Release decorated method (if there is one).\n\t * NOTE that if the singleton is actively being resolved when this method is called, this method waits for the resolution to complete and then invokes the @Release decorated method; But in any case this is a synchronous method and returns immediately to it's caller.\n\t * Also note that invoking this method does not release or invalidate the Provider;\n\t * Rather, it resets a Singleton Provider to a fresh (unresolved/unqueried) state (aka sets this.singleton to null).\n\t * It is assumed that the Singleton itself will no longer be used after this method returns.\n\t * If not a singleton, this method returns undefined.\n\t * If the singleton has been resolved, it is returned, otherwise null is returned.\n\t * If the singleton is pending resolution, a Promise for the singleton or for null is returned.\n\t * Note that if a singleton is returned, its Release method will already have been invoked.\n\t */\n\treleaseIfSingleton(): T | undefined | null | Promise<T | null> {\n\t\tif (this.singleton) {\n\t\t\tconst s = this.provideAsState();\n\t\t\tif (s.pending) {\n\t\t\t\treturn (async () => {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst v = await s.promise!;\n\t\t\t\t\t\tthis.singleton = null;\n\t\t\t\t\t\tInvokeReleaseMethod(v);\n\t\t\t\t\t\treturn v;\n\t\t\t\t\t}\n\t\t\t\t\tcatch {\n\t\t\t\t\t\tthis.singleton = null;\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\t\t\t\t})();\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis.singleton = null;\n\t\t\t\tif ((!s.rejected) && s.fulfilled) {\n\t\t\t\t\tInvokeReleaseMethod(s.fulfilled);\n\t\t\t\t\treturn s.fulfilled;\n\t\t\t\t}\n\t\t\t\treturn null;\n\t\t\t}\n\t\t}\n\t\treturn undefined;\n\t}\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sync-factory-provider.js","sourceRoot":"","sources":["../../src/sync-factory-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,gBAAgB,EAAC,MAAM,qBAAqB,CAAC;AAGrD,OAAO,EAAC,KAAK,EAAC,MAAM,SAAS,CAAC;AAE9B;;;GAGG;AACH,MAAM,OAAO,oBAAwB,SAAQ,gBAAmC;IAC/E,YAAY,QAAkB,EAAE,EAAmB,EAAE,KAAqB;QACzE,KAAK,CAAC,QAAQ,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC;IAC5B,CAAC;IAED;;;OAGG;IACH,cAAc;QACb,IAAI,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC;QAC5B,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,IAAI,CAAC;gBACJ,MAAM,GAAG,KAAK,CAAC,SAAS,CAAI,IAAI,EAAE,SAAS,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC;YACzE,CAAC;YACD,OAAO,GAAG,EAAE,CAAC;gBACZ,0EAA0E;gBAC1E,IAAI,CAAC;oBACJ,wEAAwE;oBACxE,MAAM,GAAG,KAAK,CAAC,SAAS,CAAI,IAAI,EAAE,SAAS,EAAE,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC,CAAC,CAAC;gBAC3E,CAAC;gBACD,OAAO,CAAC,EAAE,CAAC;oBACV,0CAA0C;oBAC1C,MAAM,GAAG,KAAK,CAAC,SAAS,CAAI,IAAI,EAAE,CAAC,EAAE,SAAS,CAAC,CAAC;gBACjD,CAAC;YACF,CAAC;QACF,CAAC;QACD,IAAI,IAAI,CAAC,SAAS,KAAK,IAAI;YAC1B,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC;QACzB,OAAO,MAAM,CAAC;IACf,CAAC;IAED;;;OAGG;IACH,kBAAkB,CAAC,SAAkB;QACpC,IAAI,SAAS;YACZ,OAAO,SAAS,CAAC;QAClB,OAAO,KAAK,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC;IACxC,CAAC;CACD","sourcesContent":["import {BindableProvider} from './bindable-provider';\nimport {SyncFactory} from './
|
|
1
|
+
{"version":3,"file":"sync-factory-provider.js","sourceRoot":"","sources":["../../src/sync-factory-provider.ts"],"names":[],"mappings":"AAAA,O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ncFactory} from './binding';\nimport {InjectableId, Injector} from './injector';\nimport {State} from './state';\n\n/**\n * @inheritDoc\n * This specialization simply invokes it's configured Factory and provides the result.\n */\nexport class FactoryBasedProvider<T> extends BindableProvider<T, SyncFactory<T>> {\n\tconstructor(injector: Injector, id: InjectableId<T>, maker: SyncFactory<T>) {\n\t\tsuper(injector, id, maker);\n\t}\n\n\t/**\n\t * @inheritDoc\n\t * This specialization invokes it's configured Factory and provides the result (or invokes the error handler if necessary).\n\t */\n\tprovideAsState(): State<T> {\n\t\tlet retVal = this.singleton;\n\t\tif (!retVal) {\n\t\t\ttry {\n\t\t\t\tretVal = State.MakeState<T>(null, undefined, this.maker(this.injector));\n\t\t\t}\n\t\t\tcatch (err) {\n\t\t\t\t// There was an error, give the errorHandler (if any) a crack at recovery.\n\t\t\t\ttry {\n\t\t\t\t\t// queryErrorHandler will throw if it could not get a substitute object.\n\t\t\t\t\tretVal = State.MakeState<T>(null, undefined, this.queryErrorHandler(err));\n\t\t\t\t}\n\t\t\t\tcatch (e) {\n\t\t\t\t\t// could not recover, propagate the error.\n\t\t\t\t\tretVal = State.MakeState<T>(null, e, undefined);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (this.singleton === null)\n\t\t\tthis.singleton = retVal;\n\t\treturn retVal;\n\t}\n\n\t/**\n\t * @inheritDoc\n\t * This specialization returns undefined anytime 'asyncOnly' is true (since this Provider is by definition synchronous).\n\t */\n\tresolveIfSingleton(asyncOnly: boolean): Promise<T> | undefined {\n\t\tif (asyncOnly)\n\t\t\treturn undefined;\n\t\treturn super.resolveIfSingleton(false);\n\t}\n}\n"]}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { Container } from './container';
|
|
2
2
|
export { Inject, Injectable, Optional, PostConstruct, Release } from './decorators';
|
|
3
3
|
export { ClassConstructor, InjectableId, InjectionToken, Injector } from './injector';
|
|
4
|
+
export { AsyncFactory, OnErrorCallback, OnSuccessCallback, RegisterDescriptor, SyncFactory } from './binding';
|
package/lib/injector.d.ts
CHANGED
|
@@ -31,7 +31,7 @@ export interface Injector {
|
|
|
31
31
|
/**
|
|
32
32
|
* Check to see if the existing InjectableId is known (aka has been bound).
|
|
33
33
|
* Error callbacks may wish to know if a particular InjectableId is available.
|
|
34
|
-
* Also the
|
|
34
|
+
* Also the Container's bindXXX calls always overwrite any previous bindings, so you may want to use this as a gate.
|
|
35
35
|
*
|
|
36
36
|
* @param id The id to check for.
|
|
37
37
|
* @param ascending If true, this will search up the chain of parent containers (if they exist). Default is false (only checks if the id is bound within this container).
|
package/package.json
CHANGED
package/lib/binder.d.ts
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { AbstractConstructor, ClassConstructor, InjectableId, Injector } from './injector';
|
|
2
|
-
/**
|
|
3
|
-
* Type definition for functions that return a value.
|
|
4
|
-
* The function should return a valid value, but may throw an exception if it cannot.
|
|
5
|
-
*/
|
|
6
|
-
export type SyncFactory<T> = (injector: Injector) => T;
|
|
7
|
-
/**
|
|
8
|
-
* Type definition for functions that return a Promise for a value.
|
|
9
|
-
* The function *must* not throw and must return a valid Promise (e.g. pending, resolved, rejected).
|
|
10
|
-
*/
|
|
11
|
-
export type AsyncFactory<T> = (injector: Injector) => Promise<T>;
|
|
12
|
-
/**
|
|
13
|
-
* You may bind an error handler which will be invoked if the bound InjectableId could not be put into service.
|
|
14
|
-
* An error handler *must* not throw, but may return an Error that will be propagated back up the call chain.
|
|
15
|
-
*
|
|
16
|
-
* @param binder The Binder that experienced the error.
|
|
17
|
-
* @param id The identifier for what was trying to be made.
|
|
18
|
-
* @param maker The thing that made (or tried to provideAsState). Will be one of type ClassConstructor, SyncFactory, or AsyncFactory, depending on how you registered the binding.
|
|
19
|
-
* @param error Identifies the problem that occurred.
|
|
20
|
-
* @param value If the 'maker' was able to create the thing, but it had an error during post construction, the made thing will be passed here.
|
|
21
|
-
* @returns one of 3 results...
|
|
22
|
-
* A substitute thing (kind of like a 'maker' do-over) which must be fully operational (e.g. any `@PostConstruct` will be ignored).
|
|
23
|
-
* An alternate Error which will be propagated back up the call chain.
|
|
24
|
-
* Undefined, which means the 'error' parameter will be propagated back up the call chain.
|
|
25
|
-
*/
|
|
26
|
-
export type OnErrorCallback<T, M> = (injector: Injector, id: InjectableId<T>, maker: M, error: unknown, value?: T) => T | Error | void;
|
|
27
|
-
/**
|
|
28
|
-
* You may bind a success handler which will be invoked just before the bound InjectableId is put into service.
|
|
29
|
-
* This is an alternative to the more preferred `@PostConstruct` decorator for scenarios when usage of that decorator is not feasible.
|
|
30
|
-
* WARNING:
|
|
31
|
-
* By registering a success handler, you override and nullify any `@PostConstruct` decorator on the class.
|
|
32
|
-
* In such a scenario, the success handler should perform whatever care and feeding the class expected from the `@PostConstruct` decorator.
|
|
33
|
-
* A success handler *must* not throw, but may return an Error that will be propagated back up the call chain.
|
|
34
|
-
*
|
|
35
|
-
* @param binder The Binder that performed the construction.
|
|
36
|
-
* @param id The identifier for what was made.
|
|
37
|
-
* @param maker The thing that made. Will be one of type ClassConstructor, SyncFactory, or AsyncFactory, depending on how you registered the binding.
|
|
38
|
-
* @param value The thing that was made.
|
|
39
|
-
* @returns one of 3 results...
|
|
40
|
-
* An Error which will be propagated back up the call chain.
|
|
41
|
-
* Undefined, which means the object is ready to be placed into service.
|
|
42
|
-
* A Promise that resolves to one of the above two values (undefined or Error).
|
|
43
|
-
*/
|
|
44
|
-
export type OnSuccessCallback<T, M> = (value: T, injector: Injector, id: InjectableId<T>, maker: M) => Promise<Error | void> | Error | void;
|
|
45
|
-
/**
|
|
46
|
-
* An interface allowing binding of an error handler.
|
|
47
|
-
*
|
|
48
|
-
* @see OnErrorCallback
|
|
49
|
-
*/
|
|
50
|
-
export interface BindErrHandler<T, M> {
|
|
51
|
-
onError(cb: OnErrorCallback<T, M>): void;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* An interface allowing binding of a post construction handler.
|
|
55
|
-
*
|
|
56
|
-
* @see OnSuccessCallback
|
|
57
|
-
*/
|
|
58
|
-
export interface BindHandler<T, M> extends BindErrHandler<T, M> {
|
|
59
|
-
onSuccess(cb: OnSuccessCallback<T, M>): BindErrHandler<T, M>;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* @inheritDoc
|
|
63
|
-
* This specialization also allows you to specify that the binding is 'Singleton' (e.g. only one in the system).
|
|
64
|
-
*/
|
|
65
|
-
export interface BindAs<T, M> extends BindHandler<T, M> {
|
|
66
|
-
asSingleton(): BindHandler<T, M>;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Bind Ids to producers.
|
|
70
|
-
*
|
|
71
|
-
* @deprecated {@link Binder} was removed from index.ts long ago will be removed in a future release.
|
|
72
|
-
* Use {@link Container} to build a context root — it provides all binding methods directly.
|
|
73
|
-
* For type annotations, use {@link Injector}, which {@link Container} implements.
|
|
74
|
-
*/
|
|
75
|
-
export interface Binder extends Injector {
|
|
76
|
-
/**
|
|
77
|
-
* Bind an InjectableId to a constant value.
|
|
78
|
-
* Constants are by their very nature singleton, and are assumed to be error proof.
|
|
79
|
-
*/
|
|
80
|
-
bindConstant<T>(id: InjectableId<T>, value: T): T;
|
|
81
|
-
/**
|
|
82
|
-
* Bind an InjectableId to a class (actually it's constructor).
|
|
83
|
-
* As a shortcut, you may use the class constructor as the 'id' (e.g. container.bindClass(A); ).
|
|
84
|
-
* The container will also invoke any `@PostConstruct` present on the class.
|
|
85
|
-
*/
|
|
86
|
-
bindClass<T>(id: ClassConstructor<T>, constructor?: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;
|
|
87
|
-
bindClass<T>(id: string | symbol | AbstractConstructor<T> | InjectableId<T>, constructor: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;
|
|
88
|
-
/**
|
|
89
|
-
* Bind an InjectableId to a synchronous factory that will be invoked on demand when the object is needed.
|
|
90
|
-
* The factory should produce the needed value
|
|
91
|
-
* NOTE: The container will not invoke any `@PostConstruct` present on the class, this is the responsibility of the factory.
|
|
92
|
-
*/
|
|
93
|
-
bindFactory<T>(id: InjectableId<T>, factory: SyncFactory<T>): BindAs<T, SyncFactory<T>>;
|
|
94
|
-
/**
|
|
95
|
-
* Bind an InjectableId to an asynchronous factory that will be invoked on demand when the object is needed.
|
|
96
|
-
* The factory should produce the needed value (asynchronously of course).
|
|
97
|
-
* NOTE: The container will not invoke any `@PostConstruct` present on the class, this is the responsibility of the factory.
|
|
98
|
-
* WARNING!!! The factory may not throw and must return a valid Promise (which can be pending, resolved, rejected, etc.).
|
|
99
|
-
*/
|
|
100
|
-
bindAsyncFactory<T>(id: InjectableId<T>, factory: AsyncFactory<T>): BindAs<T, AsyncFactory<T>>;
|
|
101
|
-
/**
|
|
102
|
-
* This essentially pre creates/loads all *singleton* InjectableIds currently known to the Binder.
|
|
103
|
-
* This *may* be helpful if you wish to use Injector.get on a dependency tree that has asynchronous singletons within the tree.
|
|
104
|
-
*
|
|
105
|
-
* @param asyncOnly Only resolve AsyncFactorys as well as any bound classes that have an asynchronous `@PostConstruct` decorator. WARNING: If true, SyncFactorys will *not* be resolved even if they are Singletons.
|
|
106
|
-
* @param parentRecursion If true and the the container has a parent, resolveIfSingleton will first be called for the parent
|
|
107
|
-
* @returns A Promise that resolves when all Singleton's have been resolved, OR rejects if one or more of the Singleton's failed to resolve. NOTE: Rejection does not occur until all Singleton resolutions have settled, and the rejection reason/err will be a Map<InjectableId, Error>
|
|
108
|
-
*/
|
|
109
|
-
resolveSingletons(asyncOnly?: boolean, parentRecursion?: boolean): Promise<this>;
|
|
110
|
-
}
|
package/lib/cjs/binder.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"binder.js","sourceRoot":"","sources":["../../src/binder.ts"],"names":[],"mappings":"","sourcesContent":["import {AbstractConstructor, ClassConstructor, InjectableId, Injector} from './injector';\n\n/**\n * Type definition for functions that return a value.\n * The function should return a valid value, but may throw an exception if it cannot.\n */\nexport type SyncFactory<T> = (injector: Injector) => T;\n\n/**\n * Type definition for functions that return a Promise for a value.\n * The function *must* not throw and must return a valid Promise (e.g. pending, resolved, rejected).\n */\nexport type AsyncFactory<T> = (injector: Injector) => Promise<T>;\n\n/**\n * You may bind an error handler which will be invoked if the bound InjectableId could not be put into service.\n * An error handler *must* not throw, but may return an Error that will be propagated back up the call chain.\n *\n * @param binder The Binder that experienced the error.\n * @param id The identifier for what was trying to be made.\n * @param maker The thing that made (or tried to provideAsState). Will be one of type ClassConstructor, SyncFactory, or AsyncFactory, depending on how you registered the binding.\n * @param error Identifies the problem that occurred.\n * @param value If the 'maker' was able to create the thing, but it had an error during post construction, the made thing will be passed here.\n * @returns one of 3 results...\n * A substitute thing (kind of like a 'maker' do-over) which must be fully operational (e.g. any `@PostConstruct` will be ignored).\n * An alternate Error which will be propagated back up the call chain.\n * Undefined, which means the 'error' parameter will be propagated back up the call chain.\n */\nexport type OnErrorCallback<T, M> = (injector: Injector, id: InjectableId<T>, maker: M, error: unknown, value?: T) => T | Error | void;\n\n/**\n * You may bind a success handler which will be invoked just before the bound InjectableId is put into service.\n * This is an alternative to the more preferred `@PostConstruct` decorator for scenarios when usage of that decorator is not feasible.\n * WARNING:\n * By registering a success handler, you override and nullify any `@PostConstruct` decorator on the class.\n * In such a scenario, the success handler should perform whatever care and feeding the class expected from the `@PostConstruct` decorator.\n * A success handler *must* not throw, but may return an Error that will be propagated back up the call chain.\n *\n * @param binder The Binder that performed the construction.\n * @param id The identifier for what was made.\n * @param maker The thing that made. Will be one of type ClassConstructor, SyncFactory, or AsyncFactory, depending on how you registered the binding.\n * @param value The thing that was made.\n * @returns one of 3 results...\n * An Error which will be propagated back up the call chain.\n * Undefined, which means the object is ready to be placed into service.\n * A Promise that resolves to one of the above two values (undefined or Error).\n */\nexport type OnSuccessCallback<T, M> = (value: T, injector: Injector, id: InjectableId<T>, maker: M) => Promise<Error | void> | Error | void;\n\n/**\n * An interface allowing binding of an error handler.\n *\n * @see OnErrorCallback\n */\nexport interface BindErrHandler<T, M> {\n\tonError(cb: OnErrorCallback<T, M>): void;\n}\n\n/**\n * An interface allowing binding of a post construction handler.\n *\n * @see OnSuccessCallback\n */\nexport interface BindHandler<T, M> extends BindErrHandler<T, M> {\n\tonSuccess(cb: OnSuccessCallback<T, M>): BindErrHandler<T, M>;\n}\n\n/**\n * @inheritDoc\n * This specialization also allows you to specify that the binding is 'Singleton' (e.g. only one in the system).\n */\nexport interface BindAs<T, M> extends BindHandler<T, M> {\n\tasSingleton(): BindHandler<T, M>;\n}\n\n/**\n * Bind Ids to producers.\n *\n * @deprecated {@link Binder} was removed from index.ts long ago will be removed in a future release.\n * Use {@link Container} to build a context root — it provides all binding methods directly.\n * For type annotations, use {@link Injector}, which {@link Container} implements.\n */\nexport interface Binder extends Injector {\n\n\t/**\n\t * Bind an InjectableId to a constant value.\n\t * Constants are by their very nature singleton, and are assumed to be error proof.\n\t */\n\tbindConstant<T>(id: InjectableId<T>, value: T): T;\n\n\t/**\n\t * Bind an InjectableId to a class (actually it's constructor).\n\t * As a shortcut, you may use the class constructor as the 'id' (e.g. container.bindClass(A); ).\n\t * The container will also invoke any `@PostConstruct` present on the class.\n\t */\n\tbindClass<T>(id: ClassConstructor<T>, constructor?: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;\n\n\tbindClass<T>(id: string | symbol | AbstractConstructor<T> | InjectableId<T>, constructor: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;\n\n\t/**\n\t * Bind an InjectableId to a synchronous factory that will be invoked on demand when the object is needed.\n\t * The factory should produce the needed value\n\t * NOTE: The container will not invoke any `@PostConstruct` present on the class, this is the responsibility of the factory.\n\t */\n\tbindFactory<T>(id: InjectableId<T>, factory: SyncFactory<T>): BindAs<T, SyncFactory<T>>;\n\n\t/**\n\t * Bind an InjectableId to an asynchronous factory that will be invoked on demand when the object is needed.\n\t * The factory should produce the needed value (asynchronously of course).\n\t * NOTE: The container will not invoke any `@PostConstruct` present on the class, this is the responsibility of the factory.\n\t * WARNING!!! The factory may not throw and must return a valid Promise (which can be pending, resolved, rejected, etc.).\n\t */\n\tbindAsyncFactory<T>(id: InjectableId<T>, factory: AsyncFactory<T>): BindAs<T, AsyncFactory<T>>;\n\n\t/**\n\t * This essentially pre creates/loads all *singleton* InjectableIds currently known to the Binder.\n\t * This *may* be helpful if you wish to use Injector.get on a dependency tree that has asynchronous singletons within the tree.\n\t *\n\t * @param asyncOnly Only resolve AsyncFactorys as well as any bound classes that have an asynchronous `@PostConstruct` decorator. WARNING: If true, SyncFactorys will *not* be resolved even if they are Singletons.\n\t * @param parentRecursion If true and the the container has a parent, resolveIfSingleton will first be called for the parent\n\t * @returns A Promise that resolves when all Singleton's have been resolved, OR rejects if one or more of the Singleton's failed to resolve. NOTE: Rejection does not occur until all Singleton resolutions have settled, and the rejection reason/err will be a Map<InjectableId, Error>\n\t */\n\tresolveSingletons(asyncOnly?: boolean, parentRecursion?: boolean): Promise<this>;\n}\n"]}
|
package/lib/esm/binder.js
DELETED
package/lib/esm/binder.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"binder.js","sourceRoot":"","sources":["../../src/binder.ts"],"names":[],"mappings":"","sourcesContent":["import {AbstractConstructor, ClassConstructor, InjectableId, Injector} from './injector';\n\n/**\n * Type definition for functions that return a value.\n * The function should return a valid value, but may throw an exception if it cannot.\n */\nexport type SyncFactory<T> = (injector: Injector) => T;\n\n/**\n * Type definition for functions that return a Promise for a value.\n * The function *must* not throw and must return a valid Promise (e.g. pending, resolved, rejected).\n */\nexport type AsyncFactory<T> = (injector: Injector) => Promise<T>;\n\n/**\n * You may bind an error handler which will be invoked if the bound InjectableId could not be put into service.\n * An error handler *must* not throw, but may return an Error that will be propagated back up the call chain.\n *\n * @param binder The Binder that experienced the error.\n * @param id The identifier for what was trying to be made.\n * @param maker The thing that made (or tried to provideAsState). Will be one of type ClassConstructor, SyncFactory, or AsyncFactory, depending on how you registered the binding.\n * @param error Identifies the problem that occurred.\n * @param value If the 'maker' was able to create the thing, but it had an error during post construction, the made thing will be passed here.\n * @returns one of 3 results...\n * A substitute thing (kind of like a 'maker' do-over) which must be fully operational (e.g. any `@PostConstruct` will be ignored).\n * An alternate Error which will be propagated back up the call chain.\n * Undefined, which means the 'error' parameter will be propagated back up the call chain.\n */\nexport type OnErrorCallback<T, M> = (injector: Injector, id: InjectableId<T>, maker: M, error: unknown, value?: T) => T | Error | void;\n\n/**\n * You may bind a success handler which will be invoked just before the bound InjectableId is put into service.\n * This is an alternative to the more preferred `@PostConstruct` decorator for scenarios when usage of that decorator is not feasible.\n * WARNING:\n * By registering a success handler, you override and nullify any `@PostConstruct` decorator on the class.\n * In such a scenario, the success handler should perform whatever care and feeding the class expected from the `@PostConstruct` decorator.\n * A success handler *must* not throw, but may return an Error that will be propagated back up the call chain.\n *\n * @param binder The Binder that performed the construction.\n * @param id The identifier for what was made.\n * @param maker The thing that made. Will be one of type ClassConstructor, SyncFactory, or AsyncFactory, depending on how you registered the binding.\n * @param value The thing that was made.\n * @returns one of 3 results...\n * An Error which will be propagated back up the call chain.\n * Undefined, which means the object is ready to be placed into service.\n * A Promise that resolves to one of the above two values (undefined or Error).\n */\nexport type OnSuccessCallback<T, M> = (value: T, injector: Injector, id: InjectableId<T>, maker: M) => Promise<Error | void> | Error | void;\n\n/**\n * An interface allowing binding of an error handler.\n *\n * @see OnErrorCallback\n */\nexport interface BindErrHandler<T, M> {\n\tonError(cb: OnErrorCallback<T, M>): void;\n}\n\n/**\n * An interface allowing binding of a post construction handler.\n *\n * @see OnSuccessCallback\n */\nexport interface BindHandler<T, M> extends BindErrHandler<T, M> {\n\tonSuccess(cb: OnSuccessCallback<T, M>): BindErrHandler<T, M>;\n}\n\n/**\n * @inheritDoc\n * This specialization also allows you to specify that the binding is 'Singleton' (e.g. only one in the system).\n */\nexport interface BindAs<T, M> extends BindHandler<T, M> {\n\tasSingleton(): BindHandler<T, M>;\n}\n\n/**\n * Bind Ids to producers.\n *\n * @deprecated {@link Binder} was removed from index.ts long ago will be removed in a future release.\n * Use {@link Container} to build a context root — it provides all binding methods directly.\n * For type annotations, use {@link Injector}, which {@link Container} implements.\n */\nexport interface Binder extends Injector {\n\n\t/**\n\t * Bind an InjectableId to a constant value.\n\t * Constants are by their very nature singleton, and are assumed to be error proof.\n\t */\n\tbindConstant<T>(id: InjectableId<T>, value: T): T;\n\n\t/**\n\t * Bind an InjectableId to a class (actually it's constructor).\n\t * As a shortcut, you may use the class constructor as the 'id' (e.g. container.bindClass(A); ).\n\t * The container will also invoke any `@PostConstruct` present on the class.\n\t */\n\tbindClass<T>(id: ClassConstructor<T>, constructor?: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;\n\n\tbindClass<T>(id: string | symbol | AbstractConstructor<T> | InjectableId<T>, constructor: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;\n\n\t/**\n\t * Bind an InjectableId to a synchronous factory that will be invoked on demand when the object is needed.\n\t * The factory should produce the needed value\n\t * NOTE: The container will not invoke any `@PostConstruct` present on the class, this is the responsibility of the factory.\n\t */\n\tbindFactory<T>(id: InjectableId<T>, factory: SyncFactory<T>): BindAs<T, SyncFactory<T>>;\n\n\t/**\n\t * Bind an InjectableId to an asynchronous factory that will be invoked on demand when the object is needed.\n\t * The factory should produce the needed value (asynchronously of course).\n\t * NOTE: The container will not invoke any `@PostConstruct` present on the class, this is the responsibility of the factory.\n\t * WARNING!!! The factory may not throw and must return a valid Promise (which can be pending, resolved, rejected, etc.).\n\t */\n\tbindAsyncFactory<T>(id: InjectableId<T>, factory: AsyncFactory<T>): BindAs<T, AsyncFactory<T>>;\n\n\t/**\n\t * This essentially pre creates/loads all *singleton* InjectableIds currently known to the Binder.\n\t * This *may* be helpful if you wish to use Injector.get on a dependency tree that has asynchronous singletons within the tree.\n\t *\n\t * @param asyncOnly Only resolve AsyncFactorys as well as any bound classes that have an asynchronous `@PostConstruct` decorator. WARNING: If true, SyncFactorys will *not* be resolved even if they are Singletons.\n\t * @param parentRecursion If true and the the container has a parent, resolveIfSingleton will first be called for the parent\n\t * @returns A Promise that resolves when all Singleton's have been resolved, OR rejects if one or more of the Singleton's failed to resolve. NOTE: Rejection does not occur until all Singleton resolutions have settled, and the rejection reason/err will be a Map<InjectableId, Error>\n\t */\n\tresolveSingletons(asyncOnly?: boolean, parentRecursion?: boolean): Promise<this>;\n}\n"]}
|