Code coverage report for src/Container.js

Statements: 97.06% (33 / 34)      Branches: 94.74% (18 / 19)      Functions: 85.71% (6 / 7)      Lines: 97.06% (33 / 34)      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 601 1   1   1 18 18     1 108 1     107 107 1     106 3     103 103 103 6   103     1 1 1 1 1     1     1 103 103 103 219 219         103     1        
void function (define) {
    define(
        function (require) {
            var util = require('./util');
 
            function Container(context) {
                this.context = context;
                this.singletons = {};
            }
 
            Container.prototype.createInstance = function (component) {
                if (!component) {
                    return null;
                }
 
                var id = component.id;
                if (component.scope === 'singleton' && this.singletons.hasOwnProperty(id)) {
                    return this.singletons[id];
                }
 
                if (component.scope === 'static') {
                    return component.creator;
                }
 
                var args = createArgs(this, component);
                var instance = component.creator.apply(null, args);
                if (component.scope === 'singleton') {
                    this.singletons[id] = instance;
                }
                return 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) {
                var argConfigs = component.args;
                var args = Array(argConfigs.length);
                for (var i = argConfigs.length - 1; i > -1; --i) {
                    var arg = argConfigs[i];
                    args[i] = util.hasReference(arg) ?
                        container.createInstance(container.context.getComponentConfig(arg.$ref))
                        : arg;
                }
 
                return args;
            }
 
            return Container;
 
        });
 
}(typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory; });