@miracledevs/paradigm-web-di
Version:
A minimal dependency injection library for the web
78 lines • 3.94 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DependencyContainer = void 0;
require("reflect-metadata");
const object_type_1 = require("./object-type");
const dependency_life_time_1 = require("./dependency-life-time");
class DependencyContainer {
constructor(parent, collection, name) {
this._scopedInstances = new Map();
this._collection = collection;
this._parent = parent;
this.name = name;
}
static createFromCollection(collection, name) {
if (!collection)
throw new Error("Can not create a dependency container without a dependency collection.");
return new DependencyContainer(undefined, collection, name);
}
createScopedInjector(name) {
return new DependencyContainer(this, this._collection, name);
}
resolve(objectType) {
try {
if (objectType === DependencyContainer)
return this;
// this will throw if the object type is not registered.
const descriptor = this._collection.get(objectType);
switch (descriptor.lifeTime) {
//////////////////////////////////////////////////////////////
// Singletons
//////////////////////////////////////////////////////////////
// if the descriptor tells is a singleton we try to create
// the instance or we return the instance we have.
case dependency_life_time_1.DependencyLifeTime.Singleton:
if (!descriptor.instance)
descriptor.instance = this.createInstance(objectType, descriptor);
return descriptor.instance;
//////////////////////////////////////////////////////////////
// Scoped
//////////////////////////////////////////////////////////////
// the scoped instances are scoped singletons, singletons that
// live the scoped injector instead of the global scope
case dependency_life_time_1.DependencyLifeTime.Scoped:
if (!this._parent)
throw new Error("Can not instantiate a scoped type in the global container.");
const instance = this.getInstance(objectType);
if (!instance) {
this._scopedInstances.set(objectType, this.createInstance(objectType, descriptor));
return this._scopedInstances.get(objectType);
}
return instance;
//////////////////////////////////////////////////////////////
// Transient
//////////////////////////////////////////////////////////////
case dependency_life_time_1.DependencyLifeTime.Transient:
return this.createInstance(objectType, descriptor);
default:
throw new Error(`Life time parameter not recognized as a valid life time.`);
}
}
catch (e) {
throw new Error(`Couldn't instantiate the type ${(0, object_type_1.getObjectTypeName)(objectType)}.\n - ${(e instanceof Error ? e.message : e)}`);
}
}
createInstance(objectType, descriptor) {
const parameters = [null].concat(descriptor.dependencies.map(x => this.resolve(x)));
return new (Function.prototype.bind.apply(objectType, parameters));
}
getInstance(objectType) {
if (this._scopedInstances.has(objectType))
return this._scopedInstances.get(objectType);
if (this._parent)
return this._parent.getInstance(objectType);
return undefined;
}
}
exports.DependencyContainer = DependencyContainer;
//# sourceMappingURL=dependency-container.js.map