async-injection
Version:
A robust lightweight dependency injection library for TypeScript.
51 lines • 1.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FactoryBasedProvider = void 0;
const bindable_provider_js_1 = require("./bindable-provider.js");
const state_js_1 = require("./state.js");
/**
* @inheritDoc
* This specialization simply invokes it's configured Factory and provides the result.
*/
class FactoryBasedProvider extends bindable_provider_js_1.BindableProvider {
constructor(injector, id, maker) {
super(injector, id, maker);
}
/**
* @inheritDoc
* This specialization invokes it's configured Factory and provides the result (or invokes the error handler if necessary).
*/
provideAsState() {
let retVal = this.singleton;
if (!retVal) {
try {
retVal = state_js_1.State.MakeState(null, undefined, this.maker(this.injector));
}
catch (err) {
// There was an error, give the errorHandler (if any) a crack at recovery.
try {
// queryErrorHandler will throw if it could not obtain a substitute object.
retVal = state_js_1.State.MakeState(null, undefined, this.queryErrorHandler(err));
}
catch (e) {
// could not recover, propagate the error.
retVal = state_js_1.State.MakeState(null, e, undefined);
}
}
}
if (this.singleton === null)
this.singleton = retVal;
return retVal;
}
/**
* @inheritDoc
* This specialization returns undefined anytime 'asyncOnly' is true (since this Provider is by definition synchronous).
*/
resolveIfSingleton(asyncOnly) {
if (asyncOnly)
return undefined;
return super.resolveIfSingleton(false);
}
}
exports.FactoryBasedProvider = FactoryBasedProvider;
//# sourceMappingURL=sync-factory-provider.js.map