@clawject/di
Version:
<p align="center"> <a href="https://clawject.com/" target="_blank"><img src="https://clawject.com/img/logo.svg" align="center" alt="Clawject Logo" width="120" height="120" /></a> </p>
76 lines (75 loc) • 3.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApplicationBeanDependency = void 0;
const Utils_1 = require("../Utils");
const DependencyInjectionValue_1 = require("./DependencyInjectionValue");
const ProxyBuilder_1 = require("./ProxyBuilder");
const BeanDependencyIdProvider_1 = require("./BeanDependencyIdProvider");
class ApplicationBeanDependency {
constructor(metadata, applicationBeanFinder) {
this.metadata = metadata;
this.applicationBeanFinder = applicationBeanFinder;
this.id = BeanDependencyIdProvider_1.BeanDependencyIdProvider.getAndInc();
}
async getValue() {
switch (this.metadata.kind) {
case 'plain':
return this.getPlainValue(this.metadata);
case 'value':
return this.getValueValue(this.metadata);
case 'set':
case 'map':
case 'array':
return this.getCollectionValue(this.metadata);
}
}
async getPlainValue(metadata) {
const bean = this.applicationBeanFinder.find(metadata.configurationIndex, metadata.classPropertyName);
return bean.isProxy
? this.getProxyInjectionValue(bean, metadata)
: this.getNonProxyInjectionValue(bean, metadata);
}
async getNonProxyInjectionValue(bean, metadata) {
const beanValue = await bean.getInjectionValue();
if (metadata.nestedProperty) {
return new DependencyInjectionValue_1.DependencyInjectionValue(beanValue[metadata.nestedProperty]);
}
return new DependencyInjectionValue_1.DependencyInjectionValue(beanValue);
}
getProxyInjectionValue(bean, metadata) {
const injectionValue = bean.getInjectionValue();
const nestedProperty = metadata.nestedProperty;
if (nestedProperty) {
const proxy = ProxyBuilder_1.ProxyBuilder.build(bean, () => injectionValue[nestedProperty]);
return new DependencyInjectionValue_1.DependencyInjectionValue(proxy);
}
return new DependencyInjectionValue_1.DependencyInjectionValue(bean.getInjectionValue());
}
getValueValue(metadata) {
return new DependencyInjectionValue_1.DependencyInjectionValue(metadata.value);
}
async getCollectionValue(metadata) {
if (metadata.kind === 'array' || metadata.kind === 'set') {
const values = await Promise.all(metadata.metadata.map(it => this.getPlainValue(it)));
const valuesArray = values.map(it => it.value);
if (metadata.kind === 'array') {
return new DependencyInjectionValue_1.DependencyInjectionValue(valuesArray);
}
return new DependencyInjectionValue_1.DependencyInjectionValue(new Set(valuesArray));
}
const mapEntries = await Promise.all(metadata.metadata.map(async (it) => {
let name = it.classPropertyName;
const nestedProperty = it.nestedProperty;
if (nestedProperty) {
const capitalizedNested = Utils_1.Utils.capitalizeFirstLetter(nestedProperty);
name = `${name}${capitalizedNested}`;
}
const value = await this.getPlainValue(it);
return [
name, value.value,
];
}));
return new DependencyInjectionValue_1.DependencyInjectionValue(new Map(mapEntries));
}
}
exports.ApplicationBeanDependency = ApplicationBeanDependency;