Created
July 21, 2019 13:30
-
-
Save max-lt/20a043d425407f8edfcef975fd2da010 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'reflect-metadata'; | |
interface Type<T> { | |
new(...args: any[]): T; | |
} | |
export const Injector = new class extends Map { | |
resolve<T>(target: Type<T>): T { | |
let tokens = Reflect.getMetadata('design:paramtypes', target) || []; | |
console.log('Target', target.name); | |
let injections = tokens.map((token) => Injector.resolve(token)); | |
let instance = this.get(target) | |
if (instance) { | |
return instance; | |
} | |
instance = new target(...injections); | |
console.log(`Instanciating ${instance.constructor.name}`) | |
this.set(target, instance); | |
return instance; | |
} | |
}; | |
export function Injectable(): ClassDecorator { | |
return function (target) { }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment