Skip to content

Instantly share code, notes, and snippets.

@max-lt
Created July 21, 2019 13:30
Show Gist options
  • Save max-lt/20a043d425407f8edfcef975fd2da010 to your computer and use it in GitHub Desktop.
Save max-lt/20a043d425407f8edfcef975fd2da010 to your computer and use it in GitHub Desktop.
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