@jems/di
Version:
An implementation of IoC pattern based on dependency injection that allows you to granulate and decouple your libraries or applications. Wrote using SOLID principles and a variety OOP patterns implementations.
47 lines (46 loc) • 2.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var deliveryError_1 = require("../errors/deliveryError");
/**
* Represenst an strategy to deliver a new instance targets per container with an specific strategy.
*/
var ContainerizedDeliveryStrategy = /** @class */ (function () {
function ContainerizedDeliveryStrategy() {
this._containerInstanceMap = [];
}
/**
* Deliver the transformed reference in the provided dependency metadata.
* @param resolutionContext Represents the context in which the request was made.
* @param dependencyMetadata Represents the dependency metadata that will be delivered.
* @return The transformed reference.
*/
ContainerizedDeliveryStrategy.prototype.deliver = function (resolutionContext, dependencyMetadata) {
if (!resolutionContext) {
throw new deliveryError_1.DeliveryError('Must provide a valid resolution context.');
}
if (!resolutionContext.originContainerAlias) {
throw new deliveryError_1.DeliveryError('The provided resolution context must have a valid origin container alias.');
}
if (!dependencyMetadata) {
throw new deliveryError_1.DeliveryError('Must provide the depencency metadata to deliver from.');
}
if (!dependencyMetadata.activationReference) {
throw new deliveryError_1.DeliveryError('The provided dependency metadata must have a valid reference.');
}
if (!dependencyMetadata.servicingStrategy) {
throw new deliveryError_1.DeliveryError('The provided dependency metadata must have a valid servicing strategy.');
}
var map = this._containerInstanceMap.find(function (map) { return map.containerAlias === resolutionContext.originContainerAlias; });
var servingResult;
if (!map) {
servingResult = dependencyMetadata.servicingStrategy.serve(resolutionContext, dependencyMetadata);
this._containerInstanceMap.push({ containerAlias: resolutionContext.originContainerAlias, instance: servingResult });
}
else {
servingResult = map.instance;
}
return servingResult;
};
return ContainerizedDeliveryStrategy;
}());
exports.ContainerizedDeliveryStrategy = ContainerizedDeliveryStrategy;