@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>
43 lines (42 loc) • 1.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.InternalScopeRegister = void 0;
const SingletonScope_1 = require("./SingletonScope");
const RuntimeErrors_1 = require("../api/RuntimeErrors");
const TransientScope_1 = require("./TransientScope");
class InternalScopeRegister {
constructor(scopes) {
this.scopes = new Map(scopes);
}
registerScope(name, scope) {
if (this.scopes.has(name)) {
throw new RuntimeErrors_1.RuntimeErrors.DuplicateScopeError(`Scope with name ${name} is already registered.`);
}
this.scopes.set(name, scope);
}
registerScopeAlias(from, to) {
if (this.hasScope(to)) {
throw new RuntimeErrors_1.RuntimeErrors.DuplicateScopeError(`Scope with name ${to} is already registered.`);
}
const scope = this.getScope(from);
this.scopes.set(to, scope);
}
getScope(name) {
return this.assureRegistered(name);
}
hasScope(name) {
return this.scopes.has(name);
}
assureRegistered(name) {
const scope = this.scopes.get(name);
if (!scope) {
throw new RuntimeErrors_1.RuntimeErrors.ScopeIsNotRegisteredError(`Scope with name ${name} is not registered.`);
}
return scope;
}
}
exports.InternalScopeRegister = InternalScopeRegister;
InternalScopeRegister.global = new InternalScopeRegister(new Map([
['singleton', new SingletonScope_1.SingletonScope()],
['transient', new TransientScope_1.TransientScope()],
]));