Skip to content

Commit

Permalink
fix(container): factories are explicit
Browse files Browse the repository at this point in the history
Factories should be rarely used, so the normal is constructors.
Factories are now explicitly indicated with Factory metadata. This
should also fix minification issues. Fixes #13
  • Loading branch information
EisenbergEffect committed Mar 22, 2015
1 parent 186b573 commit 667a16e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 33 deletions.
25 changes: 19 additions & 6 deletions src/container.js
@@ -1,9 +1,22 @@
import {Metadata} from 'aurelia-metadata';
import {Resolver, Registration} from './metadata';
import {isClass} from './util';
import {Resolver, Registration, Factory} from './metadata';

var emptyParameters = Object.freeze([]);

// Fix Function#name on browsers that do not support it (IE):
function test(){}
if (!test.name) {
Object.defineProperty(Function.prototype, 'name', {
get: function() {
var name = this.toString().match(/^\s*function\s*(\S*)\s*\(/)[1];
// For better performance only parse once, and then cache the
// result through a new accessor for repeated access.
Object.defineProperty(this, 'name', { value: name });
return name;
}
});
}

/**
* A lightweight, extensible dependency injection container.
*
Expand Down Expand Up @@ -270,16 +283,16 @@ export class Container {
throw error;
}

if(info.isClass){
if(info.isFactory){
return fn.apply(undefined, args);
}else{
context = Object.create(fn.prototype);

if('initialize' in fn){
fn.initialize(context);
}

return fn.apply(context, args) || context;
}else{
return fn.apply(undefined, args);
}
}

Expand Down Expand Up @@ -312,7 +325,7 @@ export class Container {
}

createConstructionInfo(fn){
var info = {isClass: isClass(fn)};
var info = {isFactory: Metadata.on(fn).has(Factory)};

if(fn.inject !== undefined){
if(typeof fn.inject === 'function'){
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Expand Up @@ -13,10 +13,11 @@ export {
Lazy,
All,
Optional,
Parent
Parent,
Factory
} from './metadata';

export {Container} from './container';

Metadata.configure.classHelper('transient', Transient);
Metadata.configure.classHelper('singleton', Singleton);
Metadata.configure.classHelper('singleton', Singleton);
8 changes: 8 additions & 0 deletions src/metadata.js
Expand Up @@ -254,3 +254,11 @@ export class Parent extends Resolver {
return new Parent(key);
}
}

/**
* An annotation used to indicate that a particular function is a factory rather than a constructor.
*
* @class Factory
* @constructor
*/
export class Factory {}
25 changes: 0 additions & 25 deletions src/util.js

This file was deleted.

0 comments on commit 667a16e

Please sign in to comment.