Code coverage report for ./src/Container.js

Statements: 97.56% (40 / 41)      Branches: 95.65% (22 / 23)      Functions: 90% (9 / 10)      Lines: 97.56% (40 / 41)      Ignored: none     

All files » ./src\ » Container.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 701 1   1   1 19 19     1 122 1     121 121 1     120 3     117 117 117 117 7   117       1 1 1 1 1     1     1 117 117 117 117 25     92 246 246 246       92 246 246       1        
void function (define) {
    define(
        function (require) {
            var u = require('./util');
 
            function Container(context) {
                this.context = context;
                this.singletons = {};
            }
 
            Container.prototype.createInstance = function (component, cb) {
                if (!component) {
                    return cb(null);
                }
 
                var id = component.id;
                if (component.scope === 'singleton' && this.singletons.hasOwnProperty(id)) {
                    return cb(this.singletons[id]);
                }
 
                if (component.scope === 'static') {
                    return cb(component.creator);
                }
 
                var me = this;
                createArgs(this, component, function (args) {
                    var instance = component.creator.apply(null, args);
                    if (component.scope === 'singleton') {
                        me.singletons[id] = instance;
                    }
                    cb(instance);
                });
            };
 
            Container.prototype.dispose = function () {
                var singletons = this.singletons;
                for (var k in singletons) {
                    var instance = singletons[k];
                    instance && typeof instance.dispose === 'function' && instance.dispose();
                }
 
                this.singletons = null;
            };
 
            function createArgs(container, component, cb) {
                var argConfigs = component.args;
                var count = argConfigs.length;
                var args = Array(count);
                if (!count) {
                    return cb(args);
                }
 
                var done = function (index) {
                    return function (instance) {
                        args[index] = instance;
                        --count === 0 && cb(args);
                    };
                };
 
                for (var i = argConfigs.length - 1; i > -1; --i) {
                    var arg = argConfigs[i];
                    u.hasReference(arg) ? container.context.getComponent(arg.$ref, done(i)) : done(i)(arg);
                }
            }
 
            return Container;
 
        });
 
}(typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory; });