async-injection 1.5.2 → 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 +9 -0
- package/ReadMe.md +0 -1
- package/lib/cjs/async-factory-provider.d.ts +4 -4
- package/lib/cjs/async-factory-provider.js +4 -4
- package/lib/cjs/async-factory-provider.js.map +1 -1
- package/lib/cjs/bindable-provider.d.ts +3 -3
- package/lib/cjs/bindable-provider.js +5 -5
- package/lib/cjs/bindable-provider.js.map +1 -1
- package/lib/cjs/binder.d.ts +2 -2
- package/lib/cjs/binder.js.map +1 -1
- package/lib/cjs/class-provider.d.ts +3 -3
- package/lib/cjs/class-provider.js +29 -26
- package/lib/cjs/class-provider.js.map +1 -1
- package/lib/cjs/constant-provider.d.ts +2 -2
- package/lib/cjs/constant-provider.js +4 -4
- package/lib/cjs/constant-provider.js.map +1 -1
- package/lib/cjs/container.d.ts +5 -5
- package/lib/cjs/container.js +18 -18
- package/lib/cjs/container.js.map +1 -1
- package/lib/cjs/decorators.d.ts +1 -1
- package/lib/cjs/decorators.js +14 -15
- package/lib/cjs/decorators.js.map +1 -1
- package/lib/cjs/index.d.ts +4 -4
- package/lib/cjs/index.js +11 -10
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/provider.d.ts +1 -1
- package/lib/cjs/provider.js +3 -3
- 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.d.ts +4 -4
- package/lib/cjs/sync-factory-provider.js +6 -6
- package/lib/cjs/sync-factory-provider.js.map +1 -1
- package/lib/cjs/utils.js +2 -2
- package/lib/cjs/utils.js.map +1 -1
- package/lib/esm/async-factory-provider.d.ts +4 -4
- package/lib/esm/async-factory-provider.js +2 -2
- package/lib/esm/async-factory-provider.js.map +1 -1
- package/lib/esm/bindable-provider.d.ts +3 -3
- package/lib/esm/bindable-provider.js +2 -2
- package/lib/esm/bindable-provider.js.map +1 -1
- package/lib/esm/binder.d.ts +2 -2
- package/lib/esm/binder.js.map +1 -1
- package/lib/esm/class-provider.d.ts +3 -3
- package/lib/esm/class-provider.js +13 -10
- package/lib/esm/class-provider.js.map +1 -1
- package/lib/esm/constant-provider.d.ts +2 -2
- package/lib/esm/constant-provider.js +2 -2
- package/lib/esm/constant-provider.js.map +1 -1
- package/lib/esm/container.d.ts +5 -5
- package/lib/esm/container.js +8 -8
- package/lib/esm/container.js.map +1 -1
- package/lib/esm/decorators.d.ts +1 -1
- package/lib/esm/decorators.js +2 -3
- package/lib/esm/decorators.js.map +1 -1
- package/lib/esm/index.d.ts +4 -4
- package/lib/esm/index.js +3 -3
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/provider.d.ts +1 -1
- package/lib/esm/provider.js +1 -1
- package/lib/esm/provider.js.map +1 -1
- package/lib/esm/state.js +1 -1
- package/lib/esm/state.js.map +1 -1
- package/lib/esm/sync-factory-provider.d.ts +4 -4
- package/lib/esm/sync-factory-provider.js +2 -2
- package/lib/esm/sync-factory-provider.js.map +1 -1
- package/lib/esm/utils.js +1 -1
- package/lib/esm/utils.js.map +1 -1
- package/package.json +14 -14
package/Changelog.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
## 1.5.4 / 2023-11-15
|
|
2
|
+
* Improved handling of class constructor parameter type recognition.
|
|
3
|
+
* Update dependencies.
|
|
4
|
+
|
|
5
|
+
## 1.5.3 / 2023-06-05
|
|
6
|
+
* Address esm issues [#10](https://github.com/pcafstockf/async-injection/issues/10) and [#15](https://github.com/pcafstockf/async-injection/issues/15)
|
|
7
|
+
* Fix missing type overload on Binder interface.
|
|
8
|
+
* Update dev dependencies.
|
|
9
|
+
|
|
1
10
|
## 1.5.2 / 2023-01-18
|
|
2
11
|
* No code changes.
|
|
3
12
|
* Updates to docs for consistency across projects.
|
package/ReadMe.md
CHANGED
|
@@ -34,7 +34,6 @@ import "reflect-metadata";
|
|
|
34
34
|
```
|
|
35
35
|
|
|
36
36
|
Please note that this library supports a wide variety of runtimes and is distributed as both esm and cjs modules, side by side.
|
|
37
|
-
Please see this [link](https://github.com/pcafstockf/async-injection/issues/10) if you experience module errors when compiling.
|
|
38
37
|
|
|
39
38
|
## Basic Usage (synchronous)
|
|
40
39
|
Here we 'get' a new transaction handling object, that itself, relies on a shared service:
|
|
@@ -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.js';
|
|
2
|
+
import { AsyncFactory } from './binder.js';
|
|
3
|
+
import { InjectableId, Injector } from './injector.js';
|
|
4
|
+
import { State } from './state.js';
|
|
5
5
|
/**
|
|
6
6
|
* @inheritDoc
|
|
7
7
|
* This specialization invokes it's configured Factory asynchronously and waits until it can provide the result.
|
|
@@ -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_js_1 = require("./bindable-provider.js");
|
|
5
|
+
const state_js_1 = require("./state.js");
|
|
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_js_1.BindableProvider {
|
|
11
11
|
constructor(injector, id, maker) {
|
|
12
12
|
super(injector, id, maker);
|
|
13
13
|
}
|
|
@@ -20,7 +20,7 @@ class AsyncFactoryBasedProvider extends bindable_provider_1.BindableProvider {
|
|
|
20
20
|
if (!retVal) {
|
|
21
21
|
// Wrap the async factory's Promise in an errorHandler aware Promise.
|
|
22
22
|
// Our contract is that an AsyncFactory may not throw and must return a valid Promise (e.g. pending, resolved, rejected, etc).
|
|
23
|
-
retVal =
|
|
23
|
+
retVal = state_js_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,iEAAwD;AAGxD,yCAAiC;AAEjC;;;GAGG;AACH,MAAa,yBAA6B,SAAQ,uCAAoC;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;YACZ,qEAAqE;YACrE,8HAA8H;YAC9H,MAAM,GAAG,gBAAK,CAAC,SAAS,CAAI,IAAI,CAAC,iBAAiB,CAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;SAC9F;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.js';\nimport {AsyncFactory} from './binder.js';\nimport {InjectableId, Injector} from './injector.js';\nimport {State} from './state.js';\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,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.js';
|
|
2
|
+
import { ClassConstructor, InjectableId, Injector } from './injector.js';
|
|
3
|
+
import { Provider } from './provider.js';
|
|
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).
|
|
@@ -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_js_1 = require("./provider.js");
|
|
5
|
+
const utils_js_1 = require("./utils.js");
|
|
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_js_1.Provider {
|
|
11
11
|
constructor(injector, id, maker) {
|
|
12
12
|
super();
|
|
13
13
|
this.injector = injector;
|
|
@@ -47,7 +47,7 @@ class BindableProvider extends provider_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_js_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')
|
|
@@ -73,7 +73,7 @@ class BindableProvider extends provider_1.Provider {
|
|
|
73
73
|
if (this.errorHandler) {
|
|
74
74
|
const handlerResult = this.errorHandler(this.injector, this.id, this.maker, err, objValue);
|
|
75
75
|
// Error handler wants us to propagate an alternative error.
|
|
76
|
-
if ((0,
|
|
76
|
+
if ((0, utils_js_1.isErrorObj)(handlerResult))
|
|
77
77
|
err = handlerResult; // Fall thru
|
|
78
78
|
else if (typeof handlerResult !== 'undefined') {
|
|
79
79
|
resolve(handlerResult); // Error handler provided a replacement, so change the State that we returned from pending to resolved.
|
|
@@ -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,+CAAuC;AACvC,yCAAsC;AAEtC;;;GAGG;AACH,MAAsB,gBAAgF,SAAQ,sBAAW;IACxH,YAAgC,QAAkB,EAAY,EAAmB,EAAY,KAAQ;QACpG,KAAK,EAAE,CAAC;QADuB,aAAQ,GAAR,QAAQ,CAAU;QAAY,OAAE,GAAF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AC,YAAY,EAAE;oBACtB,MAAM,aAAa,GAAG,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,KAAK,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;oBAC3F,4DAA4D;oBAC5D,IAAI,IAAA,qBAAU,EAAC,aAAa,CAAC;wBAC5B,GAAG,GAAG,aAAa,CAAC,CAAG,YAAY;yBAC/B,IAAI,OAAO,aAAa,KAAK,WAAW,EAAE;wBAC9C,OAAO,CAAC,aAAa,CAAC,CAAC,CAAI,uGAAuG;wBAClI,OAAO;qBACP;iBACD;gBACD,wEAAwE;gBACxE,MAAM,CAAC,GAAG,CAAC,CAAC;YACb,CAAC,CAAC;YACF,OAAO,CAAC,IAAI,CACX,CAAC,MAAM,EAAE,EAAE;gBACV,wEAAwE;gBACxE,IAAI;oBACH,OAAO,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;iBACpB;gBACD,OAAO,GAAG,EAAE;oBACX,YAAY,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;iBAC9B;YACF,CAAC,CACD,CAAC,KAAK,CACN,CAAC,GAAG,EAAE,EAAE;gBACP,YAAY,CAAC,GAAG,EAAE,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC;YAClC,CAAC,CACD,CAAC;QACH,CAAC,CAAC,CAAC;IACJ,CAAC;CACD;AAzGD,4CAyGC","sourcesContent":["import {AsyncFactory, BindAs, OnErrorCallback, OnSuccessCallback, SyncFactory} from './binder.js';\nimport {ClassConstructor, InjectableId, Injector} from './injector.js';\nimport {Provider} from './provider.js';\nimport {isErrorObj} from './utils.js';\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 makePromiseForObj<R>(waitFor: Promise<R>, cb: (result: R) => T): Promise<T> {\n\t\treturn new Promise<T>((resolve, reject) => {\n\t\t\tconst errHandlerFn = (err: any, objValue?: T) => {\n\t\t\t\t// There was an error during async post construction, see if an error handler was provided, and if so, see what it wants to do.\n\t\t\t\tif (this.errorHandler) {\n\t\t\t\t\tconst handlerResult = this.errorHandler(this.injector, this.id, this.maker, err, objValue);\n\t\t\t\t\t// Error handler wants us to propagate an alternative error.\n\t\t\t\t\tif (isErrorObj(handlerResult))\n\t\t\t\t\t\terr = handlerResult; // Fall thru\n\t\t\t\t\telse if (typeof handlerResult !== 'undefined') {\n\t\t\t\t\t\tresolve(handlerResult); // Error handler provided a replacement, so change the State that we returned from pending to resolved.\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t// This will change the State that we returned from pending to rejected.\n\t\t\t\treject(err);\n\t\t\t};\n\t\t\twaitFor.then(\n\t\t\t\t(result) => {\n\t\t\t\t\t// This will change the State that we returned from pending to resolved.\n\t\t\t\t\ttry {\n\t\t\t\t\t\tresolve(cb(result));\n\t\t\t\t\t}\n\t\t\t\t\tcatch (err) {\n\t\t\t\t\t\terrHandlerFn(err, cb(result));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t).catch(\n\t\t\t\t(err) => {\n\t\t\t\t\terrHandlerFn(err, cb(undefined));\n\t\t\t\t}\n\t\t\t);\n\t\t});\n\t}\n}\n"]}
|
package/lib/cjs/binder.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AbstractConstructor, ClassConstructor, InjectableId, Injector } from './injector';
|
|
1
|
+
import { AbstractConstructor, ClassConstructor, InjectableId, Injector } from './injector.js';
|
|
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.
|
|
@@ -80,7 +80,7 @@ export interface Binder extends Injector {
|
|
|
80
80
|
* The container will also invoke any `@PostConstruct` present on the class.
|
|
81
81
|
*/
|
|
82
82
|
bindClass<T>(id: ClassConstructor<T>, constructor?: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;
|
|
83
|
-
bindClass<T>(id: string | symbol | AbstractConstructor<T>, constructor: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;
|
|
83
|
+
bindClass<T>(id: string | symbol | AbstractConstructor<T> | InjectableId<T>, constructor: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;
|
|
84
84
|
/**
|
|
85
85
|
* Bind an InjectableId to a synchronous factory that will be invoked on demand when the object is needed.
|
|
86
86
|
* The factory should produce the needed value
|
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';\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 */\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): void;\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>, 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
|
+
{"version":3,"file":"binder.js","sourceRoot":"","sources":["../../src/binder.ts"],"names":[],"mappings":"","sourcesContent":["import {AbstractConstructor, ClassConstructor, InjectableId, Injector} from './injector.js';\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 */\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): void;\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,6 +1,6 @@
|
|
|
1
|
-
import { BindableProvider } from './bindable-provider';
|
|
2
|
-
import { ClassConstructor, InjectableId, Injector } from './injector';
|
|
3
|
-
import { State } from './state';
|
|
1
|
+
import { BindableProvider } from './bindable-provider.js';
|
|
2
|
+
import { ClassConstructor, InjectableId, Injector } from './injector.js';
|
|
3
|
+
import { State } from './state.js';
|
|
4
4
|
interface StateResolvingInjector extends Injector {
|
|
5
5
|
resolveState<T>(id: InjectableId<T>): State<T>;
|
|
6
6
|
}
|
|
@@ -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_js_1 = require("./bindable-provider.js");
|
|
5
|
+
const constants_js_1 = require("./constants.js");
|
|
6
|
+
const decorators_js_1 = require("./decorators.js");
|
|
7
|
+
const state_js_1 = require("./state.js");
|
|
8
|
+
const utils_js_1 = require("./utils.js");
|
|
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_js_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_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_js_1.POSTCONSTRUCT_ASYNC_METADATA_KEY, this.maker))
|
|
37
37
|
return super.resolveIfSingleton(false);
|
|
38
38
|
return undefined;
|
|
39
39
|
}
|
|
@@ -54,10 +54,10 @@ class ClassBasedProvider extends bindable_provider_1.BindableProvider {
|
|
|
54
54
|
else {
|
|
55
55
|
/* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */
|
|
56
56
|
// Check to see if there is a @PostConstruct annotation on a method of the class.
|
|
57
|
-
let postConstruct = Reflect.getMetadata(
|
|
57
|
+
let postConstruct = Reflect.getMetadata(constants_js_1.POSTCONSTRUCT_SYNC_METADATA_KEY, obj.constructor);
|
|
58
58
|
if (!postConstruct) {
|
|
59
59
|
maybeAsync = true;
|
|
60
|
-
postConstruct = Reflect.getMetadata(
|
|
60
|
+
postConstruct = Reflect.getMetadata(constants_js_1.POSTCONSTRUCT_ASYNC_METADATA_KEY, obj.constructor);
|
|
61
61
|
}
|
|
62
62
|
if (postConstruct && obj.constructor.prototype[postConstruct] && typeof obj.constructor.prototype[postConstruct] === 'function')
|
|
63
63
|
pcFn = (_b = (_a = obj[postConstruct]).bind) === null || _b === void 0 ? void 0 : _b.call(_a, obj);
|
|
@@ -72,48 +72,51 @@ class ClassBasedProvider extends bindable_provider_1.BindableProvider {
|
|
|
72
72
|
// The post construction method threw while executing, give the errorHandler (if any) a crack at recovery.
|
|
73
73
|
try {
|
|
74
74
|
obj = this.queryErrorHandler(err, obj); // The returned obj is unlikely to be the original obj.
|
|
75
|
-
return
|
|
75
|
+
return state_js_1.State.MakeState(null, undefined, obj);
|
|
76
76
|
}
|
|
77
77
|
catch (e) {
|
|
78
78
|
// could not recover, propagate the error.
|
|
79
|
-
return
|
|
79
|
+
return state_js_1.State.MakeState(null, e, undefined);
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
// The post construction method says it will let us know when it's finished.
|
|
83
|
-
if (result && (result instanceof Promise || (maybeAsync && (0,
|
|
83
|
+
if (result && (result instanceof Promise || (maybeAsync && (0, utils_js_1.isPromise)(result)))) {
|
|
84
84
|
// Return a State that is pending (the other return statements in this method return a State which is resolved or rejected).
|
|
85
85
|
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
86
|
-
return
|
|
86
|
+
return state_js_1.State.MakeState(this.makePromiseForObj(result, () => obj));
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
90
|
// No PostConstruct, just return a resolved State
|
|
91
|
-
return
|
|
91
|
+
return state_js_1.State.MakeState(null, undefined, obj);
|
|
92
92
|
}
|
|
93
93
|
/**
|
|
94
94
|
* This method collects the States of all the constructor parameters for our target class.
|
|
95
95
|
*/
|
|
96
96
|
getConstructorParameterStates() {
|
|
97
|
-
const argTypes = Reflect.getMetadata(
|
|
97
|
+
const argTypes = Reflect.getMetadata(constants_js_1.REFLECT_PARAMS, this.maker);
|
|
98
98
|
if (argTypes === undefined || !Array.isArray(argTypes)) {
|
|
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
|
-
//
|
|
107
|
-
const overrideToken = (0, decorators_1._getInjectedIdAt)(this.maker, index);
|
|
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.
|
|
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);
|
|
112
115
|
// If the parameter could not be resolved, see if there is an @Optional annotation
|
|
113
116
|
if ((!param.pending) && param.rejected) {
|
|
114
|
-
const md = (0,
|
|
117
|
+
const md = (0, decorators_js_1._getOptionalDefaultAt)(this.maker, index);
|
|
115
118
|
if (md)
|
|
116
|
-
param =
|
|
119
|
+
param = state_js_1.State.MakeState(null, undefined, md.value);
|
|
117
120
|
}
|
|
118
121
|
return param;
|
|
119
122
|
});
|
|
@@ -144,7 +147,7 @@ class ClassBasedProvider extends bindable_provider_1.BindableProvider {
|
|
|
144
147
|
return Reflect.construct(this.maker, params.map((p) => p.fulfilled));
|
|
145
148
|
});
|
|
146
149
|
// Once the obj is resolved, then we need to check for PostConstruct and if it was async, wait for that too.
|
|
147
|
-
return
|
|
150
|
+
return state_js_1.State.MakeState(objPromise.then((obj) => {
|
|
148
151
|
const state = this.makePostConstructState(obj);
|
|
149
152
|
if (state.pending) {
|
|
150
153
|
return state.promise; // chain (aka wait some more).
|
|
@@ -167,11 +170,11 @@ class ClassBasedProvider extends bindable_provider_1.BindableProvider {
|
|
|
167
170
|
catch (err) {
|
|
168
171
|
// There was an error, give the errorHandler (if any) a crack at recovery.
|
|
169
172
|
try {
|
|
170
|
-
return
|
|
173
|
+
return state_js_1.State.MakeState(null, undefined, this.queryErrorHandler(err));
|
|
171
174
|
}
|
|
172
175
|
catch (e) {
|
|
173
176
|
// could not recover, propagate the error.
|
|
174
|
-
return
|
|
177
|
+
return state_js_1.State.MakeState(null, e, undefined);
|
|
175
178
|
}
|
|
176
179
|
}
|
|
177
180
|
}
|
|
@@ -1 +1 @@
|
|
|
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;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;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CAAC;gBACxF,OAAO,IAAI,CAAC,sBAAsB,CAAC,MAAM,CAAC,CAAC;aAC3C;YACD,OAAO,GAAG,EAAE;gBACX,0EAA0E;gBAC1E,IAAI;oBACH,OAAO,aAAK,CAAC,SAAS,CAAI,IAAI,EAAE,SAAS,EAAE,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC,CAAC,CAAC;iBACxE;gBACD,OAAO,CAAC,EAAE;oBACT,0CAA0C;oBAC1C,OAAO,aAAK,CAAC,SAAS,CAAI,IAAI,EAAE,CAAC,EAAE,SAAS,CAAC,CAAC;iBAC9C;aACD;SACD;IACF,CAAC;CACD;AA5KD,gDA4KC","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> {\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,CA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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;AA/KD,gDA+KC","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"]}
|
|
@@ -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_js_1 = require("./provider.js");
|
|
5
|
+
const state_js_1 = require("./state.js");
|
|
6
6
|
/**
|
|
7
7
|
* @inheritDoc
|
|
8
8
|
* This specialization is always a Singleton.
|
|
9
9
|
*/
|
|
10
|
-
class ConstantProvider extends
|
|
10
|
+
class ConstantProvider extends provider_js_1.Provider {
|
|
11
11
|
constructor(constant) {
|
|
12
12
|
super();
|
|
13
|
-
this.singleton =
|
|
13
|
+
this.singleton = state_js_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,+CAAuC;AACvC,yCAAiC;AAEjC;;;GAGG;AACH,MAAa,gBAAoB,SAAQ,sBAAW;IACnD,YAAY,QAAW;QACtB,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,SAAS,GAAG,gBAAK,CAAC,SAAS,CAAI,IAAI,EAAE,SAAS,EAAE,QAAQ,CAAC,CAAC;IAChE,CAAC;IAED,cAAc;QACb,OAAO,IAAI,CAAC,SAAS,CAAC;IACvB,CAAC;CACD;AATD,4CASC","sourcesContent":["import {Provider} from './provider.js';\nimport {State} from './state.js';\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"]}
|
package/lib/cjs/container.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { AsyncFactory, BindAs, Binder, SyncFactory } from './binder';
|
|
2
|
-
import { AbstractConstructor, ClassConstructor, InjectableId, Injector } from './injector';
|
|
3
|
-
import { Provider } from './provider';
|
|
4
|
-
import { State } from './state';
|
|
1
|
+
import { AsyncFactory, BindAs, Binder, SyncFactory } from './binder.js';
|
|
2
|
+
import { AbstractConstructor, ClassConstructor, InjectableId, Injector } from './injector.js';
|
|
3
|
+
import { Provider } from './provider.js';
|
|
4
|
+
import { State } from './state.js';
|
|
5
5
|
/**
|
|
6
6
|
* Binder and Injector (aka Container) to handle (a)synchronous dependency management.
|
|
7
7
|
*/
|
|
@@ -41,7 +41,7 @@ export declare class Container implements Binder {
|
|
|
41
41
|
* @inheritDoc
|
|
42
42
|
*/
|
|
43
43
|
bindClass<T>(id: ClassConstructor<T>, constructor?: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;
|
|
44
|
-
bindClass<T>(id: string | symbol | AbstractConstructor<T>, constructor: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;
|
|
44
|
+
bindClass<T>(id: string | symbol | AbstractConstructor<T> | InjectableId<T>, constructor: ClassConstructor<T>): BindAs<T, ClassConstructor<T>>;
|
|
45
45
|
/**
|
|
46
46
|
* @inheritDoc
|
|
47
47
|
*/
|
package/lib/cjs/container.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Container = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
4
|
+
const async_factory_provider_js_1 = require("./async-factory-provider.js");
|
|
5
|
+
const bindable_provider_js_1 = require("./bindable-provider.js");
|
|
6
|
+
const class_provider_js_1 = require("./class-provider.js");
|
|
7
|
+
const constant_provider_js_1 = require("./constant-provider.js");
|
|
8
|
+
const constants_js_1 = require("./constants.js");
|
|
9
|
+
const state_js_1 = require("./state.js");
|
|
10
|
+
const sync_factory_provider_js_1 = require("./sync-factory-provider.js");
|
|
11
|
+
const utils_js_1 = require("./utils.js");
|
|
12
12
|
/**
|
|
13
13
|
* Helper class to ensure we can distinguish between Error instances legitimately returned from Providers, and Errors thrown by Providers.
|
|
14
14
|
*
|
|
@@ -62,7 +62,7 @@ class Container {
|
|
|
62
62
|
*/
|
|
63
63
|
resolve(id) {
|
|
64
64
|
const state = this.resolveState(id);
|
|
65
|
-
if ((0,
|
|
65
|
+
if ((0, utils_js_1.isPromise)(state.promise)) {
|
|
66
66
|
return state.promise;
|
|
67
67
|
}
|
|
68
68
|
if (state.rejected) {
|
|
@@ -96,17 +96,17 @@ class Container {
|
|
|
96
96
|
* @inheritDoc
|
|
97
97
|
*/
|
|
98
98
|
bindConstant(id, value) {
|
|
99
|
-
this.providers.set(id, new
|
|
99
|
+
this.providers.set(id, new constant_provider_js_1.ConstantProvider(value));
|
|
100
100
|
}
|
|
101
101
|
bindClass(id, constructor) {
|
|
102
102
|
if (typeof constructor === 'undefined') {
|
|
103
103
|
constructor = id;
|
|
104
104
|
}
|
|
105
|
-
if (!Reflect.getMetadata(
|
|
105
|
+
if (!Reflect.getMetadata(constants_js_1.INJECTABLE_METADATA_KEY, constructor)) {
|
|
106
106
|
throw new Error('Class not decorated with @Injectable [' + constructor.toString() + ']');
|
|
107
107
|
}
|
|
108
108
|
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
109
|
-
const provider = new
|
|
109
|
+
const provider = new class_provider_js_1.ClassBasedProvider(this, id, constructor);
|
|
110
110
|
this.providers.set(id, provider);
|
|
111
111
|
return provider.makeBindAs();
|
|
112
112
|
}
|
|
@@ -114,7 +114,7 @@ class Container {
|
|
|
114
114
|
* @inheritDoc
|
|
115
115
|
*/
|
|
116
116
|
bindFactory(id, factory) {
|
|
117
|
-
const provider = new
|
|
117
|
+
const provider = new sync_factory_provider_js_1.FactoryBasedProvider(this, id, factory);
|
|
118
118
|
this.providers.set(id, provider);
|
|
119
119
|
return provider.makeBindAs();
|
|
120
120
|
}
|
|
@@ -122,7 +122,7 @@ class Container {
|
|
|
122
122
|
* @inheritDoc
|
|
123
123
|
*/
|
|
124
124
|
bindAsyncFactory(id, factory) {
|
|
125
|
-
const provider = new
|
|
125
|
+
const provider = new async_factory_provider_js_1.AsyncFactoryBasedProvider(this, id, factory);
|
|
126
126
|
this.providers.set(id, provider);
|
|
127
127
|
return provider.makeBindAs();
|
|
128
128
|
}
|
|
@@ -186,13 +186,13 @@ class Container {
|
|
|
186
186
|
// This code (below) will only ever execute if the creator of this container passes in their own implementation of an Injector.
|
|
187
187
|
/* istanbul ignore next */
|
|
188
188
|
try {
|
|
189
|
-
return
|
|
189
|
+
return state_js_1.State.MakeState(this.parent.resolve(id), undefined, undefined);
|
|
190
190
|
}
|
|
191
191
|
catch (err) {
|
|
192
|
-
return
|
|
192
|
+
return state_js_1.State.MakeState(null, err);
|
|
193
193
|
}
|
|
194
194
|
}
|
|
195
|
-
return
|
|
195
|
+
return state_js_1.State.MakeState(null, new Error('Symbol not bound: ' + id.toString()));
|
|
196
196
|
}
|
|
197
197
|
return provider.provideAsState();
|
|
198
198
|
}
|
|
@@ -232,7 +232,7 @@ class Container {
|
|
|
232
232
|
clazz = Container;
|
|
233
233
|
const retVal = new clazz(this.parent);
|
|
234
234
|
this.providers.forEach((v, k) => {
|
|
235
|
-
if (v instanceof
|
|
235
|
+
if (v instanceof bindable_provider_js_1.BindableProvider) {
|
|
236
236
|
v = Object.assign(Object.create(Object.getPrototypeOf(v)), v);
|
|
237
237
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
238
238
|
v.injector = retVal;
|