All files module.js

86.67% Statements 26/30
83.33% Branches 5/6
84.21% Functions 16/19
86.67% Lines 26/30
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201                                83x 83x 83x 83x   83x   83x                     134x             27x             26x             59x             9x                                           93x                                         96x             48x               73x             24x               1x               27x 1x     26x               26x 1x     25x               23x               26x           26x                                      
/**
 * © 2014 Liferay, Inc. <https://liferay.com>
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */
 
import ResolvablePromise from './resolvable-promise';
 
/**
 * A module descriptor
 */
export default class Module {
	/**
	 * @param {string} name name of module
	 */
	constructor(name) {
		this._name = name;
		this._dependencies = undefined;
		this._factory = undefined;
		this._implementation = {};
 
		this._map = undefined;
 
		this._state = {
			_fetch: ResolvablePromise.new(),
			_define: ResolvablePromise.new(),
			_implement: ResolvablePromise.new(),
		};
	}
 
	/**
	 * Name of module
	 */
	get name() {
		return this._name;
	}
 
	/**
	 * Dependencies of module
	 */
	get dependencies() {
		return this._dependencies;
	}
 
	/**
	 * AMD factory function
	 */
	get factory() {
		return this._factory;
	}
 
	/**
	 * Result of factory invocation (module.exports)
	 */
	get implementation() {
		return this._implementation;
	}
 
	/**
	 * Local module mappings for module
	 */
	get map() {
		return this._map;
	}
 
	/**
	 * Flag informing that this module's implementation needs to have __esModule
	 * property defined as true even before it is implemented.
	 *
	 * This is necessary for cyclic dependencies to work for ES6+ modules.
	 */
	get esModule() {
		return this._implementation.__esModule;
	}
 
	/**
	 * Get the fetch promise which is fulfilled when the script containing the
	 * module definition has been loaded/failed.
	 *
	 * Note that a module may be defined even if it is not yet fetched because
	 * define() gets called while the script is being loaded.
	 * @return {ResolvablePromise}
	 */
	get fetch() {
		return this._state._fetch;
	}
 
	/**
	 * Shorthand for this.fetch.resolved
	 */
	get fetched() {
		return this.fetch.resolved;
	}
 
	/**
	 * Get the define promise which if fulfilled when the script had been
	 * registered by the AMD define() function.
	 *
	 * Note that definition does not imply implementation.
	 *
	 * Also note that a module may be defined even if it is not yet fetched
	 * because define() gets called while the script is being loaded.
	 * @return {ResolvablePromise}
	 */
	get define() {
		return this._state._define;
	}
 
	/**
	 * Shorthand for this.define.resolved
	 */
	get defined() {
		return this.define.resolved;
	}
 
	/**
	 * Get the implement promise which if fulfilled when the module has been
	 * defined and its AMD factory function has been invoked successfully.
	 */
	get implement() {
		return this._state._implement;
	}
 
	/**
	 * Shorthand for this.implement.resolved
	 */
	get implemented() {
		return this.implement.resolved;
	}
 
	/**
	 * Name of module
	 * @param {string} name
	 */
	set name(name) {
		throw new Error(`Name of module ${this.name} is read-only`);
	}
 
	/**
	 * Dependencies of module
	 * @param {Array} dependencies
	 */
	set dependencies(dependencies) {
		if (this._dependencies) {
			throw new Error(`Dependencies of module ${this.name} already set`);
		}
 
		this._dependencies = dependencies;
	}
 
	/**
	 * AMD factory function
	 * @param {function} factory
	 */
	set factory(factory) {
		if (this._factory) {
			throw new Error(`Factory of module ${this.name} already set`);
		}
 
		this._factory = factory;
	}
 
	/**
	 * Result of factory invocation (module.exports)
	 * @param {*} implementation
	 */
	set implementation(implementation) {
		this._implementation = implementation;
	}
 
	/**
	 * Local module mappings for module
	 * @param {object} map
	 */
	set map(map) {
		Iif (this._map) {
			throw new Error(
				`Local module map of module ${this.name} already set`
			);
		}
 
		this._map = map;
	}
 
	/**
	 * Flag informing that this module's implementation needs to have __esModule
	 * property defined as true even before it is implemented.
	 *
	 * This is necessary for cyclic dependencies to work for ES6+ modules.
	 *
	 * @param {boolean} esModule
	 */
	set esModule(esModule) {
		Object.defineProperty(this._implementation, '__esModule', {
			configurable: true,
			value: esModule,
			writable: true,
		});
	}
}