async-injection
Version:
A robust lightweight dependency injection library for TypeScript.
101 lines • 4.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BindableProvider = void 0;
const provider_js_1 = require("./provider.js");
const utils_js_1 = require("./utils.js");
/**
* @inheritDoc
* This abstraction is for Providers that can be additionally configured as Singletons and/or configured with error and/or success handling callback(s).
*/
class BindableProvider extends provider_js_1.Provider {
constructor(injector, id, maker) {
super();
this.injector = injector;
this.id = id;
this.maker = maker;
}
/**
* Invoked by the Binder to create chain-able configuration
*
* @see BindAs
*/
makeBindAs() {
const retVal = {
onError: (cb) => {
this.errorHandler = cb;
},
onSuccess: (cb) => {
this.successHandler = cb;
return retVal;
},
asSingleton: () => {
this.singleton = null; // Flag state as no longer undefined.
return retVal;
}
};
return retVal;
}
/**
* Encapsulate the logic of invoking any configured error handler, and processing it's result.
*
* @see OnErrorCallback
*
* @returns The object substituted by the callback (otherwise this method throws the appropriate error).
*/
queryErrorHandler(err, obj) {
// There was an error during construction, see if an error handler was provided, and if so, see what it wants to do.
if (this.errorHandler) {
const handlerResult = this.errorHandler(this.injector, this.id, this.maker, err, obj);
// Error handler wants us to propagate an error.
if ((0, utils_js_1.isErrorObj)(handlerResult))
throw handlerResult;
// Error handler has no opinion, so provideAsState a state that reflects the error we just caught.
if (typeof handlerResult === 'undefined')
throw err;
// Error handler provided a valid (fully resolved) replacement.
return handlerResult;
}
// No error handler, provideAsState a state that reflects the error we just caught.
throw err;
}
/**
* This is like a retry mechanism that uses the Provider's errorHandler (if any) to attempt recovery whenever the supplied Promise rejects.
* This method returns a Promise that rejects if recovery was not possible.
* If the supplied Promise resolves, then this method passes the result to the callback, and then resolve as whatever that callback returns.
*
* @param waitFor The supplied Promise.
* @param cb Callback to be invoked if the supplied Promise resolves.
*/
makePromiseForObj(waitFor, cb) {
return new Promise((resolve, reject) => {
const errHandlerFn = (err, objValue) => {
// There was an error during async post construction, see if an error handler was provided, and if so, see what it wants to do.
if (this.errorHandler) {
const handlerResult = this.errorHandler(this.injector, this.id, this.maker, err, objValue);
// Error handler wants us to propagate an alternative error.
if ((0, utils_js_1.isErrorObj)(handlerResult))
err = handlerResult; // Fall thru
else if (typeof handlerResult !== 'undefined') {
resolve(handlerResult); // Error handler provided a replacement, so change the State that we returned from pending to resolved.
return;
}
}
// This will change the State that we returned from pending to rejected.
reject(err);
};
waitFor.then((result) => {
// This will change the State that we returned from pending to resolved.
try {
resolve(cb(result));
}
catch (err) {
errHandlerFn(err, cb(result));
}
}).catch((err) => {
errHandlerFn(err, cb(undefined));
});
});
}
}
exports.BindableProvider = BindableProvider;
//# sourceMappingURL=bindable-provider.js.map