Skip to content

Commit

Permalink
fix(container): enable autoregister of non-functions
Browse files Browse the repository at this point in the history
Fixes #31
  • Loading branch information
EisenbergEffect committed Jun 1, 2015
1 parent 47aa487 commit c40ac43
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,16 @@ export class Container {
throw new Error('fn cannot be null or undefined.')
}

registration = Metadata.get(Metadata.registration, fn);
if(typeof fn === 'function'){
registration = Metadata.get(Metadata.registration, fn);

if(registration !== undefined){
registration.register(this, key || fn, fn);
if(registration !== undefined){
registration.register(this, key || fn, fn);
}else{
this.registerSingleton(key || fn, fn);
}
}else{
this.registerSingleton(key || fn, fn);
this.registerInstance(fn, fn);
}
}

Expand Down
18 changes: 18 additions & 0 deletions test/container.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@ describe('container', () => {
expect(app1.logger).toBe(app2.logger);
});

it('automatically configures non-functions as instances', () => {
var someObject = {};

class App1 {
constructor(something) {
this.something = something;
}
}

inject(someObject)(App1);


var container = new Container();
var app1 = container.get(App1);

expect(app1.something).toBe(someObject);
});

it('configures singleton via api', () => {
class Logger {}

Expand Down

0 comments on commit c40ac43

Please sign in to comment.