async initializeModulesAync()

in modularity/module-manager.js [358:418]


    async initializeModulesAync(toInitializedModules) {
        const moduleInfos = Object.values(toInitializedModules);

        /** @type {number} */
        let previousCount = moduleInfos.length;

        /** @type {number} */
        let triedCount = 0;

        /** @type {Donuts.Modularity.ILoadedModuleInfo} */
        let moduleInfo;

        for (moduleInfo of moduleInfos) {
            if (this.loadedModules[moduleInfo.name]) {
                if (this.loadedModules[moduleInfo.name] === moduleInfo.version) {
                    continue;
                }

                throw new Error(`A different version of module ${moduleInfo.name}@${this.loadedModules[moduleInfo.name]} already registered. (Module to register: ${moduleInfo.name}@${moduleInfo.version}).`);
            }

            await this.registerComponentsAsync(moduleInfo.namespace || moduleInfo.name, moduleInfo.components);
        }

        moduleLoop:
        while (moduleInfo = moduleInfos.shift()) {
            for (const depName in moduleInfo.dependencies) {
                if (!(depName in this.loadedModules)) {

                    if (!(depName in toInitializedModules)) {
                        throw new Error(`Required dependent module "${depName}" by module "${moduleInfo.name}" cannot be found.`);
                    }

                    moduleInfos.push(moduleInfo);

                    // circular references check.
                    if (previousCount === moduleInfos.length) {
                        triedCount++;

                        if (triedCount >= moduleInfos.length) {
                            throw new Error(
                                "There are circular references among the following modules: "
                                + moduleInfos.map((moduleInfo) => moduleInfo.name).join(", "));
                        }

                    } else {
                        previousCount = moduleInfos.length;
                        triedCount = 0;
                    }

                    continue moduleLoop;
                }
            }

            if (utils.isFunction(moduleInfo.module.initializeAsync)) {
                await moduleInfo.module.initializeAsync(this);
            }

            this.loadedModules[moduleInfo.name] = moduleInfo.version;
        }
    }