arcs_module.js 1.7 KB
/**
 * definition of the main module function: 
 * it takes an anonymous function as a parameter
 * the anonymous function has one parameter: the object encompassing 
 * ARCS definitions (in order to able to use ARCS.Component.create, ...)
 * @param moduleDefinition {function} main function of the module. 
 * It should return a list of components
 * @param deps {mixed[]} dependencies
 */

// TODO remove dependency handling


// reimplementation using native promises
arcs_module = function(moduleDefinition) {
    var storeComponents, i;
    
    // TODO verify if this is still needed
    if (typeof module !== 'undefined') {
        if (module.parent.exports) {
            ARCS = module.exports; 
        }
    }
    
    
    storeComponents = function () {
        var mdef, p;
        // we should insert ARCS at the beginning of deps !
        
        mdef = (typeof moduleDefinition === 'function') ?
                moduleDefinition.apply(this) : moduleDefinition;

        if (mdef === undefined) {
            throw new Error("[ARCS] Your module is undefined. Did you forget to export components?\nCode of module follows:\n" +moduleDefinition);
        }

        for (p in mdef) {
            if (mdef.hasOwnProperty(p) && ARCS.Context.currentContext != null) {
                ARCS.Context.currentContext.setFactory(p,mdef[p]); //.setFactory(ARCS.Application.currentApplication, p, mdef[p]);
            }
        }
        
        return Promise.resolve();
    };
    // until now, it is the very same code.
    
    
        ARCS.Context.currentContext.addLibraryPromise(
            storeComponents,
            function(reason) { console.error("[ARCS] Failed to load dependency ", reason ); })
        
        );
    
}