@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>
74 lines (73 loc) • 2.75 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApplicationBean = void 0;
const RuntimeErrors_1 = require("../api/RuntimeErrors");
const ProxyBuilder_1 = require("./ProxyBuilder");
class ApplicationBean {
get objectFactory() {
if (this._objectFactory === null) {
throw new RuntimeErrors_1.RuntimeErrors.IllegalStateError('Object factory not initialized');
}
return this._objectFactory;
}
constructor(id, parentConfiguration, beanClassProperty, beanMetadata, dependencies, classConstructor, scopeRegister) {
this.id = id;
this.parentConfiguration = parentConfiguration;
this.beanClassProperty = beanClassProperty;
this.beanMetadata = beanMetadata;
this.dependencies = dependencies;
this.classConstructor = classConstructor;
this.scopeRegister = scopeRegister;
this.proxy = null;
this._objectFactory = null;
this.getScopedBeanValue = () => {
return this.getScope().get(this.name, this.objectFactory);
};
this.name = `${parentConfiguration.id}_${id}_${beanMetadata.qualifiedName}`;
}
init(objectFactory) {
this._objectFactory = objectFactory;
}
getInjectionValue() {
if (this.isProxy) {
return this.getProxyBean();
}
return this.getScopedBeanValue();
}
get isProxy() {
return this.getScope().useProxy?.() ?? true;
}
get isSingleton() {
const singletonScope = this.scopeRegister.getScope('singleton');
return this.getScope() === singletonScope;
}
get lazy() {
return this.beanMetadata.lazy ?? this.parentConfiguration.metadata.lazy;
}
get scopeName() {
return this.beanMetadata.scope ?? this.parentConfiguration.metadata.scope;
}
get isLifecycleFunction() {
return this.beanMetadata.kind === 5 /* BeanKind.LIFECYCLE_METHOD */ || this.beanMetadata.kind === 6 /* BeanKind.LIFECYCLE_ARROW_FUNCTION */;
}
getScope() {
return this.scopeRegister.getScope(this.scopeName);
}
toMetadata() {
return {
parentConfigurationClassConstructor: this.parentConfiguration.classConstructor,
parentConfigurationInstance: this.parentConfiguration.instance,
name: this.name,
classPropertyName: this.beanClassProperty,
scope: this.scopeName,
resolvedConstructor: this.classConstructor,
};
}
getProxyBean() {
if (!this.proxy) {
this.proxy = ProxyBuilder_1.ProxyBuilder.build(this, this.getScopedBeanValue);
}
return this.proxy;
}
}
exports.ApplicationBean = ApplicationBean;