di.js icon indicating copy to clipboard operation
di.js copied to clipboard

feat(injector): implement a different approach to injector scopes

Open rodyhaddad opened this issue 11 years ago • 2 comments

An easy way to review this is to read the tests and see if that's what we want

Review on Reviewable

rodyhaddad avatar Jun 12 '14 01:06 rodyhaddad

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 avatar Jun 12 '14 01:06 rodyhaddad

@rodyhaddad this is awesome. Thank you! I left some subtle comments, nothing serious...

vojtajina avatar Jun 17 '14 18:06 vojtajina