di.js
di.js copied to clipboard
feat(injector): implement a different approach to injector scopes
This PR makes this test pass (discussed with vojta, having this test pass is not set in stone):
it('should do stuff and work', function () {
class RouteScope extends ScopeAnnotation {}
function Compiler() {}
@Inject(Compiler)
function Foo(compiler) {
this.compiler = compiler;
}
@RouteScope
@Inject(Foo, Compiler)
function RequestHandler(foo, compiler) {
this.foo = foo;
this.compiler = compiler;
}
@Provide(Compiler)
function RouteCompiler() {}
var injector = new Injector(),
child = injector.createChild([RouteCompiler], [RouteScope]);
var reqHandler = child.get(RequestHandler);
expect(reqHandler.compiler).not.toBe(reqHandler.foo.compiler);
expect(reqHandler.compiler).toBeInstanceOf(RouteCompiler);
expect(reqHandler.foo.compiler).toBeInstanceOf(Compiler);
});
I'll amend the PR to add this test if that's the behavior that we want.
@rodyhaddad this is awesome. Thank you! I left some subtle comments, nothing serious...