all files / montage/core/ module-reference.js

63.64% Statements 7/11
50% Branches 3/6
66.67% Functions 2/3
63.64% Lines 7/11
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96                        77×     77× 77×   77×                                                                                                                                                    
/**
 * @module montage/core/module-reference
 * @requires montage/core/core
 */
var Montage = require("./core").Montage;
 
/**
 * @class ModuleReference
 * @extends Montage
 */
exports.ModuleReference = Montage.specialize( /** @lends ModuleReference.prototype */ {
 
    initWithIdAndRequire: {
        value: function (id, require) {
            Iif (!id || ! require) {
                throw new Error("Module ID and require required");
            }
            this.id = id;
            this.require = require;
 
            return this;
        }
    },
 
    /**
     * The absolute id of the module within the `require` package
     * @property {string} value
     */
    id: {
        value: null
    },
 
    /**
     * The require of a package.
     * @property {function} value
     */
    require: {
        value: null
    },
 
    _exports: {
        value: null
    },
    /**
     * A promise for the exports of the module. The exports are loaded lazily
     * when the property is accessed.
     *
     * @returns {Promise.<Object>} The exports of the module.
     */
    exports: {
        get: function () {
            if (this._exports) {
                return this._exports;
            }
            return this._exports = this.require.async(this.id);
        }
    },
 
    /**
     * Resolves this module reference so that it can be required from
     * otherRequire.
     *
     * @function
     * @param {function} otherRequire - Require from another package that has
     * the package of this module as a dependency.
     * @returns {string} The module id to pass to otherRequire that results
     * in this module.
     * @throws {Error} If there is no mapping from this require inside
     * otherRequire.
     *
     * @example
     * var ref = new ModuleReference().initWithIdAndRequire("core/uuid", montageRequire);
     * ref.resolve(applicationRequire); // => "montage/core/uuid"
     *
     * @example
     * var ref = new ModuleReference().initWithIdAndRequire("ui/main.reel", applicationRequire);
     * ref.resolve(montageRequire); // => Error
     * // because there is no module id such that montageRequire(id) can
     * // return the module from inside your application
     */
    resolve: {
        value: function (otherRequire) {
            return otherRequire.identify(this.id, this.require);
        }
    },
 
    // Used for cross-frame detection, similar to Array.isArray, but just
    // a property as there's no need for complex logic.
    isModuleReference: {
        writable: false,
        configurable: false,
        value: true
    }
});