react-application-core
Version:
A react-based application core for the business applications.
68 lines • 2.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DiSupport = exports.bindToConstantValue = exports.bindInSingleton = exports.provideInSingleton = exports.staticInjector = void 0;
var di_module_1 = require("./di.module");
var cachedServices = new Map();
/**
* @stable [26.04.2018]
* @param {interfaces.ServiceIdentifier<T>} serviceIdentifier
* @returns {any}
*/
exports.staticInjector = function (serviceIdentifier) {
// Two-level cache because of a low performance
var instance = cachedServices.get(serviceIdentifier);
if (cachedServices.has(serviceIdentifier)) {
return instance; // Null or instance
}
if (di_module_1.appContainer.isBound(serviceIdentifier)) {
cachedServices.set(serviceIdentifier, instance = di_module_1.appContainer.get(serviceIdentifier));
}
else {
cachedServices.set(serviceIdentifier, instance = null);
}
return instance;
};
/**
* @stable [26.04.2018]
* @param target
* @returns {(target: any) => any}
*/
exports.provideInSingleton = function (target) { return di_module_1.provideInSingletonDecorator(target)
.inSingletonScope()
.done(true); };
/**
* @stable [16.09.2019]
* @param {interfaces.ServiceIdentifier<T> | {new(...args: AnyT[]): T}} contract
* @param {{new(...args: AnyT[]): T}} implementation
* @returns {interfaces.BindingWhenOnSyntax<any>}
*/
exports.bindInSingleton = function (contract, implementation) {
return (di_module_1.appContainer.isBound(contract)
? di_module_1.appContainer.rebind(contract)
: di_module_1.appContainer.bind(contract)).to(implementation || contract).inSingletonScope();
};
/**
* @stable [24.09.2019]
* @param {interfaces.ServiceIdentifier<T>} contract
* @param {T} instance
* @returns {interfaces.BindingWhenOnSyntax<any>}
*/
exports.bindToConstantValue = function (contract, instance) {
return (di_module_1.appContainer.isBound(contract)
? di_module_1.appContainer.rebind(contract)
: di_module_1.appContainer.bind(contract)).toConstantValue(instance);
};
/**
* @stable [08.06.2020]
*/
var DiSupport = /** @class */ (function () {
function DiSupport() {
}
DiSupport.bindInSingleton = exports.bindInSingleton;
DiSupport.bindToConstantValue = exports.bindToConstantValue;
DiSupport.provideInSingleton = exports.provideInSingleton;
DiSupport.staticInjector = exports.staticInjector;
return DiSupport;
}());
exports.DiSupport = DiSupport;
//# sourceMappingURL=di.support.js.map