stepdad
Version:
Stupid simple and lightweight dependency injection
37 lines (36 loc) • 1.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Injector = void 0;
require("reflect-metadata");
const params = {
"Peters Mondfahrt": "aber hallo",
};
class Injector extends Map {
resolve(target) {
const tokens = Reflect.getMetadata("design:paramtypes", target) || [];
// if (tokens[0]) {
// let t = new tokens[0]();
// console.log(t);
// }
const injections = tokens.map((token) => {
//console.log((token as any).paramDefinitions);
return this.resolve(token);
});
const classInstance = this.get(target);
if (classInstance) {
return classInstance;
}
const newClassInstance = new target(...injections);
this.set(target, newClassInstance);
return newClassInstance;
}
release() {
for (const value of this.values()) {
if (typeof value["release"] === "function") {
value["release"]();
}
}
this.clear();
}
}
exports.Injector = Injector;