async-injection 1.5.3 → 1.5.4
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/Changelog.md +4 -0
- package/lib/cjs/class-provider.js +8 -5
- package/lib/cjs/class-provider.js.map +1 -1
- package/lib/cjs/decorators.js +1 -2
- package/lib/cjs/decorators.js.map +1 -1
- package/lib/esm/class-provider.js +8 -5
- package/lib/esm/class-provider.js.map +1 -1
- package/lib/esm/decorators.js +1 -2
- package/lib/esm/decorators.js.map +1 -1
- package/package.json +10 -10
package/Changelog.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
## 1.5.4 / 2023-11-15
|
|
2
|
+
* Improved handling of class constructor parameter type recognition.
|
|
3
|
+
* Update dependencies.
|
|
4
|
+
|
|
1
5
|
## 1.5.3 / 2023-06-05
|
|
2
6
|
* Address esm issues [#10](https://github.com/pcafstockf/async-injection/issues/10) and [#15](https://github.com/pcafstockf/async-injection/issues/15)
|
|
3
7
|
* Fix missing type overload on Binder interface.
|
|
@@ -99,13 +99,16 @@ class ClassBasedProvider extends bindable_provider_js_1.BindableProvider {
|
|
|
99
99
|
return [];
|
|
100
100
|
}
|
|
101
101
|
return argTypes.map((argType, index) => {
|
|
102
|
-
// The reflect-metadata API fails on circular dependencies
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
106
|
-
// Check if an Inject annotation precedes the parameter.
|
|
102
|
+
// The reflect-metadata API fails on circular dependencies returning undefined instead.
|
|
103
|
+
// Additionally, it cannot return generic types (no runtime type info).
|
|
104
|
+
// If an Inject annotation precedes the parameter, then that is what should get injected.
|
|
107
105
|
const overrideToken = (0, decorators_js_1._getInjectedIdAt)(this.maker, index);
|
|
106
|
+
// If there was no Inject annotation, we might still be able to determine what to inject using the 'argType' (aka Reflect design:paramtypes).
|
|
108
107
|
const actualToken = overrideToken === undefined ? argType : overrideToken;
|
|
108
|
+
if (actualToken === undefined) {
|
|
109
|
+
// No Inject annotation, and the type is not known.
|
|
110
|
+
throw new Error(`Injection error. Unable to determine parameter ${index} type/value of ${this.maker.toString()} constructor`);
|
|
111
|
+
}
|
|
109
112
|
// Ask our container to resolve the parameter.
|
|
110
113
|
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
111
114
|
let param = this.injector.resolveState(actualToken);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"class-provider.js","sourceRoot":"","sources":["../../src/class-provider.ts"],"names":[],"mappings":";;;AAAA,iEAAwD;AACxD,iDAAiH;AACjH,mDAAwE;AAExE,yCAAiC;AACjC,yCAAqC;AAYrC;;;GAGG;AACH,MAAa,kBAAsB,SAAQ,uCAAwC;IAClF,YAAY,QAAgC,EAAE,EAAmB,EAAE,KAA0B;QAC5F,KAAK,CAAC,QAAQ,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC;IAC5B,CAAC;IAED;;;;OAIG;IACH,cAAc;QACb,IAAI,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC;QAC5B,IAAI,CAAC,MAAM,EAAE;YACZ,MAAM,GAAG,IAAI,CAAC,kBAAkB,EAAE,CAAC;SACnC;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,CAAC,CAAC,SAAS,CAAC,IAAI,OAAO,CAAC,WAAW,CAAC,+CAAgC,EAAE,IAAI,CAAC,KAAK,CAAC;YACpF,OAAO,KAAK,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC;QACxC,OAAO,SAAS,CAAC;IAClB,CAAC;IAED;;OAEG;IACO,sBAAsB,CAAC,GAAM;;QACtC,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC,WAAW,EAA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DA4KC","sourcesContent":["import {BindableProvider} from './bindable-provider.js';\nimport {POSTCONSTRUCT_ASYNC_METADATA_KEY, POSTCONSTRUCT_SYNC_METADATA_KEY, REFLECT_PARAMS} from './constants.js';\nimport {_getInjectedIdAt, _getOptionalDefaultAt} from './decorators.js';\nimport {ClassConstructor, InjectableId, Injector} from './injector.js';\nimport {State} from './state.js';\nimport {isPromise} from './utils.js';\n\n/*\n * This is a bit of a hack, but it avoids a ton of alternative hacks.\n * Note that in the Container, resolveState is a protected method.\n * Injector was never meant to publicly expose State.\n * Gotta love JS!\n */\ninterface StateResolvingInjector extends Injector {\n\tresolveState<T>(id: InjectableId<T>): State<T>;\n}\n\n/**\n * @inheritDoc\n * This specialization invokes it's configured class constructor synchronously and then scans for (and invokes) any @PostConstruct (which may be synchronous or asynchronous).\n */\nexport class ClassBasedProvider<T> extends BindableProvider<T, ClassConstructor<T>> {\n\tconstructor(injector: StateResolvingInjector, id: InjectableId<T>, maker: ClassConstructor<T>) {\n\t\tsuper(injector, id, maker);\n\t}\n\n\t/**\n\t * @inheritDoc\n\t * @see the class description for this Provider.\n\t * This method is just a singleton guard, the real work is done by provideAsStateImpl.\n\t */\n\tprovideAsState(): State<T> {\n\t\tlet retVal = this.singleton;\n\t\tif (!retVal) {\n\t\t\tretVal = this.provideAsStateImpl();\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 if 'asyncOnly' is true and there is no asynchronous PostConstruct annotation (since class constructors can never by asynchronous).\n\t */\n\tresolveIfSingleton(asyncOnly: boolean): Promise<T> {\n\t\tif ((!asyncOnly) || Reflect.getMetadata(POSTCONSTRUCT_ASYNC_METADATA_KEY, this.maker))\n\t\t\treturn super.resolveIfSingleton(false);\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Make a resolved or pending State that reflects any @PostConstruct annotations.\n\t */\n\tprotected makePostConstructState(obj: T): State<T> {\n\t\tif (typeof obj === 'object' && (!Array.isArray(obj)) && obj.constructor) {\n\t\t\tlet maybeAsync = false;\n\t\t\tlet pcFn: () => void | Error | Promise<void | Error>;\n\t\t\tif (typeof this.successHandler === 'function') {\n\t\t\t\tmaybeAsync = true;\n\t\t\t\tpcFn = () => {\n\t\t\t\t\treturn this.successHandler(obj, this.injector, this.id, this.maker);\n\t\t\t\t};\n\t\t\t}\n\t\t\telse {\n\t\t\t\t/* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */\n\t\t\t\t// Check to see if there is a @PostConstruct annotation on a method of the class.\n\t\t\t\tlet postConstruct: string = Reflect.getMetadata(POSTCONSTRUCT_SYNC_METADATA_KEY, obj.constructor);\n\t\t\t\tif (!postConstruct) {\n\t\t\t\t\tmaybeAsync = true;\n\t\t\t\t\tpostConstruct = Reflect.getMetadata(POSTCONSTRUCT_ASYNC_METADATA_KEY, obj.constructor);\n\t\t\t\t}\n\t\t\t\tif (postConstruct && obj.constructor.prototype[postConstruct] && typeof obj.constructor.prototype[postConstruct] === 'function')\n\t\t\t\t\tpcFn = obj[postConstruct].bind?.(obj);\n\n\t\t\t\t/* eslint-enable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */\n\t\t\t}\n\t\t\tif (pcFn) {\n\t\t\t\tlet result: any;\n\t\t\t\ttry {\n\t\t\t\t\tresult = pcFn();\n\t\t\t\t}\n\t\t\t\tcatch (err) {\n\t\t\t\t\t// The post construction method threw while executing, give the errorHandler (if any) a crack at recovery.\n\t\t\t\t\ttry {\n\t\t\t\t\t\tobj = this.queryErrorHandler(err, obj); // The returned obj is unlikely to be the original obj.\n\t\t\t\t\t\treturn State.MakeState<T>(null, undefined, obj);\n\t\t\t\t\t}\n\t\t\t\t\tcatch (e) {\n\t\t\t\t\t\t// could not recover, propagate the error.\n\t\t\t\t\t\treturn State.MakeState<T>(null, e, undefined);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t// The post construction method says it will let us know when it's finished.\n\t\t\t\tif (result && (result instanceof Promise || (maybeAsync && isPromise<void>(result)))) {\n\t\t\t\t\t// Return a State that is pending (the other return statements in this method return a State which is resolved or rejected).\n\t\t\t\t\t/* eslint-disable @typescript-eslint/no-unsafe-argument */\n\t\t\t\t\treturn State.MakeState<T>(this.makePromiseForObj<T>(result, () => obj));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t// No PostConstruct, just return a resolved State\n\t\treturn State.MakeState<T>(null, undefined, obj);\n\t}\n\n\t/**\n\t * This method collects the States of all the constructor parameters for our target class.\n\t */\n\tprotected getConstructorParameterStates(): State[] {\n\t\tconst argTypes = Reflect.getMetadata(REFLECT_PARAMS, this.maker);\n\t\tif (argTypes === undefined || !Array.isArray(argTypes)) {\n\t\t\treturn [];\n\t\t}\n\t\treturn argTypes.map((argType, index) => {\n\t\t\t// The reflect-metadata API fails on circular dependencies, and will return undefined for the argument instead.\n\t\t\tif (argType === undefined) {\n\t\t\t\tthrow new Error(`Injection error. Recursive dependency in constructor for ${this.maker.toString()} at index ${index}`);\n\t\t\t}\n\t\t\t// Check if an Inject annotation precedes the parameter.\n\t\t\tconst overrideToken = _getInjectedIdAt(this.maker, index);\n\t\t\tconst actualToken = overrideToken === undefined ? argType : overrideToken;\n\t\t\t// Ask our container to resolve the parameter.\n\t\t\t/* eslint-disable @typescript-eslint/no-unsafe-argument */\n\t\t\tlet param = (this.injector as StateResolvingInjector).resolveState(actualToken);\n\t\t\t// If the parameter could not be resolved, see if there is an @Optional annotation\n\t\t\tif ((!param.pending) && param.rejected) {\n\t\t\t\tconst md = _getOptionalDefaultAt(this.maker, index);\n\t\t\t\tif (md)\n\t\t\t\t\tparam = State.MakeState<any>(null, undefined, md.value);\n\t\t\t}\n\t\t\treturn param;\n\t\t});\n\t}\n\n\t/**\n\t * Gather the needed constructor parameters, invoke the constructor, and figure out what post construction needs done.\n\t */\n\tprivate provideAsStateImpl(): State<T> {\n\t\tconst params = this.getConstructorParameterStates();\n\n\t\t// If any of the params are in a rejected state, we cannot construct.\n\t\tconst paramRejection = params.find((p) => {\n\t\t\treturn (!p.pending) && p.rejected;\n\t\t});\n\t\tif (paramRejection) {\n\t\t\treturn paramRejection as State<T>;\n\t\t}\n\t\t// If any of the params are in a pending state, we will have to wait for them to be resolved before we can construct.\n\t\tconst pendingParams = params.filter((p) => {\n\t\t\treturn p.pending;\n\t\t}).map((p) => {\n\t\t\treturn p.promise;\n\t\t});\n\t\tif (pendingParams.length > 0) {\n\t\t\t// Some of the parameters needed for construction are not yet available, wait for them and then attempt construction.\n\t\t\tconst objPromise = this.makePromiseForObj<any[]>(Promise.all(pendingParams), () => {\n\t\t\t\t// All the parameters are now available, instantiate the class.\n\t\t\t\t// If this throws, it will be handled by our caller.\n\t\t\t\treturn Reflect.construct(this.maker, params.map((p) => p.fulfilled as unknown));\n\t\t\t});\n\t\t\t// Once the obj is resolved, then we need to check for PostConstruct and if it was async, wait for that too.\n\t\t\treturn State.MakeState<T>(objPromise.then((obj) => {\n\t\t\t\tconst state = this.makePostConstructState(obj);\n\t\t\t\tif (state.pending) {\n\t\t\t\t\treturn state.promise; // chain (aka wait some more).\n\t\t\t\t}\n\t\t\t\telse if (state.rejected) {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-return\n\t\t\t\t\treturn state.rejected as any; // error\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\treturn state.fulfilled; // value (aka obj).\n\t\t\t\t}\n\t\t\t}));\n\t\t}\n\t\telse {\n\t\t\t// All parameters needed for construction are available, instantiate the object.\n\t\t\ttry {\n\t\t\t\tconst newObj = Reflect.construct(this.maker, params.map((p) => p.fulfilled as unknown));\n\t\t\t\treturn this.makePostConstructState(newObj);\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\treturn 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\treturn State.MakeState<T>(null, e, undefined);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"class-provider.js","sourceRoot":"","sources":["../../src/class-provider.ts"],"names":[],"mappings":";;;AAAA,iEAAwD;AACxD,iDAAiH;AACjH,mDAAwE;AAExE,yCAAiC;AACjC,yCAAqC;AAYrC;;;GAGG;AACH,MAAa,kBAAsB,SAAQ,uCAAwC;IAClF,YAAY,QAAgC,EAAE,EAAmB,EAAE,KAA0B;QAC5F,KAAK,CAAC,QAAQ,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC;IAC5B,CAAC;IAED;;;;OAIG;IACH,cAAc;QACb,IAAI,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC;QAC5B,IAAI,CAAC,MAAM,EAAE;YACZ,MAAM,GAAG,IAAI,CAAC,kBAAkB,EAAE,CAAC;SACnC;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,CAAC,CAAC,SAAS,CAAC,IAAI,OAAO,CAAC,WAAW,CAAC,+CAAgC,EAAE,IAAI,CAAC,KAAK,CAAC;YACpF,OAAO,KAAK,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC;QACxC,OAAO,SAAS,CAAC;IAClB,CAAC;IAED;;OAEG;IACO,sBAAsB,CAAC,GAAM;;QACtC,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC,WAAW,EAAE;YACxE,IAAI,UAAU,GAAG,KAAK,CAAC;YACvB,IAAI,IAAgD,CAAC;YACrD,IAAI,OAAO,IAAI,C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\nimport {POSTCONSTRUCT_ASYNC_METADATA_KEY, POSTCONSTRUCT_SYNC_METADATA_KEY, REFLECT_PARAMS} from './constants.js';\nimport {_getInjectedIdAt, _getOptionalDefaultAt} from './decorators.js';\nimport {ClassConstructor, InjectableId, Injector} from './injector.js';\nimport {State} from './state.js';\nimport {isPromise} from './utils.js';\n\n/*\n * This is a bit of a hack, but it avoids a ton of alternative hacks.\n * Note that in the Container, resolveState is a protected method.\n * Injector was never meant to publicly expose State.\n * Gotta love JS!\n */\ninterface StateResolvingInjector extends Injector {\n\tresolveState<T>(id: InjectableId<T>): State<T>;\n}\n\n/**\n * @inheritDoc\n * This specialization invokes it's configured class constructor synchronously and then scans for (and invokes) any @PostConstruct (which may be synchronous or asynchronous).\n */\nexport class ClassBasedProvider<T> extends BindableProvider<T, ClassConstructor<T>> {\n\tconstructor(injector: StateResolvingInjector, id: InjectableId<T>, maker: ClassConstructor<T>) {\n\t\tsuper(injector, id, maker);\n\t}\n\n\t/**\n\t * @inheritDoc\n\t * @see the class description for this Provider.\n\t * This method is just a singleton guard, the real work is done by provideAsStateImpl.\n\t */\n\tprovideAsState(): State<T> {\n\t\tlet retVal = this.singleton;\n\t\tif (!retVal) {\n\t\t\tretVal = this.provideAsStateImpl();\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 if 'asyncOnly' is true and there is no asynchronous PostConstruct annotation (since class constructors can never by asynchronous).\n\t */\n\tresolveIfSingleton(asyncOnly: boolean): Promise<T> {\n\t\tif ((!asyncOnly) || Reflect.getMetadata(POSTCONSTRUCT_ASYNC_METADATA_KEY, this.maker))\n\t\t\treturn super.resolveIfSingleton(false);\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Make a resolved or pending State that reflects any @PostConstruct annotations.\n\t */\n\tprotected makePostConstructState(obj: T): State<T> {\n\t\tif (typeof obj === 'object' && (!Array.isArray(obj)) && obj.constructor) {\n\t\t\tlet maybeAsync = false;\n\t\t\tlet pcFn: () => void | Error | Promise<void | Error>;\n\t\t\tif (typeof this.successHandler === 'function') {\n\t\t\t\tmaybeAsync = true;\n\t\t\t\tpcFn = () => {\n\t\t\t\t\treturn this.successHandler(obj, this.injector, this.id, this.maker);\n\t\t\t\t};\n\t\t\t}\n\t\t\telse {\n\t\t\t\t/* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */\n\t\t\t\t// Check to see if there is a @PostConstruct annotation on a method of the class.\n\t\t\t\tlet postConstruct: string = Reflect.getMetadata(POSTCONSTRUCT_SYNC_METADATA_KEY, obj.constructor);\n\t\t\t\tif (!postConstruct) {\n\t\t\t\t\tmaybeAsync = true;\n\t\t\t\t\tpostConstruct = Reflect.getMetadata(POSTCONSTRUCT_ASYNC_METADATA_KEY, obj.constructor);\n\t\t\t\t}\n\t\t\t\tif (postConstruct && obj.constructor.prototype[postConstruct] && typeof obj.constructor.prototype[postConstruct] === 'function')\n\t\t\t\t\tpcFn = obj[postConstruct].bind?.(obj);\n\n\t\t\t\t/* eslint-enable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */\n\t\t\t}\n\t\t\tif (pcFn) {\n\t\t\t\tlet result: any;\n\t\t\t\ttry {\n\t\t\t\t\tresult = pcFn();\n\t\t\t\t}\n\t\t\t\tcatch (err) {\n\t\t\t\t\t// The post construction method threw while executing, give the errorHandler (if any) a crack at recovery.\n\t\t\t\t\ttry {\n\t\t\t\t\t\tobj = this.queryErrorHandler(err, obj); // The returned obj is unlikely to be the original obj.\n\t\t\t\t\t\treturn State.MakeState<T>(null, undefined, obj);\n\t\t\t\t\t}\n\t\t\t\t\tcatch (e) {\n\t\t\t\t\t\t// could not recover, propagate the error.\n\t\t\t\t\t\treturn State.MakeState<T>(null, e, undefined);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t// The post construction method says it will let us know when it's finished.\n\t\t\t\tif (result && (result instanceof Promise || (maybeAsync && isPromise<void>(result)))) {\n\t\t\t\t\t// Return a State that is pending (the other return statements in this method return a State which is resolved or rejected).\n\t\t\t\t\t/* eslint-disable @typescript-eslint/no-unsafe-argument */\n\t\t\t\t\treturn State.MakeState<T>(this.makePromiseForObj<T>(result, () => obj));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t// No PostConstruct, just return a resolved State\n\t\treturn State.MakeState<T>(null, undefined, obj);\n\t}\n\n\t/**\n\t * This method collects the States of all the constructor parameters for our target class.\n\t */\n\tprotected getConstructorParameterStates(): State[] {\n\t\tconst argTypes = Reflect.getMetadata(REFLECT_PARAMS, this.maker);\n\t\tif (argTypes === undefined || !Array.isArray(argTypes)) {\n\t\t\treturn [];\n\t\t}\n\t\treturn argTypes.map((argType, index) => {\n\t\t\t// The reflect-metadata API fails on circular dependencies returning undefined instead.\n\t\t\t// Additionally, it cannot return generic types (no runtime type info).\n\t\t\t// If an Inject annotation precedes the parameter, then that is what should get injected.\n\t\t\tconst overrideToken = _getInjectedIdAt(this.maker, index);\n\t\t\t// If there was no Inject annotation, we might still be able to determine what to inject using the 'argType' (aka Reflect design:paramtypes).\n\t\t\tconst actualToken = overrideToken === undefined ? argType : overrideToken;\n\t\t\tif (actualToken === undefined) {\n\t\t\t\t// No Inject annotation, and the type is not known.\n\t\t\t\tthrow new Error(`Injection error. Unable to determine parameter ${index} type/value of ${this.maker.toString()} constructor`);\n\t\t\t}\n\t\t\t// Ask our container to resolve the parameter.\n\t\t\t/* eslint-disable @typescript-eslint/no-unsafe-argument */\n\t\t\tlet param = (this.injector as StateResolvingInjector).resolveState(actualToken);\n\t\t\t// If the parameter could not be resolved, see if there is an @Optional annotation\n\t\t\tif ((!param.pending) && param.rejected) {\n\t\t\t\tconst md = _getOptionalDefaultAt(this.maker, index);\n\t\t\t\tif (md)\n\t\t\t\t\tparam = State.MakeState<any>(null, undefined, md.value);\n\t\t\t}\n\t\t\treturn param;\n\t\t});\n\t}\n\n\t/**\n\t * Gather the needed constructor parameters, invoke the constructor, and figure out what post construction needs done.\n\t */\n\tprivate provideAsStateImpl(): State<T> {\n\t\tconst params = this.getConstructorParameterStates();\n\n\t\t// If any of the params are in a rejected state, we cannot construct.\n\t\tconst paramRejection = params.find((p) => {\n\t\t\treturn (!p.pending) && p.rejected;\n\t\t});\n\t\tif (paramRejection) {\n\t\t\treturn paramRejection as State<T>;\n\t\t}\n\t\t// If any of the params are in a pending state, we will have to wait for them to be resolved before we can construct.\n\t\tconst pendingParams = params.filter((p) => {\n\t\t\treturn p.pending;\n\t\t}).map((p) => {\n\t\t\treturn p.promise;\n\t\t});\n\t\tif (pendingParams.length > 0) {\n\t\t\t// Some of the parameters needed for construction are not yet available, wait for them and then attempt construction.\n\t\t\tconst objPromise = this.makePromiseForObj<any[]>(Promise.all(pendingParams), () => {\n\t\t\t\t// All the parameters are now available, instantiate the class.\n\t\t\t\t// If this throws, it will be handled by our caller.\n\t\t\t\treturn Reflect.construct(this.maker, params.map((p) => p.fulfilled as unknown));\n\t\t\t});\n\t\t\t// Once the obj is resolved, then we need to check for PostConstruct and if it was async, wait for that too.\n\t\t\treturn State.MakeState<T>(objPromise.then((obj) => {\n\t\t\t\tconst state = this.makePostConstructState(obj);\n\t\t\t\tif (state.pending) {\n\t\t\t\t\treturn state.promise; // chain (aka wait some more).\n\t\t\t\t}\n\t\t\t\telse if (state.rejected) {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-return\n\t\t\t\t\treturn state.rejected as any; // error\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\treturn state.fulfilled; // value (aka obj).\n\t\t\t\t}\n\t\t\t}));\n\t\t}\n\t\telse {\n\t\t\t// All parameters needed for construction are available, instantiate the object.\n\t\t\ttry {\n\t\t\t\tconst newObj = Reflect.construct(this.maker, params.map((p) => p.fulfilled as unknown));\n\t\t\t\treturn this.makePostConstructState(newObj);\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\treturn 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\treturn State.MakeState<T>(null, e, undefined);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n"]}
|
package/lib/cjs/decorators.js
CHANGED
|
@@ -76,9 +76,8 @@ function Inject(id) {
|
|
|
76
76
|
* @returns Undefined (nothing), as this decorator does not modify the parameter in any way.
|
|
77
77
|
*/
|
|
78
78
|
return function (target, parameterName, parameterIndex) {
|
|
79
|
-
const hint = targetHint(target);
|
|
80
79
|
if (id === undefined) {
|
|
81
|
-
throw new Error('Undefined id passed to @Inject [' +
|
|
80
|
+
throw new Error('Undefined id passed to @Inject [' + targetHint(target) + ']');
|
|
82
81
|
}
|
|
83
82
|
const paramKey = validateSingleConstructorParam('Inject', target, parameterIndex);
|
|
84
83
|
Reflect.defineMetadata(constants_js_1.INJECT_METADATA_KEY, id, target, paramKey);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decorators.js","sourceRoot":"","sources":["../../src/decorators.ts"],"names":[],"mappings":";;;AAAA,iDAAiD;AACjD;;GAEG;AACH,iDAA4M;AAG5M,0CAA0C;AAC1C,SAAS,UAAU,CAAC,MAAgB;IACnC,IAAI,IAAwB,CAAC;IAC7B,IAAI,MAAM,EAAE;QACX,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;QACnB,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,MAAM,CAAC,WAAW,EAAE;YAClC,IAAI,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC;SAC/B;KACD;IACD,OAAO,IAAI,CAAC;AACb,CAAC;AAED,0DAA0D;AAC1D,SAAS,kBAAkB,CAAC,MAAW;IACtC,yGAAyG;IACzG,IAAI,OAAO,MAAM,KAAK,UAAU,IAAI,MAAM,CAAC,cA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CAAC,WAAW,KAAK,UAAU,EAAE;YAC3E,MAAM,IAAI,KAAK,CAAC,2CAA2C,GAAG,MAAM,CAAC,QAAQ,EAAE,GAAG,GAAG,GAAG,UAAU,CAAC,QAAQ,EAAE,GAAG,GAAG,CAAC,CAAC;SACrH;QACD,IAAI,OAAO,CAAC,cAAc,CAAC,mCAAoB,EAAE,MAAM,CAAC,WAAW,CAAC,EAAE;YACrE,MAAM,IAAI,KAAK,CAAC,mCAAmC,GAAG,UAAU,CAAC,MAAM,CAAC,WAAW,CAAC,GAAG,GAAG,CAAC,CAAC;SAC5F;QACD,OAAO,CAAC,cAAc,CAAC,mCAAoB,EAAE,UAAU,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC;IAC9E,CAAC,CAAC;AACH,CAAC;AAjBD,0BAiBC","sourcesContent":["/* eslint-disable @typescript-eslint/ban-types */\n/**\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_RETURN, RELEASE_METADATA_KEY} from './constants.js';\nimport {InjectableId} from './injector.js';\n\n// Help user locate misapplied decorators.\nfunction targetHint(target: Function) {\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\t// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access\n\tif (typeof target === 'function' && target.hasOwnProperty('prototype')) {\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\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: Function, parameterName: string | symbol, parameterIndex: number): void {\n\t\tconst hint = targetHint(target);\n\t\tif (id === undefined) {\n\t\t\tthrow new Error('Undefined id passed to @Inject [' + hint + ']');\n\t\t}\n\t\tconst paramKey = validateSingleConstructorParam('Inject', target, 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 { // eslint-disable-line @typescript-eslint/explicit-module-boundary-types\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: Function, parameterName: string | symbol, parameterIndex: number): void {\n\t\tconst paramKey = validateSingleConstructorParam('Optional', target, 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) { // eslint-disable-line @typescript-eslint/no-unused-vars\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) { // eslint-disable-line @typescript-eslint/no-unused-vars\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,iDAAiD;AACjD;;GAEG;AACH,iDAA4M;AAG5M,0CAA0C;AAC1C,SAAS,UAAU,CAAC,MAAgB;IACnC,IAAI,IAAwB,CAAC;IAC7B,IAAI,MAAM,EAAE;QACX,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;QACnB,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,MAAM,CAAC,WAAW,EAAE;YAClC,IAAI,GAA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;;;;;GAWG;AACH,SAAgB,OAAO;IACtB;;;;;OAKG;IACH,oCAAoC;IACpC,OAAO,UAAU,MAAc,EAAE,UAA2B,EAAE,UAA8B;QAC3F,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,OAAO,MAAM,CAAC,WAAW,KAAK,UAAU,EAAE;YAC3E,MAAM,IAAI,KAAK,CAAC,2CAA2C,GAAG,MAAM,CAAC,QAAQ,EAAE,GAAG,GAAG,GAAG,UAAU,CAAC,QAAQ,EAAE,GAAG,GAAG,CAAC,CAAC;SACrH;QACD,IAAI,OAAO,CAAC,cAAc,CAAC,mCAAoB,EAAE,MAAM,CAAC,WAAW,CAAC,EAAE;YACrE,MAAM,IAAI,KAAK,CAAC,mCAAmC,GAAG,UAAU,CAAC,MAAM,CAAC,WAAW,CAAC,GAAG,GAAG,CAAC,CAAC;SAC5F;QACD,OAAO,CAAC,cAAc,CAAC,mCAAoB,EAAE,UAAU,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC;IAC9E,CAAC,CAAC;AACH,CAAC;AAjBD,0BAiBC","sourcesContent":["/* eslint-disable @typescript-eslint/ban-types */\n/**\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_RETURN, RELEASE_METADATA_KEY} from './constants.js';\nimport {InjectableId} from './injector.js';\n\n// Help user locate misapplied decorators.\nfunction targetHint(target: Function) {\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\t// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access\n\tif (typeof target === 'function' && target.hasOwnProperty('prototype')) {\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\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: Function, parameterName: string | symbol, parameterIndex: number): void {\n\t\tif (id === undefined) {\n\t\t\tthrow new Error('Undefined id passed to @Inject [' + targetHint(target) + ']');\n\t\t}\n\t\tconst paramKey = validateSingleConstructorParam('Inject', target, 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 { // eslint-disable-line @typescript-eslint/explicit-module-boundary-types\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: Function, parameterName: string | symbol, parameterIndex: number): void {\n\t\tconst paramKey = validateSingleConstructorParam('Optional', target, 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) { // eslint-disable-line @typescript-eslint/no-unused-vars\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) { // eslint-disable-line @typescript-eslint/no-unused-vars\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"]}
|
|
@@ -96,13 +96,16 @@ export class ClassBasedProvider extends BindableProvider {
|
|
|
96
96
|
return [];
|
|
97
97
|
}
|
|
98
98
|
return argTypes.map((argType, index) => {
|
|
99
|
-
// The reflect-metadata API fails on circular dependencies
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
// Check if an Inject annotation precedes the parameter.
|
|
99
|
+
// The reflect-metadata API fails on circular dependencies returning undefined instead.
|
|
100
|
+
// Additionally, it cannot return generic types (no runtime type info).
|
|
101
|
+
// If an Inject annotation precedes the parameter, then that is what should get injected.
|
|
104
102
|
const overrideToken = _getInjectedIdAt(this.maker, index);
|
|
103
|
+
// If there was no Inject annotation, we might still be able to determine what to inject using the 'argType' (aka Reflect design:paramtypes).
|
|
105
104
|
const actualToken = overrideToken === undefined ? argType : overrideToken;
|
|
105
|
+
if (actualToken === undefined) {
|
|
106
|
+
// No Inject annotation, and the type is not known.
|
|
107
|
+
throw new Error(`Injection error. Unable to determine parameter ${index} type/value of ${this.maker.toString()} constructor`);
|
|
108
|
+
}
|
|
106
109
|
// Ask our container to resolve the parameter.
|
|
107
110
|
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
108
111
|
let param = this.injector.resolveState(actualToken);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"class-provider.js","sourceRoot":"","sources":["../../src/class-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,gBAAgB,EAAC,MAAM,wBAAwB,CAAC;AACxD,OAAO,EAAC,gCAAgC,EAAE,+BAA+B,EAAE,cAAc,EAAC,MAAM,gBAAgB,CAAC;AACjH,OAAO,EAAC,gBAAgB,EAAE,qBAAqB,EAAC,MAAM,iBAAiB,CAAC;AAExE,OAAO,EAAC,KAAK,EAAC,MAAM,YAAY,CAAC;AACjC,OAAO,EAAC,SAAS,EAAC,MAAM,YAAY,CAAC;AAYrC;;;GAGG;AACH,MAAM,OAAO,kBAAsB,SAAQ,gBAAwC;IAClF,YAAY,QAAgC,EAAE,EAAmB,EAAE,KAA0B;QAC5F,KAAK,CAAC,QAAQ,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC;IAC5B,CAAC;IAED;;;;OAIG;IACH,cAAc;QACb,IAAI,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC;QAC5B,IA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F,IAAI;gBACH,MAAM,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,SAAoB,CAAC,CAAC,CAAC;gBACxF,OAAO,IAAI,CAAC,sBAAsB,CAAC,MAAM,CAAC,CAAC;aAC3C;YACD,OAAO,GAAG,EAAE;gBACX,0EAA0E;gBAC1E,IAAI;oBACH,OAAO,KAAK,CAAC,SAAS,CAAI,IAAI,EAAE,SAAS,EAAE,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC,CAAC,CAAC;iBACxE;gBACD,OAAO,CAAC,EAAE;oBACT,0CAA0C;oBAC1C,OAAO,KAAK,CAAC,SAAS,CAAI,IAAI,EAAE,CAAC,EAAE,SAAS,CAAC,CAAC;iBAC9C;aACD;SACD;IACF,CAAC;CACD","sourcesContent":["import {BindableProvider} from './bindable-provider.js';\nimport {POSTCONSTRUCT_ASYNC_METADATA_KEY, POSTCONSTRUCT_SYNC_METADATA_KEY, REFLECT_PARAMS} from './constants.js';\nimport {_getInjectedIdAt, _getOptionalDefaultAt} from './decorators.js';\nimport {ClassConstructor, InjectableId, Injector} from './injector.js';\nimport {State} from './state.js';\nimport {isPromise} from './utils.js';\n\n/*\n * This is a bit of a hack, but it avoids a ton of alternative hacks.\n * Note that in the Container, resolveState is a protected method.\n * Injector was never meant to publicly expose State.\n * Gotta love JS!\n */\ninterface StateResolvingInjector extends Injector {\n\tresolveState<T>(id: InjectableId<T>): State<T>;\n}\n\n/**\n * @inheritDoc\n * This specialization invokes it's configured class constructor synchronously and then scans for (and invokes) any @PostConstruct (which may be synchronous or asynchronous).\n */\nexport class ClassBasedProvider<T> extends BindableProvider<T, ClassConstructor<T>> {\n\tconstructor(injector: StateResolvingInjector, id: InjectableId<T>, maker: ClassConstructor<T>) {\n\t\tsuper(injector, id, maker);\n\t}\n\n\t/**\n\t * @inheritDoc\n\t * @see the class description for this Provider.\n\t * This method is just a singleton guard, the real work is done by provideAsStateImpl.\n\t */\n\tprovideAsState(): State<T> {\n\t\tlet retVal = this.singleton;\n\t\tif (!retVal) {\n\t\t\tretVal = this.provideAsStateImpl();\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 if 'asyncOnly' is true and there is no asynchronous PostConstruct annotation (since class constructors can never by asynchronous).\n\t */\n\tresolveIfSingleton(asyncOnly: boolean): Promise<T> {\n\t\tif ((!asyncOnly) || Reflect.getMetadata(POSTCONSTRUCT_ASYNC_METADATA_KEY, this.maker))\n\t\t\treturn super.resolveIfSingleton(false);\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Make a resolved or pending State that reflects any @PostConstruct annotations.\n\t */\n\tprotected makePostConstructState(obj: T): State<T> {\n\t\tif (typeof obj === 'object' && (!Array.isArray(obj)) && obj.constructor) {\n\t\t\tlet maybeAsync = false;\n\t\t\tlet pcFn: () => void | Error | Promise<void | Error>;\n\t\t\tif (typeof this.successHandler === 'function') {\n\t\t\t\tmaybeAsync = true;\n\t\t\t\tpcFn = () => {\n\t\t\t\t\treturn this.successHandler(obj, this.injector, this.id, this.maker);\n\t\t\t\t};\n\t\t\t}\n\t\t\telse {\n\t\t\t\t/* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */\n\t\t\t\t// Check to see if there is a @PostConstruct annotation on a method of the class.\n\t\t\t\tlet postConstruct: string = Reflect.getMetadata(POSTCONSTRUCT_SYNC_METADATA_KEY, obj.constructor);\n\t\t\t\tif (!postConstruct) {\n\t\t\t\t\tmaybeAsync = true;\n\t\t\t\t\tpostConstruct = Reflect.getMetadata(POSTCONSTRUCT_ASYNC_METADATA_KEY, obj.constructor);\n\t\t\t\t}\n\t\t\t\tif (postConstruct && obj.constructor.prototype[postConstruct] && typeof obj.constructor.prototype[postConstruct] === 'function')\n\t\t\t\t\tpcFn = obj[postConstruct].bind?.(obj);\n\n\t\t\t\t/* eslint-enable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */\n\t\t\t}\n\t\t\tif (pcFn) {\n\t\t\t\tlet result: any;\n\t\t\t\ttry {\n\t\t\t\t\tresult = pcFn();\n\t\t\t\t}\n\t\t\t\tcatch (err) {\n\t\t\t\t\t// The post construction method threw while executing, give the errorHandler (if any) a crack at recovery.\n\t\t\t\t\ttry {\n\t\t\t\t\t\tobj = this.queryErrorHandler(err, obj); // The returned obj is unlikely to be the original obj.\n\t\t\t\t\t\treturn State.MakeState<T>(null, undefined, obj);\n\t\t\t\t\t}\n\t\t\t\t\tcatch (e) {\n\t\t\t\t\t\t// could not recover, propagate the error.\n\t\t\t\t\t\treturn State.MakeState<T>(null, e, undefined);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t// The post construction method says it will let us know when it's finished.\n\t\t\t\tif (result && (result instanceof Promise || (maybeAsync && isPromise<void>(result)))) {\n\t\t\t\t\t// Return a State that is pending (the other return statements in this method return a State which is resolved or rejected).\n\t\t\t\t\t/* eslint-disable @typescript-eslint/no-unsafe-argument */\n\t\t\t\t\treturn State.MakeState<T>(this.makePromiseForObj<T>(result, () => obj));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t// No PostConstruct, just return a resolved State\n\t\treturn State.MakeState<T>(null, undefined, obj);\n\t}\n\n\t/**\n\t * This method collects the States of all the constructor parameters for our target class.\n\t */\n\tprotected getConstructorParameterStates(): State[] {\n\t\tconst argTypes = Reflect.getMetadata(REFLECT_PARAMS, this.maker);\n\t\tif (argTypes === undefined || !Array.isArray(argTypes)) {\n\t\t\treturn [];\n\t\t}\n\t\treturn argTypes.map((argType, index) => {\n\t\t\t// The reflect-metadata API fails on circular dependencies, and will return undefined for the argument instead.\n\t\t\tif (argType === undefined) {\n\t\t\t\tthrow new Error(`Injection error. Recursive dependency in constructor for ${this.maker.toString()} at index ${index}`);\n\t\t\t}\n\t\t\t// Check if an Inject annotation precedes the parameter.\n\t\t\tconst overrideToken = _getInjectedIdAt(this.maker, index);\n\t\t\tconst actualToken = overrideToken === undefined ? argType : overrideToken;\n\t\t\t// Ask our container to resolve the parameter.\n\t\t\t/* eslint-disable @typescript-eslint/no-unsafe-argument */\n\t\t\tlet param = (this.injector as StateResolvingInjector).resolveState(actualToken);\n\t\t\t// If the parameter could not be resolved, see if there is an @Optional annotation\n\t\t\tif ((!param.pending) && param.rejected) {\n\t\t\t\tconst md = _getOptionalDefaultAt(this.maker, index);\n\t\t\t\tif (md)\n\t\t\t\t\tparam = State.MakeState<any>(null, undefined, md.value);\n\t\t\t}\n\t\t\treturn param;\n\t\t});\n\t}\n\n\t/**\n\t * Gather the needed constructor parameters, invoke the constructor, and figure out what post construction needs done.\n\t */\n\tprivate provideAsStateImpl(): State<T> {\n\t\tconst params = this.getConstructorParameterStates();\n\n\t\t// If any of the params are in a rejected state, we cannot construct.\n\t\tconst paramRejection = params.find((p) => {\n\t\t\treturn (!p.pending) && p.rejected;\n\t\t});\n\t\tif (paramRejection) {\n\t\t\treturn paramRejection as State<T>;\n\t\t}\n\t\t// If any of the params are in a pending state, we will have to wait for them to be resolved before we can construct.\n\t\tconst pendingParams = params.filter((p) => {\n\t\t\treturn p.pending;\n\t\t}).map((p) => {\n\t\t\treturn p.promise;\n\t\t});\n\t\tif (pendingParams.length > 0) {\n\t\t\t// Some of the parameters needed for construction are not yet available, wait for them and then attempt construction.\n\t\t\tconst objPromise = this.makePromiseForObj<any[]>(Promise.all(pendingParams), () => {\n\t\t\t\t// All the parameters are now available, instantiate the class.\n\t\t\t\t// If this throws, it will be handled by our caller.\n\t\t\t\treturn Reflect.construct(this.maker, params.map((p) => p.fulfilled as unknown));\n\t\t\t});\n\t\t\t// Once the obj is resolved, then we need to check for PostConstruct and if it was async, wait for that too.\n\t\t\treturn State.MakeState<T>(objPromise.then((obj) => {\n\t\t\t\tconst state = this.makePostConstructState(obj);\n\t\t\t\tif (state.pending) {\n\t\t\t\t\treturn state.promise; // chain (aka wait some more).\n\t\t\t\t}\n\t\t\t\telse if (state.rejected) {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-return\n\t\t\t\t\treturn state.rejected as any; // error\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\treturn state.fulfilled; // value (aka obj).\n\t\t\t\t}\n\t\t\t}));\n\t\t}\n\t\telse {\n\t\t\t// All parameters needed for construction are available, instantiate the object.\n\t\t\ttry {\n\t\t\t\tconst newObj = Reflect.construct(this.maker, params.map((p) => p.fulfilled as unknown));\n\t\t\t\treturn this.makePostConstructState(newObj);\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\treturn 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\treturn State.MakeState<T>(null, e, undefined);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"class-provider.js","sourceRoot":"","sources":["../../src/class-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,gBAAgB,EAAC,MAAM,wBAAwB,CAAC;AACxD,OAAO,EAAC,gCAAgC,EAAE,+BAA+B,EAAE,cAAc,EAAC,MAAM,gBAAgB,CAAC;AACjH,OAAO,EAAC,gBAAgB,EAAE,qBAAqB,EAAC,MAAM,iBAAiB,CAAC;AAExE,OAAO,EAAC,KAAK,EAAC,MAAM,YAAY,CAAC;AACjC,OAAO,EAAC,SAAS,EAAC,MAAM,YAAY,CAAC;AAYrC;;;GAGG;AACH,MAAM,OAAO,kBAAsB,SAAQ,gBAAwC;IAClF,YAAY,QAAgC,EAAE,EAAmB,EAAE,KAA0B;QAC5F,KAAK,CAAC,QAAQ,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC;IA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C,QAAQ;iBACtC;qBACI;oBACJ,OAAO,KAAK,CAAC,SAAS,CAAC,CAAC,mBAAmB;iBAC3C;YACF,CAAC,CAAC,CAAC,CAAC;SACJ;aACI;YACJ,gFAAgF;YAChF,IAAI;gBACH,MAAM,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,SAAoB,CAAC,CAAC,CAAC;gBACxF,OAAO,IAAI,CAAC,sBAAsB,CAAC,MAAM,CAAC,CAAC;aAC3C;YACD,OAAO,GAAG,EAAE;gBACX,0EAA0E;gBAC1E,IAAI;oBACH,OAAO,KAAK,CAAC,SAAS,CAAI,IAAI,EAAE,SAAS,EAAE,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC,CAAC,CAAC;iBACxE;gBACD,OAAO,CAAC,EAAE;oBACT,0CAA0C;oBAC1C,OAAO,KAAK,CAAC,SAAS,CAAI,IAAI,EAAE,CAAC,EAAE,SAAS,CAAC,CAAC;iBAC9C;aACD;SACD;IACF,CAAC;CACD","sourcesContent":["import {BindableProvider} from './bindable-provider.js';\nimport {POSTCONSTRUCT_ASYNC_METADATA_KEY, POSTCONSTRUCT_SYNC_METADATA_KEY, REFLECT_PARAMS} from './constants.js';\nimport {_getInjectedIdAt, _getOptionalDefaultAt} from './decorators.js';\nimport {ClassConstructor, InjectableId, Injector} from './injector.js';\nimport {State} from './state.js';\nimport {isPromise} from './utils.js';\n\n/*\n * This is a bit of a hack, but it avoids a ton of alternative hacks.\n * Note that in the Container, resolveState is a protected method.\n * Injector was never meant to publicly expose State.\n * Gotta love JS!\n */\ninterface StateResolvingInjector extends Injector {\n\tresolveState<T>(id: InjectableId<T>): State<T>;\n}\n\n/**\n * @inheritDoc\n * This specialization invokes it's configured class constructor synchronously and then scans for (and invokes) any @PostConstruct (which may be synchronous or asynchronous).\n */\nexport class ClassBasedProvider<T> extends BindableProvider<T, ClassConstructor<T>> {\n\tconstructor(injector: StateResolvingInjector, id: InjectableId<T>, maker: ClassConstructor<T>) {\n\t\tsuper(injector, id, maker);\n\t}\n\n\t/**\n\t * @inheritDoc\n\t * @see the class description for this Provider.\n\t * This method is just a singleton guard, the real work is done by provideAsStateImpl.\n\t */\n\tprovideAsState(): State<T> {\n\t\tlet retVal = this.singleton;\n\t\tif (!retVal) {\n\t\t\tretVal = this.provideAsStateImpl();\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 if 'asyncOnly' is true and there is no asynchronous PostConstruct annotation (since class constructors can never by asynchronous).\n\t */\n\tresolveIfSingleton(asyncOnly: boolean): Promise<T> {\n\t\tif ((!asyncOnly) || Reflect.getMetadata(POSTCONSTRUCT_ASYNC_METADATA_KEY, this.maker))\n\t\t\treturn super.resolveIfSingleton(false);\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Make a resolved or pending State that reflects any @PostConstruct annotations.\n\t */\n\tprotected makePostConstructState(obj: T): State<T> {\n\t\tif (typeof obj === 'object' && (!Array.isArray(obj)) && obj.constructor) {\n\t\t\tlet maybeAsync = false;\n\t\t\tlet pcFn: () => void | Error | Promise<void | Error>;\n\t\t\tif (typeof this.successHandler === 'function') {\n\t\t\t\tmaybeAsync = true;\n\t\t\t\tpcFn = () => {\n\t\t\t\t\treturn this.successHandler(obj, this.injector, this.id, this.maker);\n\t\t\t\t};\n\t\t\t}\n\t\t\telse {\n\t\t\t\t/* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */\n\t\t\t\t// Check to see if there is a @PostConstruct annotation on a method of the class.\n\t\t\t\tlet postConstruct: string = Reflect.getMetadata(POSTCONSTRUCT_SYNC_METADATA_KEY, obj.constructor);\n\t\t\t\tif (!postConstruct) {\n\t\t\t\t\tmaybeAsync = true;\n\t\t\t\t\tpostConstruct = Reflect.getMetadata(POSTCONSTRUCT_ASYNC_METADATA_KEY, obj.constructor);\n\t\t\t\t}\n\t\t\t\tif (postConstruct && obj.constructor.prototype[postConstruct] && typeof obj.constructor.prototype[postConstruct] === 'function')\n\t\t\t\t\tpcFn = obj[postConstruct].bind?.(obj);\n\n\t\t\t\t/* eslint-enable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */\n\t\t\t}\n\t\t\tif (pcFn) {\n\t\t\t\tlet result: any;\n\t\t\t\ttry {\n\t\t\t\t\tresult = pcFn();\n\t\t\t\t}\n\t\t\t\tcatch (err) {\n\t\t\t\t\t// The post construction method threw while executing, give the errorHandler (if any) a crack at recovery.\n\t\t\t\t\ttry {\n\t\t\t\t\t\tobj = this.queryErrorHandler(err, obj); // The returned obj is unlikely to be the original obj.\n\t\t\t\t\t\treturn State.MakeState<T>(null, undefined, obj);\n\t\t\t\t\t}\n\t\t\t\t\tcatch (e) {\n\t\t\t\t\t\t// could not recover, propagate the error.\n\t\t\t\t\t\treturn State.MakeState<T>(null, e, undefined);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t// The post construction method says it will let us know when it's finished.\n\t\t\t\tif (result && (result instanceof Promise || (maybeAsync && isPromise<void>(result)))) {\n\t\t\t\t\t// Return a State that is pending (the other return statements in this method return a State which is resolved or rejected).\n\t\t\t\t\t/* eslint-disable @typescript-eslint/no-unsafe-argument */\n\t\t\t\t\treturn State.MakeState<T>(this.makePromiseForObj<T>(result, () => obj));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t// No PostConstruct, just return a resolved State\n\t\treturn State.MakeState<T>(null, undefined, obj);\n\t}\n\n\t/**\n\t * This method collects the States of all the constructor parameters for our target class.\n\t */\n\tprotected getConstructorParameterStates(): State[] {\n\t\tconst argTypes = Reflect.getMetadata(REFLECT_PARAMS, this.maker);\n\t\tif (argTypes === undefined || !Array.isArray(argTypes)) {\n\t\t\treturn [];\n\t\t}\n\t\treturn argTypes.map((argType, index) => {\n\t\t\t// The reflect-metadata API fails on circular dependencies returning undefined instead.\n\t\t\t// Additionally, it cannot return generic types (no runtime type info).\n\t\t\t// If an Inject annotation precedes the parameter, then that is what should get injected.\n\t\t\tconst overrideToken = _getInjectedIdAt(this.maker, index);\n\t\t\t// If there was no Inject annotation, we might still be able to determine what to inject using the 'argType' (aka Reflect design:paramtypes).\n\t\t\tconst actualToken = overrideToken === undefined ? argType : overrideToken;\n\t\t\tif (actualToken === undefined) {\n\t\t\t\t// No Inject annotation, and the type is not known.\n\t\t\t\tthrow new Error(`Injection error. Unable to determine parameter ${index} type/value of ${this.maker.toString()} constructor`);\n\t\t\t}\n\t\t\t// Ask our container to resolve the parameter.\n\t\t\t/* eslint-disable @typescript-eslint/no-unsafe-argument */\n\t\t\tlet param = (this.injector as StateResolvingInjector).resolveState(actualToken);\n\t\t\t// If the parameter could not be resolved, see if there is an @Optional annotation\n\t\t\tif ((!param.pending) && param.rejected) {\n\t\t\t\tconst md = _getOptionalDefaultAt(this.maker, index);\n\t\t\t\tif (md)\n\t\t\t\t\tparam = State.MakeState<any>(null, undefined, md.value);\n\t\t\t}\n\t\t\treturn param;\n\t\t});\n\t}\n\n\t/**\n\t * Gather the needed constructor parameters, invoke the constructor, and figure out what post construction needs done.\n\t */\n\tprivate provideAsStateImpl(): State<T> {\n\t\tconst params = this.getConstructorParameterStates();\n\n\t\t// If any of the params are in a rejected state, we cannot construct.\n\t\tconst paramRejection = params.find((p) => {\n\t\t\treturn (!p.pending) && p.rejected;\n\t\t});\n\t\tif (paramRejection) {\n\t\t\treturn paramRejection as State<T>;\n\t\t}\n\t\t// If any of the params are in a pending state, we will have to wait for them to be resolved before we can construct.\n\t\tconst pendingParams = params.filter((p) => {\n\t\t\treturn p.pending;\n\t\t}).map((p) => {\n\t\t\treturn p.promise;\n\t\t});\n\t\tif (pendingParams.length > 0) {\n\t\t\t// Some of the parameters needed for construction are not yet available, wait for them and then attempt construction.\n\t\t\tconst objPromise = this.makePromiseForObj<any[]>(Promise.all(pendingParams), () => {\n\t\t\t\t// All the parameters are now available, instantiate the class.\n\t\t\t\t// If this throws, it will be handled by our caller.\n\t\t\t\treturn Reflect.construct(this.maker, params.map((p) => p.fulfilled as unknown));\n\t\t\t});\n\t\t\t// Once the obj is resolved, then we need to check for PostConstruct and if it was async, wait for that too.\n\t\t\treturn State.MakeState<T>(objPromise.then((obj) => {\n\t\t\t\tconst state = this.makePostConstructState(obj);\n\t\t\t\tif (state.pending) {\n\t\t\t\t\treturn state.promise; // chain (aka wait some more).\n\t\t\t\t}\n\t\t\t\telse if (state.rejected) {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-return\n\t\t\t\t\treturn state.rejected as any; // error\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\treturn state.fulfilled; // value (aka obj).\n\t\t\t\t}\n\t\t\t}));\n\t\t}\n\t\telse {\n\t\t\t// All parameters needed for construction are available, instantiate the object.\n\t\t\ttry {\n\t\t\t\tconst newObj = Reflect.construct(this.maker, params.map((p) => p.fulfilled as unknown));\n\t\t\t\treturn this.makePostConstructState(newObj);\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\treturn 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\treturn State.MakeState<T>(null, e, undefined);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n"]}
|
package/lib/esm/decorators.js
CHANGED
|
@@ -72,9 +72,8 @@ export function Inject(id) {
|
|
|
72
72
|
* @returns Undefined (nothing), as this decorator does not modify the parameter in any way.
|
|
73
73
|
*/
|
|
74
74
|
return function (target, parameterName, parameterIndex) {
|
|
75
|
-
const hint = targetHint(target);
|
|
76
75
|
if (id === undefined) {
|
|
77
|
-
throw new Error('Undefined id passed to @Inject [' +
|
|
76
|
+
throw new Error('Undefined id passed to @Inject [' + targetHint(target) + ']');
|
|
78
77
|
}
|
|
79
78
|
const paramKey = validateSingleConstructorParam('Inject', target, parameterIndex);
|
|
80
79
|
Reflect.defineMetadata(INJECT_METADATA_KEY, id, target, paramKey);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decorators.js","sourceRoot":"","sources":["../../src/decorators.ts"],"names":[],"mappings":"AAAA,iDAAiD;AACjD;;GAEG;AACH,OAAO,EAAC,mBAAmB,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,gCAAgC,EAAE,+BAA+B,EAAE,cAAc,EAAE,oBAAoB,EAAC,MAAM,gBAAgB,CAAC;AAG5M,0CAA0C;AAC1C,SAAS,UAAU,CAAC,MAAgB;IACnC,IAAI,IAAwB,CAAC;IAC7B,IAAI,MAAM,EAAE;QACX,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;QACnB,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,MAAM,CAAC,WAAW,EAAE;YAClC,IAAI,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC;SAC/B;KACD;IACD,OAAO,IAAI,CAAC;AACb,CAAC;AAED,0DAA0D;AAC1D,SAAS,kBAAkB,CAAC,MAAW;IACtC,yGAAyG;IACzG,IAAI,OAAO,MAAM,KAAK,UAAU,IAAI,MAAM,CAAC,cAAc,CAAC,WAAW,CAAC,EAAE;QACvE,sEAAsE;QACtE,OAAO,MAAM,CAAC,SAAS,CAAC,WAAW,KAAK,MAAM,CAAC;KAC/C;IACD,OAAO,KAAK,CAAC;AACd,CAAC;AAED,4DAA4D;AAC5D,SAAS,eAAe,CAAC,GAAW;IACnC,OAAO,SAAS,GAAG,EAAE,CAAC;AACvB,CAAC;AAED,2EAA2E;AAC3E,SAAS,wBAAwB,CAAC,SAAiB,EAAE,MAAgB,EAAE,GAAW;IACjF,IAAI,CAAC,kBAAkB,CAAC,MAAM,CAAC,EAAE;QAChC,MAAM,IAAI,KAAK,CA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AW,CAAC,CAAC;IAC9E,CAAC,CAAC;AACH,CAAC","sourcesContent":["/* eslint-disable @typescript-eslint/ban-types */\n/**\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_RETURN, RELEASE_METADATA_KEY} from './constants.js';\nimport {InjectableId} from './injector.js';\n\n// Help user locate misapplied decorators.\nfunction targetHint(target: Function) {\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\t// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access\n\tif (typeof target === 'function' && target.hasOwnProperty('prototype')) {\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\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: Function, parameterName: string | symbol, parameterIndex: number): void {\n\t\tconst hint = targetHint(target);\n\t\tif (id === undefined) {\n\t\t\tthrow new Error('Undefined id passed to @Inject [' + hint + ']');\n\t\t}\n\t\tconst paramKey = validateSingleConstructorParam('Inject', target, 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 { // eslint-disable-line @typescript-eslint/explicit-module-boundary-types\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: Function, parameterName: string | symbol, parameterIndex: number): void {\n\t\tconst paramKey = validateSingleConstructorParam('Optional', target, 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) { // eslint-disable-line @typescript-eslint/no-unused-vars\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) { // eslint-disable-line @typescript-eslint/no-unused-vars\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,iDAAiD;AACjD;;GAEG;AACH,OAAO,EAAC,mBAAmB,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,gCAAgC,EAAE,+BAA+B,EAAE,cAAc,EAAE,oBAAoB,EAAC,MAAM,gBAAgB,CAAC;AAG5M,0CAA0C;AAC1C,SAAS,UAAU,CAAC,MAAgB;IACnC,IAAI,IAAwB,CAAC;IAC7B,IAAI,MAAM,EAAE;QACX,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;QACnB,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,MAAM,CAAC,WAAW,EAAE;YAClC,IAAI,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC;SAC/B;KACD;IACD,OAAO,IAAI,CAAC;AACb,CAAC;AAED,0DAA0D;AAC1D,SAAS,kBAAkB,CAAC,MAAW;IACtC,yGAAyG;IACzG,IAAI,OAAO,MAAM,KAAK,UAAU,IAAI,MAAM,CAAC,cAAc,CAAC,WAAW,CAAC,EAAE;QACvE,sEAAsE;QACtE,OAAO,MAAM,CAAC,SAAS,CAAC,WAAW,KAAK,MAAM,CAAC;KAC/C;IACD,OAAO,KAAK,CA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AoB,EAAE,MAAM,CAAC,WAAW,CAAC,EAAE;YACrE,MAAM,IAAI,KAAK,CAAC,mCAAmC,GAAG,UAAU,CAAC,MAAM,CAAC,WAAW,CAAC,GAAG,GAAG,CAAC,CAAC;SAC5F;QACD,OAAO,CAAC,cAAc,CAAC,oBAAoB,EAAE,UAAU,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC;IAC9E,CAAC,CAAC;AACH,CAAC","sourcesContent":["/* eslint-disable @typescript-eslint/ban-types */\n/**\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_RETURN, RELEASE_METADATA_KEY} from './constants.js';\nimport {InjectableId} from './injector.js';\n\n// Help user locate misapplied decorators.\nfunction targetHint(target: Function) {\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\t// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access\n\tif (typeof target === 'function' && target.hasOwnProperty('prototype')) {\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\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: Function, parameterName: string | symbol, parameterIndex: number): void {\n\t\tif (id === undefined) {\n\t\t\tthrow new Error('Undefined id passed to @Inject [' + targetHint(target) + ']');\n\t\t}\n\t\tconst paramKey = validateSingleConstructorParam('Inject', target, 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 { // eslint-disable-line @typescript-eslint/explicit-module-boundary-types\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: Function, parameterName: string | symbol, parameterIndex: number): void {\n\t\tconst paramKey = validateSingleConstructorParam('Optional', target, 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) { // eslint-disable-line @typescript-eslint/no-unused-vars\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) { // eslint-disable-line @typescript-eslint/no-unused-vars\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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "async-injection",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.4",
|
|
4
4
|
"description": "A robust lightweight dependency injection library for TypeScript.",
|
|
5
5
|
"author": "Frank Stock",
|
|
6
6
|
"license": "MIT",
|
|
@@ -51,22 +51,22 @@
|
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@istanbuljs/nyc-config-typescript": "~1.0.2",
|
|
54
|
-
"@types/jasmine": "~
|
|
55
|
-
"@typescript-eslint/eslint-plugin": "~5.
|
|
56
|
-
"@typescript-eslint/parser": "~5.
|
|
57
|
-
"eslint": "~8.
|
|
58
|
-
"eslint-plugin-import": "~2.
|
|
59
|
-
"eslint-plugin-jsdoc": "~39.
|
|
54
|
+
"@types/jasmine": "~5.1.2",
|
|
55
|
+
"@typescript-eslint/eslint-plugin": "~5.62.0",
|
|
56
|
+
"@typescript-eslint/parser": "~5.62.0",
|
|
57
|
+
"eslint": "~8.53.0",
|
|
58
|
+
"eslint-plugin-import": "~2.29.0",
|
|
59
|
+
"eslint-plugin-jsdoc": "~39.9.1",
|
|
60
60
|
"eslint-plugin-prefer-arrow": "~1.2.3",
|
|
61
|
-
"jasmine": "~
|
|
61
|
+
"jasmine": "~5.1.0",
|
|
62
62
|
"jasmine-console-reporter": "~3.1.0",
|
|
63
63
|
"nyc": "~15.1.0",
|
|
64
64
|
"reflect-metadata": "~0.1.13",
|
|
65
|
-
"rimraf": "~
|
|
65
|
+
"rimraf": "~5.0.5",
|
|
66
66
|
"source-map-support": "~0.5.21",
|
|
67
67
|
"ts-node": "~10.9.1",
|
|
68
68
|
"tsconfig-paths": "~4.2.0",
|
|
69
|
-
"tslib": "~2.
|
|
69
|
+
"tslib": "~2.6.2",
|
|
70
70
|
"typescript": "~4.9.5"
|
|
71
71
|
},
|
|
72
72
|
"nyc": {
|