2023-09-08 19:52:21 +00:00
|
|
|
"use strict";
|
|
|
|
(function modulePrivateClosure () {
|
|
|
|
/**
|
|
|
|
* A class that represents Mimicry Module
|
|
|
|
* */
|
|
|
|
class Module {
|
|
|
|
/**
|
|
|
|
* @param {Array<Dependency>} deps - array of files that this module depends on
|
|
|
|
* @param {Function} body - body function of the module that defines the module itself
|
|
|
|
* */
|
|
|
|
constructor (deps, body) {
|
|
|
|
this._dependencies = deps;
|
|
|
|
this._body = body;
|
|
|
|
this._loaded = 0;
|
|
|
|
this._callbacks = [];
|
|
|
|
this._value = null;
|
|
|
|
this._executed = false;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @returns {Boolean} - true if the module have already been executed
|
|
|
|
* */
|
|
|
|
get executed () {
|
|
|
|
return this._executed;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @returns {any} - a value that is returned from the module body function
|
|
|
|
* */
|
|
|
|
get value () {
|
|
|
|
return this._value;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @returns {Boolean} - true if the module has its dependencies resolved
|
|
|
|
* */
|
|
|
|
get allDependenciesResolved () {
|
|
|
|
return this.executed || this._loaded === this._dependencies.length;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* This function should be called when one of the dependencies are considered to be satisfied
|
|
|
|
* If the dependency is not a module this happens when the file is loaded
|
|
|
|
* If the dependency is a module this happens when the module is executed
|
|
|
|
*
|
|
|
|
* @param {Mimicry} mimicry - Mimicry instance that was loading this module,
|
|
|
|
* it is needed for calling "execute" if the dependencies are resolved
|
2023-09-09 13:22:19 +00:00
|
|
|
* @param {any} global - a global object for current platform
|
2023-09-08 19:52:21 +00:00
|
|
|
* */
|
2023-09-09 13:22:19 +00:00
|
|
|
incrementDependency (mimicry, global) {
|
2023-09-08 19:52:21 +00:00
|
|
|
++this._loaded
|
|
|
|
if (this.allDependenciesResolved)
|
2023-09-09 13:22:19 +00:00
|
|
|
this.execute(mimicry, global);
|
2023-09-08 19:52:21 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* If the modules is executed - calls the callback immediately
|
|
|
|
* Otherwise remembers the callback and executes all of them in the same order
|
|
|
|
* that they were added.
|
|
|
|
*
|
|
|
|
* Callback is called with the following arguments:
|
|
|
|
* first: global object
|
|
|
|
* second: the value of this module
|
|
|
|
*
|
|
|
|
* @param {Function} callback - a callback to call
|
2023-09-09 13:22:19 +00:00
|
|
|
* @param {any} global - a global object for current platform
|
2023-09-08 19:52:21 +00:00
|
|
|
* */
|
2023-09-09 13:22:19 +00:00
|
|
|
addCallback (/*Function*/callback, global) {
|
2023-09-08 19:52:21 +00:00
|
|
|
if (this._executed)
|
2023-09-09 13:22:19 +00:00
|
|
|
callback.call(null, global, this._value);
|
2023-09-08 19:52:21 +00:00
|
|
|
else
|
|
|
|
this._callbacks.push(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes a module, launching its body function
|
|
|
|
*
|
|
|
|
* @param {Mimicry} mimicry - an instance of Mimicry that was loading this module
|
|
|
|
* It is needed to acquire values of dependencies
|
2023-09-09 13:22:19 +00:00
|
|
|
* @param {any} global - a global object for current platform
|
2023-09-08 19:52:21 +00:00
|
|
|
* */
|
2023-09-09 13:22:19 +00:00
|
|
|
execute (mimicry, global) {
|
2023-09-08 19:52:21 +00:00
|
|
|
if (this.executed)
|
|
|
|
throw new Error("An attempt to execute a module for the second time");
|
|
|
|
|
|
|
|
if (this._body instanceof Function) {
|
|
|
|
const values = [];
|
|
|
|
for (let i = 0; i < this._dependencies.length; ++i) {
|
|
|
|
const name = this._dependencies[i].name;
|
|
|
|
values.push(mimicry.getModule(name) || mimicry.getAsset(name));
|
|
|
|
}
|
2023-09-09 13:22:19 +00:00
|
|
|
this._value = this._body.call(null, global, values);
|
2023-09-08 19:52:21 +00:00
|
|
|
} else {
|
|
|
|
this._value = null;
|
|
|
|
}
|
|
|
|
this._executed = true;
|
|
|
|
const callbacks = this._callbacks;
|
|
|
|
delete this._callbacks;
|
|
|
|
delete this._dependencies;
|
|
|
|
|
|
|
|
for (let i = 0; i < callbacks.length; ++i)
|
2023-09-09 13:22:19 +00:00
|
|
|
callbacks[i].call(null, global, this._value);
|
2023-09-08 19:52:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof module === 'object' && module.exports)
|
|
|
|
module.exports = Module;
|
|
|
|
else
|
|
|
|
window.__MimicryModule__ = Module;
|
|
|
|
})();
|