async-injection 2.2.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ReadMe.md +1 -0
- package/lib/async-factory-provider.d.ts +4 -4
- package/lib/bindable-provider.d.ts +3 -3
- package/lib/binder.d.ts +6 -2
- package/lib/cjs/async-factory-provider.js +5 -5
- package/lib/cjs/async-factory-provider.js.map +1 -1
- package/lib/cjs/bindable-provider.js +33 -33
- package/lib/cjs/bindable-provider.js.map +1 -1
- package/lib/cjs/binder.js.map +1 -1
- package/lib/cjs/class-provider.js +36 -37
- package/lib/cjs/class-provider.js.map +1 -1
- package/lib/cjs/constant-provider.js +4 -4
- package/lib/cjs/constant-provider.js.map +1 -1
- package/lib/cjs/container.js +19 -23
- package/lib/cjs/container.js.map +1 -1
- package/lib/cjs/decorators.js +21 -25
- package/lib/cjs/decorators.js.map +1 -1
- package/lib/cjs/index.js +10 -10
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/injector.js +5 -5
- package/lib/cjs/injector.js.map +1 -1
- package/lib/cjs/provider.js +16 -11
- package/lib/cjs/provider.js.map +1 -1
- package/lib/cjs/state.js +2 -2
- package/lib/cjs/state.js.map +1 -1
- package/lib/cjs/sync-factory-provider.js +7 -7
- package/lib/cjs/sync-factory-provider.js.map +1 -1
- package/lib/cjs/utils.js +10 -13
- package/lib/cjs/utils.js.map +1 -1
- package/lib/class-provider.d.ts +4 -4
- package/lib/constant-provider.d.ts +2 -2
- package/lib/container.d.ts +6 -6
- package/lib/decorators.d.ts +1 -1
- package/lib/esm/async-factory-provider.js +1 -1
- package/lib/esm/async-factory-provider.js.map +1 -1
- package/lib/esm/bindable-provider.js +29 -29
- package/lib/esm/bindable-provider.js.map +1 -1
- package/lib/esm/binder.js.map +1 -1
- package/lib/esm/class-provider.js +17 -18
- package/lib/esm/class-provider.js.map +1 -1
- package/lib/esm/constant-provider.js.map +1 -1
- package/lib/esm/container.js +1 -5
- package/lib/esm/container.js.map +1 -1
- package/lib/esm/decorators.js +1 -4
- package/lib/esm/decorators.js.map +1 -1
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/injector.js +5 -5
- package/lib/esm/injector.js.map +1 -1
- package/lib/esm/provider.js +14 -9
- package/lib/esm/provider.js.map +1 -1
- package/lib/esm/state.js.map +1 -1
- package/lib/esm/sync-factory-provider.js +1 -1
- package/lib/esm/sync-factory-provider.js.map +1 -1
- package/lib/esm/utils.js +6 -8
- package/lib/esm/utils.js.map +1 -1
- package/lib/index.d.ts +3 -3
- package/lib/provider.d.ts +4 -4
- package/lib/state.d.ts +3 -3
- package/lib/sync-factory-provider.d.ts +5 -5
- package/package.json +7 -18
package/ReadMe.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# Async-Injection
|
|
2
2
|
[](https://github.com/pcafstockf/async-injection/actions)
|
|
3
3
|
[](https://github.com/pcafstockf/async-injection/actions)
|
|
4
|
+
[](https://codecov.io/gh/pcafstockf/async-injection)
|
|
4
5
|
[](https://opensource.org/licenses/MIT)
|
|
5
6
|

|
|
6
7
|
[](https://www.npmjs.com/package/async-injection)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { BindableProvider } from './bindable-provider
|
|
2
|
-
import { AsyncFactory } from './binder
|
|
3
|
-
import { InjectableId, Injector } from './injector
|
|
4
|
-
import { State } from './state
|
|
1
|
+
import { BindableProvider } from './bindable-provider';
|
|
2
|
+
import { AsyncFactory } from './binder';
|
|
3
|
+
import { InjectableId, Injector } from './injector';
|
|
4
|
+
import { State } from './state';
|
|
5
5
|
/**
|
|
6
6
|
* @inheritDoc
|
|
7
7
|
* This specialization invokes it's configured Factory asynchronously and waits until it can provide the result.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { AsyncFactory, BindAs, OnErrorCallback, OnSuccessCallback, SyncFactory } from './binder
|
|
2
|
-
import { ClassConstructor, InjectableId, Injector } from './injector
|
|
3
|
-
import { Provider } from './provider
|
|
1
|
+
import { AsyncFactory, BindAs, OnErrorCallback, OnSuccessCallback, SyncFactory } from './binder';
|
|
2
|
+
import { ClassConstructor, InjectableId, Injector } from './injector';
|
|
3
|
+
import { Provider } from './provider';
|
|
4
4
|
/**
|
|
5
5
|
* @inheritDoc
|
|
6
6
|
* This abstraction is for Providers that can be additionally configured as Singletons and/or configured with error and/or success handling callback(s).
|
package/lib/binder.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AbstractConstructor, ClassConstructor, InjectableId, Injector } from './injector
|
|
1
|
+
import { AbstractConstructor, ClassConstructor, InjectableId, Injector } from './injector';
|
|
2
2
|
/**
|
|
3
3
|
* Type definition for functions that return a value.
|
|
4
4
|
* The function should return a valid value, but may throw an exception if it cannot.
|
|
@@ -10,7 +10,7 @@ export type SyncFactory<T> = (injector: Injector) => T;
|
|
|
10
10
|
*/
|
|
11
11
|
export type AsyncFactory<T> = (injector: Injector) => Promise<T>;
|
|
12
12
|
/**
|
|
13
|
-
* You may bind an error handler which will be invoked
|
|
13
|
+
* You may bind an error handler which will be invoked if the bound InjectableId could not be put into service.
|
|
14
14
|
* An error handler *must* not throw, but may return an Error that will be propagated back up the call chain.
|
|
15
15
|
*
|
|
16
16
|
* @param binder The Binder that experienced the error.
|
|
@@ -67,6 +67,10 @@ export interface BindAs<T, M> extends BindHandler<T, M> {
|
|
|
67
67
|
}
|
|
68
68
|
/**
|
|
69
69
|
* Bind Ids to producers.
|
|
70
|
+
*
|
|
71
|
+
* @deprecated {@link Binder} was removed from index.ts long ago will be removed in a future release.
|
|
72
|
+
* Use {@link Container} to build a context root — it provides all binding methods directly.
|
|
73
|
+
* For type annotations, use {@link Injector}, which {@link Container} implements.
|
|
70
74
|
*/
|
|
71
75
|
export interface Binder extends Injector {
|
|
72
76
|
/**
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AsyncFactoryBasedProvider = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
4
|
+
const bindable_provider_1 = require("./bindable-provider");
|
|
5
|
+
const state_1 = require("./state");
|
|
6
6
|
/**
|
|
7
7
|
* @inheritDoc
|
|
8
8
|
* This specialization invokes it's configured Factory asynchronously and waits until it can provide the result.
|
|
9
9
|
*/
|
|
10
|
-
class AsyncFactoryBasedProvider extends
|
|
10
|
+
class AsyncFactoryBasedProvider extends bindable_provider_1.BindableProvider {
|
|
11
11
|
constructor(injector, id, maker) {
|
|
12
12
|
super(injector, id, maker);
|
|
13
13
|
}
|
|
@@ -19,8 +19,8 @@ class AsyncFactoryBasedProvider extends bindable_provider_js_1.BindableProvider
|
|
|
19
19
|
let retVal = this.singleton;
|
|
20
20
|
if (!retVal) {
|
|
21
21
|
// Wrap the async factory's Promise in an errorHandler aware Promise.
|
|
22
|
-
// Our contract is that an AsyncFactory may not throw and must return a valid Promise (e.g
|
|
23
|
-
retVal =
|
|
22
|
+
// Our contract is that an AsyncFactory may not throw and must return a valid Promise (e.g., pending, resolved, rejected, etc).
|
|
23
|
+
retVal = state_1.State.MakeState(this.makePromiseForObj(this.maker(this.injector), obj => obj));
|
|
24
24
|
}
|
|
25
25
|
if (this.singleton === null)
|
|
26
26
|
this.singleton = retVal;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async-factory-provider.js","sourceRoot":"","sources":["../../src/async-factory-provider.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"async-factory-provider.js","sourceRoot":"","sources":["../../src/async-factory-provider.ts"],"names":[],"mappings":";;;AAAA,2DAAqD;AAGrD,mCAA8B;AAE9B;;;GAGG;AACH,MAAa,yBAA6B,SAAQ,oCAAoC;IACrF,YAAY,QAAkB,EAAE,EAAmB,EAAE,KAAsB;QAC1E,KAAK,CAAC,QAAQ,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC;IAC5B,CAAC;IAED;;;OAGG;IACH,cAAc;QACb,IAAI,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC;QAC5B,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,qEAAqE;YACrE,+HAA+H;YAC/H,MAAM,GAAG,aAAK,CAAC,SAAS,CAAI,IAAI,CAAC,iBAAiB,CAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;QAC/F,CAAC;QACD,IAAI,IAAI,CAAC,SAAS,KAAK,IAAI;YAC1B,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC;QACzB,OAAO,MAAM,CAAC;IACf,CAAC;CACD;AApBD,8DAoBC","sourcesContent":["import {BindableProvider} from './bindable-provider';\nimport {AsyncFactory} from './binder';\nimport {InjectableId, Injector} from './injector';\nimport {State} from './state';\n\n/**\n * @inheritDoc\n * This specialization invokes it's configured Factory asynchronously and waits until it can provide the result.\n */\nexport class AsyncFactoryBasedProvider<T> extends BindableProvider<T, AsyncFactory<T>> {\n\tconstructor(injector: Injector, id: InjectableId<T>, maker: AsyncFactory<T>) {\n\t\tsuper(injector, id, maker);\n\t}\n\n\t/**\n\t * @inheritDoc\n\t * This specialization invokes it's configured Factory and provides the result (or invokes the error handler if necessary).\n\t */\n\tprovideAsState(): State<T> {\n\t\tlet retVal = this.singleton;\n\t\tif (!retVal) {\n\t\t\t// Wrap the async factory's Promise in an errorHandler aware Promise.\n\t\t\t// Our contract is that an AsyncFactory may not throw and must return a valid Promise (e.g., pending, resolved, rejected, etc).\n\t\t\tretVal = State.MakeState<T>(this.makePromiseForObj<T>(this.maker(this.injector), obj => obj));\n\t\t}\n\t\tif (this.singleton === null)\n\t\t\tthis.singleton = retVal;\n\t\treturn retVal;\n\t}\n}\n"]}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BindableProvider = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
4
|
+
const provider_1 = require("./provider");
|
|
5
|
+
const utils_1 = require("./utils");
|
|
6
6
|
/**
|
|
7
7
|
* @inheritDoc
|
|
8
8
|
* This abstraction is for Providers that can be additionally configured as Singletons and/or configured with error and/or success handling callback(s).
|
|
9
9
|
*/
|
|
10
|
-
class BindableProvider extends
|
|
10
|
+
class BindableProvider extends provider_1.Provider {
|
|
11
11
|
constructor(injector, id, maker) {
|
|
12
12
|
super();
|
|
13
13
|
this.injector = injector;
|
|
@@ -47,7 +47,7 @@ class BindableProvider extends provider_js_1.Provider {
|
|
|
47
47
|
if (this.errorHandler) {
|
|
48
48
|
const handlerResult = this.errorHandler(this.injector, this.id, this.maker, err, obj);
|
|
49
49
|
// Error handler wants us to propagate an error.
|
|
50
|
-
if ((0,
|
|
50
|
+
if ((0, utils_1.isErrorObj)(handlerResult))
|
|
51
51
|
throw handlerResult;
|
|
52
52
|
// Error handler has no opinion, so provideAsState a state that reflects the error we just caught.
|
|
53
53
|
if (typeof handlerResult === 'undefined')
|
|
@@ -66,35 +66,35 @@ class BindableProvider extends provider_js_1.Provider {
|
|
|
66
66
|
* @param waitFor The supplied Promise.
|
|
67
67
|
* @param cb Callback to be invoked if the supplied Promise resolves.
|
|
68
68
|
*/
|
|
69
|
-
makePromiseForObj(waitFor, cb) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
69
|
+
async makePromiseForObj(waitFor, cb) {
|
|
70
|
+
// Local helper: consults the errorHandler (if any) for recovery; returns a substitute or re-throws.
|
|
71
|
+
const handleError = (err, objValue) => {
|
|
72
|
+
if (this.errorHandler) {
|
|
73
|
+
const handlerResult = this.errorHandler(this.injector, this.id, this.maker, err, objValue);
|
|
74
|
+
// Error handler wants us to propagate an alternative error.
|
|
75
|
+
if ((0, utils_1.isErrorObj)(handlerResult))
|
|
76
|
+
throw handlerResult;
|
|
77
|
+
// Error handler provided a valid (fully resolved) replacement.
|
|
78
|
+
else if (typeof handlerResult !== 'undefined')
|
|
79
|
+
return handlerResult;
|
|
80
|
+
}
|
|
81
|
+
throw err;
|
|
82
|
+
};
|
|
83
|
+
let result;
|
|
84
|
+
try {
|
|
85
|
+
result = await waitFor;
|
|
86
|
+
}
|
|
87
|
+
catch (err) {
|
|
88
|
+
// waitFor rejected — ask the error handler for recovery, passing cb(undefined) as the partial object value.
|
|
89
|
+
return handleError(err, cb(undefined));
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
return cb(result);
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
// cb threw after a successful resolution — ask the error handler for recovery.
|
|
96
|
+
return handleError(err, cb(result));
|
|
97
|
+
}
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
exports.BindableProvider = BindableProvider;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bindable-provider.js","sourceRoot":"","sources":["../../src/bindable-provider.ts"],"names":[],"mappings":";;;AAEA
|
|
1
|
+
{"version":3,"file":"bindable-provider.js","sourceRoot":"","sources":["../../src/bindable-provider.ts"],"names":[],"mappings":";;;AAEA,yCAAoC;AACpC,mCAAmC;AAEnC;;;GAGG;AACH,MAAsB,gBAAgF,SAAQ,mBAAW;IACxH,YAAgC,QAAkB,EAAY,EAAmB,EAAY,KAAQ;QACpG,KAAK,EAAE,CAAC;QADuB,aAAQ,GAAR,QAAQ,CAAU;QAAY,OAAE,GAAF,EAAE,CAAiB;QAAY,UAAK,GAAL,KAAK,CAAG;IAEr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,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,KAAK,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;gBAC3F,4DAA4D;gBAC5D,IAAI,IAAA,kBAAU,EAAC,aAAa,CAAC;oBAC5B,MAAM,aAAa,CAAC;gBACrB,+DAA+D;qBAC1D,IAAI,OAAO,aAAa,KAAK,WAAW;oBAC5C,OAAO,aAAa,CAAC;YACvB,CAAC;YACD,MAAM,GAAG,CAAC;QACX,CAAC,CAAC;QACF,IAAI,MAAS,CAAC;QACd,IAAI,CAAC;YACJ,MAAM,GAAG,MAAM,OAAO,CAAC;QACxB,CAAC;QACD,OAAO,GAAG,EAAE,CAAC;YACZ,4GAA4G;YAC5G,OAAO,WAAW,CAAC,GAAG,EAAE,EAAE,CAAC,SAAyB,CAAC,CAAC,CAAC;QACxD,CAAC;QACD,IAAI,CAAC;YACJ,OAAO,EAAE,CAAC,MAAM,CAAC,CAAC;QACnB,CAAC;QACD,OAAO,GAAG,EAAE,CAAC;YACZ,+EAA+E;YAC/E,OAAO,WAAW,CAAC,GAAG,EAAE,EAAE,CAAC,MAAW,CAAC,CAAC,CAAC;QAC1C,CAAC;IACF,CAAC;CACD;AArGD,4CAqGC","sourcesContent":["import {AsyncFactory, BindAs, OnErrorCallback, OnSuccessCallback, SyncFactory} from './binder';\nimport {ClassConstructor, InjectableId, Injector} from './injector';\nimport {Provider} from './provider';\nimport {isErrorObj} from './utils';\n\n/**\n * @inheritDoc\n * This abstraction is for Providers that can be additionally configured as Singletons and/or configured with error and/or success handling callback(s).\n */\nexport abstract class BindableProvider<T, M = ClassConstructor<T> | SyncFactory<T> | AsyncFactory<T>> extends Provider<T> {\n\tprotected constructor(protected injector: Injector, protected id: InjectableId<T>, protected maker: M) {\n\t\tsuper();\n\t}\n\n\t/**\n\t * A user supplied success handling function.\n\t * Default value is undefined.\n\t */\n\tprotected successHandler?: OnSuccessCallback<T, any>;\n\n\t/**\n\t * A user supplied error handling function.\n\t * Default value is undefined.\n\t */\n\tprotected errorHandler?: OnErrorCallback<T, any>;\n\n\t/**\n\t * Invoked by the Binder to create chain-able configuration\n\t *\n\t * @see BindAs\n\t */\n\tmakeBindAs(): BindAs<T, M> {\n\t\tconst retVal: BindAs<T, M> = {\n\t\t\tonError: (cb: OnErrorCallback<T, M>) => {\n\t\t\t\tthis.errorHandler = cb;\n\t\t\t},\n\t\t\tonSuccess: (cb: OnSuccessCallback<T, M>) => {\n\t\t\t\tthis.successHandler = cb;\n\t\t\t\treturn retVal;\n\t\t\t},\n\t\t\tasSingleton: () => {\n\t\t\t\tthis.singleton = null; // Flag state as no longer undefined.\n\t\t\t\treturn retVal;\n\t\t\t}\n\t\t};\n\t\treturn retVal;\n\t}\n\n\t/**\n\t * Encapsulate the logic of invoking any configured error handler, and processing it's result.\n\t *\n\t * @see OnErrorCallback\n\t *\n\t * @returns The object substituted by the callback (otherwise this method throws the appropriate error).\n\t */\n\tprotected queryErrorHandler(err: unknown, obj?: T): T {\n\t\t// There was an error during construction, see if an error handler was provided, and if so, see what it wants to do.\n\t\tif (this.errorHandler) {\n\t\t\tconst handlerResult = this.errorHandler(this.injector, this.id, this.maker, err, obj);\n\t\t\t// Error handler wants us to propagate an error.\n\t\t\tif (isErrorObj(handlerResult))\n\t\t\t\tthrow handlerResult;\n\t\t\t// Error handler has no opinion, so provideAsState a state that reflects the error we just caught.\n\t\t\tif (typeof handlerResult === 'undefined')\n\t\t\t\tthrow err;\n\t\t\t// Error handler provided a valid (fully resolved) replacement.\n\t\t\treturn handlerResult;\n\t\t}\n\t\t// No error handler, provideAsState a state that reflects the error we just caught.\n\t\tthrow err;\n\t}\n\n\t/**\n\t * This is like a retry mechanism that uses the Provider's errorHandler (if any) to attempt recovery whenever the supplied Promise rejects.\n\t * This method returns a Promise that rejects if recovery was not possible.\n\t * If the supplied Promise resolves, then this method passes the result to the callback, and then resolve as whatever that callback returns.\n\t *\n\t * @param waitFor The supplied Promise.\n\t * @param cb Callback to be invoked if the supplied Promise resolves.\n\t */\n\tprotected async makePromiseForObj<R>(waitFor: Promise<R>, cb: (result: R) => T): Promise<T> {\n\t\t// Local helper: consults the errorHandler (if any) for recovery; returns a substitute or re-throws.\n\t\tconst handleError = (err: unknown, objValue?: T): T => {\n\t\t\tif (this.errorHandler) {\n\t\t\t\tconst handlerResult = this.errorHandler(this.injector, this.id, this.maker, err, objValue);\n\t\t\t\t// Error handler wants us to propagate an alternative error.\n\t\t\t\tif (isErrorObj(handlerResult))\n\t\t\t\t\tthrow handlerResult;\n\t\t\t\t// Error handler provided a valid (fully resolved) replacement.\n\t\t\t\telse if (typeof handlerResult !== 'undefined')\n\t\t\t\t\treturn handlerResult;\n\t\t\t}\n\t\t\tthrow err;\n\t\t};\n\t\tlet result: R;\n\t\ttry {\n\t\t\tresult = await waitFor;\n\t\t}\n\t\tcatch (err) {\n\t\t\t// waitFor rejected — ask the error handler for recovery, passing cb(undefined) as the partial object value.\n\t\t\treturn handleError(err, cb(undefined as unknown as R));\n\t\t}\n\t\ttry {\n\t\t\treturn cb(result);\n\t\t}\n\t\tcatch (err) {\n\t\t\t// cb threw after a successful resolution — ask the error handler for recovery.\n\t\t\treturn handleError(err, cb(result as R));\n\t\t}\n\t}\n}\n"]}
|
package/lib/cjs/binder.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"binder.js","sourceRoot":"","sources":["../../src/binder.ts"],"names":[],"mappings":"","sourcesContent":["import {AbstractConstructor, ClassConstructor, InjectableId, Injector} from './injector
|
|
1
|
+
{"version":3,"file":"binder.js","sourceRoot":"","sources":["../../src/binder.ts"],"names":[],"mappings":"","sourcesContent":["import {AbstractConstructor, ClassConstructor, InjectableId, Injector} from './injector';\n\n/**\n * Type definition for functions that return a value.\n * The function should return a valid value, but may throw an exception if it cannot.\n */\nexport type SyncFactory<T> = (injector: Injector) => T;\n\n/**\n * Type definition for functions that return a Promise for a value.\n * The function *must* not throw and must return a valid Promise (e.g. pending, resolved, rejected).\n */\nexport type AsyncFactory<T> = (injector: Injector) => Promise<T>;\n\n/**\n * You may bind an error handler which will be invoked if the bound InjectableId could not be put into service.\n * An error handler *must* not throw, but may return an Error that will be propagated back up the call chain.\n *\n * @param binder The Binder that experienced the error.\n * @param id The identifier for what was trying to be made.\n * @param maker The thing that made (or tried to provideAsState). Will be one of type ClassConstructor, SyncFactory, or AsyncFactory, depending on how you registered the binding.\n * @param error Identifies the problem that occurred.\n * @param value If the 'maker' was able to create the thing, but it had an error during post construction, the made thing will be passed here.\n * @returns one of 3 results...\n * A substitute thing (kind of like a 'maker' do-over) which must be fully operational (e.g. any `@PostConstruct` will be ignored).\n * An alternate Error which will be propagated back up the call chain.\n * Undefined, which means the 'error' parameter will be propagated back up the call chain.\n */\nexport type OnErrorCallback<T, M> = (injector: Injector, id: InjectableId<T>, maker: M, error: unknown, value?: T) => T | Error | void;\n\n/**\n * You may bind a success handler which will be invoked just before the bound InjectableId is put into service.\n * This is an alternative to the more preferred `@PostConstruct` decorator for scenarios when usage of that decorator is not feasible.\n * WARNING:\n * By registering a success handler, you override and nullify any `@PostConstruct` decorator on the class.\n * In such a scenario, the success handler should perform whatever care and feeding the class expected from the `@PostConstruct` decorator.\n * A success handler *must* not throw, but may return an Error that will be propagated back up the call chain.\n *\n * @param binder The Binder that performed the construction.\n * @param id The identifier for what was made.\n * @param maker The thing that made. Will be one of type ClassConstructor, SyncFactory, or AsyncFactory, depending on how you registered the binding.\n * @param value The thing that was made.\n * @returns one of 3 results...\n * An Error which will be propagated back up the call chain.\n * Undefined, which means the object is ready to be placed into service.\n * A Promise that resolves to one of the above two values (undefined or Error).\n */\nexport type OnSuccessCallback<T, M> = (value: T, injector: Injector, id: InjectableId<T>, maker: M) => Promise<Error | void> | Error | void;\n\n/**\n * An interface allowing binding of an error handler.\n *\n * @see OnErrorCallback\n */\nexport interface BindErrHandler<T, M> {\n\tonError(cb: OnErrorCallback<T, M>): void;\n}\n\n/**\n * An interface allowing binding of a post construction handler.\n *\n * @see OnSuccessCallback\n */\nexport interface BindHandler<T, M> extends BindErrHandler<T, M> {\n\tonSuccess(cb: OnSuccessCallback<T, M>): BindErrHandler<T, M>;\n}\n\n/**\n * @inheritDoc\n * This specialization also allows you to specify that the binding is 'Singleton' (e.g. only one in the system).\n */\nexport interface BindAs<T, M> extends BindHandler<T, M> {\n\tasSingleton(): BindHandler<T, M>;\n}\n\n/**\n * Bind Ids to producers.\n *\n * @deprecated {@link Binder} was removed from index.ts long ago will be removed in a future release.\n * Use {@link Container} to build a context root — it provides all binding methods directly.\n * For type annotations, use {@link Injector}, which {@link Container} implements.\n */\nexport interface Binder extends Injector {\n\n\t/**\n\t * Bind an InjectableId to a constant value.\n\t * Constants are by their very nature singleton, and are assumed to be error proof.\n\t */\n\tbindConstant<T>(id: InjectableId<T>, value: T): T;\n\n\t/**\n\t * Bind an InjectableId to a class (actually it's constructor).\n\t * As a shortcut, you may use the class constructor as the 'id' (e.g. container.bindClass(A); ).\n\t * The container will also invoke any `@PostConstruct` present on the class.\n\t */\n\tbindClass<T>(id: ClassConstructor<T>, constructor?: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;\n\n\tbindClass<T>(id: string | symbol | AbstractConstructor<T> | InjectableId<T>, constructor: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;\n\n\t/**\n\t * Bind an InjectableId to a synchronous factory that will be invoked on demand when the object is needed.\n\t * The factory should produce the needed value\n\t * NOTE: The container will not invoke any `@PostConstruct` present on the class, this is the responsibility of the factory.\n\t */\n\tbindFactory<T>(id: InjectableId<T>, factory: SyncFactory<T>): BindAs<T, SyncFactory<T>>;\n\n\t/**\n\t * Bind an InjectableId to an asynchronous factory that will be invoked on demand when the object is needed.\n\t * The factory should produce the needed value (asynchronously of course).\n\t * NOTE: The container will not invoke any `@PostConstruct` present on the class, this is the responsibility of the factory.\n\t * WARNING!!! The factory may not throw and must return a valid Promise (which can be pending, resolved, rejected, etc.).\n\t */\n\tbindAsyncFactory<T>(id: InjectableId<T>, factory: AsyncFactory<T>): BindAs<T, AsyncFactory<T>>;\n\n\t/**\n\t * This essentially pre creates/loads all *singleton* InjectableIds currently known to the Binder.\n\t * This *may* be helpful if you wish to use Injector.get on a dependency tree that has asynchronous singletons within the tree.\n\t *\n\t * @param asyncOnly Only resolve AsyncFactorys as well as any bound classes that have an asynchronous `@PostConstruct` decorator. WARNING: If true, SyncFactorys will *not* be resolved even if they are Singletons.\n\t * @param parentRecursion If true and the the container has a parent, resolveIfSingleton will first be called for the parent\n\t * @returns A Promise that resolves when all Singleton's have been resolved, OR rejects if one or more of the Singleton's failed to resolve. NOTE: Rejection does not occur until all Singleton resolutions have settled, and the rejection reason/err will be a Map<InjectableId, Error>\n\t */\n\tresolveSingletons(asyncOnly?: boolean, parentRecursion?: boolean): Promise<this>;\n}\n"]}
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ClassBasedProvider = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
4
|
+
const bindable_provider_1 = require("./bindable-provider");
|
|
5
|
+
const constants_1 = require("./constants");
|
|
6
|
+
const decorators_1 = require("./decorators");
|
|
7
|
+
const state_1 = require("./state");
|
|
8
|
+
const utils_1 = require("./utils");
|
|
9
9
|
/**
|
|
10
10
|
* @inheritDoc
|
|
11
11
|
* This specialization invokes it's configured class constructor synchronously and then scans for (and invokes) any @PostConstruct (which may be synchronous or asynchronous).
|
|
12
12
|
*/
|
|
13
|
-
class ClassBasedProvider extends
|
|
13
|
+
class ClassBasedProvider extends bindable_provider_1.BindableProvider {
|
|
14
14
|
constructor(injector, id, maker) {
|
|
15
15
|
super(injector, id, maker);
|
|
16
16
|
}
|
|
@@ -33,7 +33,7 @@ class ClassBasedProvider extends bindable_provider_js_1.BindableProvider {
|
|
|
33
33
|
* This specialization returns undefined if 'asyncOnly' is true and there is no asynchronous PostConstruct annotation (since class constructors can never by asynchronous).
|
|
34
34
|
*/
|
|
35
35
|
resolveIfSingleton(asyncOnly) {
|
|
36
|
-
if ((!asyncOnly) || Reflect.getMetadata(
|
|
36
|
+
if ((!asyncOnly) || Reflect.getMetadata(constants_1.POSTCONSTRUCT_ASYNC_METADATA_KEY, this.maker))
|
|
37
37
|
return super.resolveIfSingleton(false);
|
|
38
38
|
return undefined;
|
|
39
39
|
}
|
|
@@ -42,7 +42,7 @@ class ClassBasedProvider extends bindable_provider_js_1.BindableProvider {
|
|
|
42
42
|
*/
|
|
43
43
|
makePostConstructState(obj) {
|
|
44
44
|
var _a, _b;
|
|
45
|
-
if (typeof obj === 'object' && (!Array.isArray(obj)) && obj.constructor) {
|
|
45
|
+
if (obj !== null && typeof obj === 'object' && (!Array.isArray(obj)) && obj.constructor) {
|
|
46
46
|
let maybeAsync = false;
|
|
47
47
|
let pcFn;
|
|
48
48
|
if (typeof this.successHandler === 'function') {
|
|
@@ -52,16 +52,15 @@ class ClassBasedProvider extends bindable_provider_js_1.BindableProvider {
|
|
|
52
52
|
};
|
|
53
53
|
}
|
|
54
54
|
else {
|
|
55
|
-
/* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */
|
|
56
55
|
// Check to see if there is a @PostConstruct annotation on a method of the class.
|
|
57
|
-
|
|
56
|
+
const ctor = obj.constructor;
|
|
57
|
+
let postConstruct = Reflect.getMetadata(constants_1.POSTCONSTRUCT_SYNC_METADATA_KEY, ctor);
|
|
58
58
|
if (!postConstruct) {
|
|
59
59
|
maybeAsync = true;
|
|
60
|
-
postConstruct = Reflect.getMetadata(
|
|
60
|
+
postConstruct = Reflect.getMetadata(constants_1.POSTCONSTRUCT_ASYNC_METADATA_KEY, ctor);
|
|
61
61
|
}
|
|
62
|
-
if (postConstruct &&
|
|
62
|
+
if (postConstruct && ctor.prototype[postConstruct] && typeof ctor.prototype[postConstruct] === 'function')
|
|
63
63
|
pcFn = (_b = (_a = obj[postConstruct]).bind) === null || _b === void 0 ? void 0 : _b.call(_a, obj);
|
|
64
|
-
/* eslint-enable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */
|
|
65
64
|
}
|
|
66
65
|
if (pcFn) {
|
|
67
66
|
let result;
|
|
@@ -72,29 +71,28 @@ class ClassBasedProvider extends bindable_provider_js_1.BindableProvider {
|
|
|
72
71
|
// The post construction method threw while executing, give the errorHandler (if any) a crack at recovery.
|
|
73
72
|
try {
|
|
74
73
|
obj = this.queryErrorHandler(err, obj); // The returned obj is unlikely to be the original obj.
|
|
75
|
-
return
|
|
74
|
+
return state_1.State.MakeState(null, undefined, obj);
|
|
76
75
|
}
|
|
77
76
|
catch (e) {
|
|
78
77
|
// could not recover, propagate the error.
|
|
79
|
-
return
|
|
78
|
+
return state_1.State.MakeState(null, e, undefined);
|
|
80
79
|
}
|
|
81
80
|
}
|
|
82
81
|
// The post construction method says it will let us know when it's finished.
|
|
83
|
-
if (result && (result instanceof Promise || (maybeAsync && (0,
|
|
82
|
+
if (result && (result instanceof Promise || (maybeAsync && (0, utils_1.isPromise)(result)))) {
|
|
84
83
|
// Return a State that is pending (the other return statements in this method return a State which is resolved or rejected).
|
|
85
|
-
|
|
86
|
-
return state_js_1.State.MakeState(this.makePromiseForObj(result, () => obj));
|
|
84
|
+
return state_1.State.MakeState(this.makePromiseForObj(result, () => obj));
|
|
87
85
|
}
|
|
88
86
|
}
|
|
89
87
|
}
|
|
90
88
|
// No PostConstruct, just return a resolved State
|
|
91
|
-
return
|
|
89
|
+
return state_1.State.MakeState(null, undefined, obj);
|
|
92
90
|
}
|
|
93
91
|
/**
|
|
94
92
|
* This method collects the States of all the constructor parameters for our target class.
|
|
95
93
|
*/
|
|
96
94
|
getConstructorParameterStates() {
|
|
97
|
-
const argTypes = Reflect.getMetadata(
|
|
95
|
+
const argTypes = Reflect.getMetadata(constants_1.REFLECT_PARAMS, this.maker);
|
|
98
96
|
if (argTypes === undefined || !Array.isArray(argTypes)) {
|
|
99
97
|
return [];
|
|
100
98
|
}
|
|
@@ -102,7 +100,7 @@ class ClassBasedProvider extends bindable_provider_js_1.BindableProvider {
|
|
|
102
100
|
// The reflect-metadata API fails on circular dependencies returning undefined instead.
|
|
103
101
|
// Additionally, it cannot return generic types (no runtime type info).
|
|
104
102
|
// If an Inject annotation precedes the parameter, then that is what should get injected.
|
|
105
|
-
const overrideToken = (0,
|
|
103
|
+
const overrideToken = (0, decorators_1._getInjectedIdAt)(this.maker, index);
|
|
106
104
|
// If there was no Inject annotation, we might still be able to determine what to inject using the 'argType' (aka Reflect design:paramtypes).
|
|
107
105
|
const actualToken = overrideToken === undefined ? argType : overrideToken;
|
|
108
106
|
if (actualToken === undefined) {
|
|
@@ -110,13 +108,12 @@ class ClassBasedProvider extends bindable_provider_js_1.BindableProvider {
|
|
|
110
108
|
throw new Error(`Injection error. Unable to determine parameter ${index} type/value of ${this.maker.toString()} constructor`);
|
|
111
109
|
}
|
|
112
110
|
// Ask our container to resolve the parameter.
|
|
113
|
-
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
114
111
|
let param = this.injector.resolveState(actualToken);
|
|
115
112
|
// If the parameter could not be resolved, see if there is an @Optional annotation
|
|
116
113
|
if ((!param.pending) && param.rejected) {
|
|
117
|
-
const md = (0,
|
|
114
|
+
const md = (0, decorators_1._getOptionalDefaultAt)(this.maker, index);
|
|
118
115
|
if (md)
|
|
119
|
-
param =
|
|
116
|
+
param = state_1.State.MakeState(null, undefined, md.value);
|
|
120
117
|
}
|
|
121
118
|
return param;
|
|
122
119
|
});
|
|
@@ -137,41 +134,43 @@ class ClassBasedProvider extends bindable_provider_js_1.BindableProvider {
|
|
|
137
134
|
// We do this by mapping each param to a Promise (pending or not), and then awaiting them all.
|
|
138
135
|
// This might create some unnecessary (but immediately resolved) Promise objects,
|
|
139
136
|
// BUT, it allows us to chain for failure *and* substitute the Optional (if one exists).
|
|
140
|
-
const objPromise = this.makePromiseForObj(Promise.all(params.map((p, idx) => {
|
|
137
|
+
const objPromise = this.makePromiseForObj(Promise.all(params.map(async (p, idx) => {
|
|
141
138
|
if (p.pending) {
|
|
142
|
-
|
|
139
|
+
try {
|
|
140
|
+
return await p.promise;
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
143
|
// This was a promised param that failed to resolve.
|
|
144
144
|
// If there is an Optional decorator, use that, otherwise, failure is failure.
|
|
145
|
-
const md = (0,
|
|
145
|
+
const md = (0, decorators_1._getOptionalDefaultAt)(this.maker, idx);
|
|
146
146
|
if (!md)
|
|
147
147
|
throw err;
|
|
148
148
|
return md.value;
|
|
149
|
-
}
|
|
149
|
+
}
|
|
150
150
|
}
|
|
151
|
-
|
|
152
|
-
return Promise.reject(p.rejected);
|
|
153
|
-
return Promise.resolve(p.fulfilled);
|
|
151
|
+
return p.fulfilled;
|
|
154
152
|
})), (values) => {
|
|
155
153
|
if (values) {
|
|
156
154
|
// All the parameters are now available, instantiate the class.
|
|
157
155
|
// If this throws, it will be handled by our caller.
|
|
158
156
|
return Reflect.construct(this.maker, values);
|
|
159
157
|
}
|
|
158
|
+
return undefined;
|
|
160
159
|
});
|
|
161
160
|
// Once the obj is resolved, then we need to check for PostConstruct and if it was async, wait for that too.
|
|
162
|
-
return
|
|
161
|
+
return state_1.State.MakeState((async () => {
|
|
162
|
+
const obj = await objPromise;
|
|
163
163
|
const state = this.makePostConstructState(obj);
|
|
164
164
|
if (state.pending) {
|
|
165
|
-
return state.promise; // chain (aka wait some more).
|
|
165
|
+
return await state.promise; // chain (aka wait some more).
|
|
166
166
|
}
|
|
167
167
|
else if (state.rejected) {
|
|
168
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
169
168
|
return state.rejected; // error
|
|
170
169
|
}
|
|
171
170
|
else {
|
|
172
171
|
return state.fulfilled; // value (aka obj).
|
|
173
172
|
}
|
|
174
|
-
}));
|
|
173
|
+
})());
|
|
175
174
|
}
|
|
176
175
|
else {
|
|
177
176
|
// All parameters needed for construction are available, instantiate the object.
|
|
@@ -182,11 +181,11 @@ class ClassBasedProvider extends bindable_provider_js_1.BindableProvider {
|
|
|
182
181
|
catch (err) {
|
|
183
182
|
// There was an error, give the errorHandler (if any) a crack at recovery.
|
|
184
183
|
try {
|
|
185
|
-
return
|
|
184
|
+
return state_1.State.MakeState(null, undefined, this.queryErrorHandler(err));
|
|
186
185
|
}
|
|
187
186
|
catch (e) {
|
|
188
187
|
// could not recover, propagate the error.
|
|
189
|
-
return
|
|
188
|
+
return state_1.State.MakeState(null, e, undefined);
|
|
190
189
|
}
|
|
191
190
|
}
|
|
192
191
|
}
|
|
@@ -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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AAC,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,gBAAK,CAAC,SAAS,CAAI,IAAI,EAAE,SAAS,EAAE,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC,CAAC,CAAC;iBACxE;gBACD,OAAO,CAAC,EAAE;oBACT,0CAA0C;oBAC1C,OAAO,gBAAK,CAAC,SAAS,CAAI,IAAI,EAAE,CAAC,EAAE,SAAS,CAAC,CAAC;iBAC9C;aACD;SACD;IACF,CAAC;CACD;AA3LD,gDA2LC","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 firstRejectedParam = params.find((p) => {\n\t\t\treturn (!p.pending) && p.rejected;\n\t\t});\n\t\tif (firstRejectedParam)\n\t\t\treturn firstRejectedParam as State<T>;\n\t\tif (params.some(p => p.pending)) {\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\t// We do this by mapping each param to a Promise (pending or not), and then awaiting them all.\n\t\t\t// This might create some unnecessary (but immediately resolved) Promise objects,\n\t\t\t// BUT, it allows us to chain for failure *and* substitute the Optional (if one exists).\n\t\t\tconst objPromise = this.makePromiseForObj<any[]>(Promise.all(params.map((p, idx) => {\n\t\t\t\tif (p.pending) {\n\t\t\t\t\treturn p.promise.catch(err => {\n\t\t\t\t\t\t// This was a promised param that failed to resolve.\n\t\t\t\t\t\t// If there is an Optional decorator, use that, otherwise, failure is failure.\n\t\t\t\t\t\tconst md = _getOptionalDefaultAt(this.maker, idx);\n\t\t\t\t\t\tif (!md)\n\t\t\t\t\t\t\tthrow err;\n\t\t\t\t\t\treturn md.value as unknown;\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tif (p.rejected)\n\t\t\t\t\treturn Promise.reject(p.rejected);\n\t\t\t\treturn Promise.resolve(p.fulfilled);\n\t\t\t})), (values) => {\n\t\t\t\tif (values) {\n\t\t\t\t\t// All the parameters are now available, instantiate the class.\n\t\t\t\t\t// If this throws, it will be handled by our caller.\n\t\t\t\t\treturn Reflect.construct(this.maker, values);\n\t\t\t\t}\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,2DAAqD;AACrD,2CAA8G;AAC9G,6CAAqE;AAErE,mCAA8B;AAC9B,mCAAkC;AAYlC;;;GAGG;AACH,MAAa,kBAAsB,SAAQ,oCAAwC;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,CAAC;YACb,MAAM,GAAG,IAAI,CAAC,kBAAkB,EAAE,CAAC;QACpC,CAAC;QACD,IAAI,IAAI,CAAC,SAAS,KAAK,IAAI;YAC1B,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC;QACzB,OAAO,MAAM,CAAC;IACf,CAAC;IAED;;;OAGG;IACH,kBAAkB,CAAC,SAAkB;QACpC,IAAI,CAAC,CAAC,SAAS,CAAC,IAAI,OAAO,CAAC,WAAW,CAAC,4CAAgC,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,GAAG,KAAK,IAAI,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,IAAK,GAAW,CAAC,WAAW,EAAE,CAAC;YAClG,IAAI,UAAU,GAAG,KAAK,CAAC;YACvB,IAAI,IAA8D,CAAC;YACnE,IAAI,OAAO,IAAI,CAAC,cAAc,KAAK,UAAU,EAA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,CAAC,CAAC,SAAoB,CAAC,CAAC,CAAC;gBACxF,OAAO,IAAI,CAAC,sBAAsB,CAAC,MAAM,CAAC,CAAC;YAC5C,CAAC;YACD,OAAO,GAAG,EAAE,CAAC;gBACZ,0EAA0E;gBAC1E,IAAI,CAAC;oBACJ,OAAO,aAAK,CAAC,SAAS,CAAI,IAAI,EAAE,SAAS,EAAE,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC,CAAC,CAAC;gBACzE,CAAC;gBACD,OAAO,CAAC,EAAE,CAAC;oBACV,0CAA0C;oBAC1C,OAAO,aAAK,CAAC,SAAS,CAAI,IAAI,EAAE,CAAC,EAAE,SAAS,CAAC,CAAC;gBAC/C,CAAC;YACF,CAAC;QACF,CAAC;IACF,CAAC;CACD;AA1LD,gDA0LC","sourcesContent":["import {BindableProvider} from './bindable-provider';\nimport {POSTCONSTRUCT_ASYNC_METADATA_KEY, POSTCONSTRUCT_SYNC_METADATA_KEY, REFLECT_PARAMS} from './constants';\nimport {_getInjectedIdAt, _getOptionalDefaultAt} from './decorators';\nimport {ClassConstructor, InjectableId, Injector} from './injector';\nimport {State} from './state';\nimport {isPromise} from './utils';\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> | undefined {\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 (obj !== null && typeof obj === 'object' && (!Array.isArray(obj)) && (obj as any).constructor) {\n\t\t\tlet maybeAsync = false;\n\t\t\tlet pcFn: (() => void | Error | Promise<void | Error>) | undefined;\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// Check to see if there is a @PostConstruct annotation on a method of the class.\n\t\t\t\tconst ctor = (obj as any).constructor;\n\t\t\t\tlet postConstruct: string = Reflect.getMetadata(POSTCONSTRUCT_SYNC_METADATA_KEY, ctor) as string;\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, ctor) as string;\n\t\t\t\t}\n\t\t\t\tif (postConstruct && ctor.prototype[postConstruct] && typeof ctor.prototype[postConstruct] === 'function')\n\t\t\t\t\tpcFn = (obj as any)[postConstruct].bind?.(obj);\n\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\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\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 firstRejectedParam = params.find((p) => {\n\t\t\treturn (!p.pending) && p.rejected;\n\t\t});\n\t\tif (firstRejectedParam)\n\t\t\treturn firstRejectedParam as State<T>;\n\t\tif (params.some(p => p.pending)) {\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\t// We do this by mapping each param to a Promise (pending or not), and then awaiting them all.\n\t\t\t// This might create some unnecessary (but immediately resolved) Promise objects,\n\t\t\t// BUT, it allows us to chain for failure *and* substitute the Optional (if one exists).\n\t\t\tconst objPromise = this.makePromiseForObj<any[]>(Promise.all(params.map(async (p, idx) => {\n\t\t\t\tif (p.pending) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\treturn await p.promise!;\n\t\t\t\t\t}\n\t\t\t\t\tcatch (err) {\n\t\t\t\t\t\t// This was a promised param that failed to resolve.\n\t\t\t\t\t\t// If there is an Optional decorator, use that, otherwise, failure is failure.\n\t\t\t\t\t\tconst md = _getOptionalDefaultAt(this.maker, idx);\n\t\t\t\t\t\tif (!md)\n\t\t\t\t\t\t\tthrow err;\n\t\t\t\t\t\treturn md.value as unknown;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn p.fulfilled;\n\t\t\t})), (values) => {\n\t\t\t\tif (values) {\n\t\t\t\t\t// All the parameters are now available, instantiate the class.\n\t\t\t\t\t// If this throws, it will be handled by our caller.\n\t\t\t\t\treturn Reflect.construct(this.maker, values);\n\t\t\t\t}\n\t\t\t\treturn undefined as unknown as T;\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>((async () => {\n\t\t\t\tconst obj = await objPromise;\n\t\t\t\tconst state = this.makePostConstructState(obj);\n\t\t\t\tif (state.pending) {\n\t\t\t\t\treturn await 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\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,16 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ConstantProvider = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
4
|
+
const provider_1 = require("./provider");
|
|
5
|
+
const state_1 = require("./state");
|
|
6
6
|
/**
|
|
7
7
|
* @inheritDoc
|
|
8
8
|
* This specialization is always a Singleton.
|
|
9
9
|
*/
|
|
10
|
-
class ConstantProvider extends
|
|
10
|
+
class ConstantProvider extends provider_1.Provider {
|
|
11
11
|
constructor(constant) {
|
|
12
12
|
super();
|
|
13
|
-
this.singleton =
|
|
13
|
+
this.singleton = state_1.State.MakeState(null, undefined, constant);
|
|
14
14
|
}
|
|
15
15
|
provideAsState() {
|
|
16
16
|
return this.singleton;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constant-provider.js","sourceRoot":"","sources":["../../src/constant-provider.ts"],"names":[],"mappings":";;;AAAA
|
|
1
|
+
{"version":3,"file":"constant-provider.js","sourceRoot":"","sources":["../../src/constant-provider.ts"],"names":[],"mappings":";;;AAAA,yCAAoC;AACpC,mCAA8B;AAE9B;;;GAGG;AACH,MAAa,gBAAoB,SAAQ,mBAAW;IACnD,YAAY,QAAW;QACtB,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,SAAS,GAAG,aAAK,CAAC,SAAS,CAAI,IAAI,EAAE,SAAS,EAAE,QAAQ,CAAC,CAAC;IAChE,CAAC;IAED,cAAc;QACb,OAAO,IAAI,CAAC,SAAU,CAAC;IACxB,CAAC;CACD;AATD,4CASC","sourcesContent":["import {Provider} from './provider';\nimport {State} from './state';\n\n/**\n * @inheritDoc\n * This specialization is always a Singleton.\n */\nexport class ConstantProvider<T> extends Provider<T> {\n\tconstructor(constant: T) {\n\t\tsuper();\n\t\tthis.singleton = State.MakeState<T>(null, undefined, constant);\n\t}\n\n\tprovideAsState(): State<T> {\n\t\treturn this.singleton!;\n\t}\n}\n"]}
|