async-injection
Version:
A robust lightweight dependency injection library for TypeScript.
47 lines • 1.71 kB
JavaScript
import { BindableProvider } from './bindable-provider.js';
import { State } from './state.js';
/**
* @inheritDoc
* This specialization simply invokes it's configured Factory and provides the result.
*/
export class FactoryBasedProvider extends 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.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.MakeState(null, undefined, this.queryErrorHandler(err));
}
catch (e) {
// could not recover, propagate the error.
retVal = 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);
}
}
//# sourceMappingURL=sync-factory-provider.js.map