UNPKG

stepdad

Version:

Stupid simple and lightweight dependency injection

42 lines (33 loc) 958 B
import "reflect-metadata"; import { Type } from "../interfaces/Type"; const params = { "Peters Mondfahrt": "aber hallo", }; export class Injector extends Map { public resolve<T>(target: Type<any>): T { const tokens = Reflect.getMetadata("design:paramtypes", target) || []; // if (tokens[0]) { // let t = new tokens[0](); // console.log(t); // } const injections = tokens.map((token: Type<any>) => { //console.log((token as any).paramDefinitions); return this.resolve<any>(token); }); const classInstance = this.get(target); if (classInstance) { return classInstance; } const newClassInstance = new target(...injections); this.set(target, newClassInstance); return newClassInstance; } public release(): void { for (const value of this.values()) { if (typeof value["release"] === "function") { value["release"](); } } this.clear(); } }