{"size": 3327, "ext": "js", "max_stars_repo_path": "dist/nger-core/lib/platform/platform_ref.js", "max_stars_repo_name": "meepobrother/nger", "max_stars_repo_stars_event_min_datetime": "2019-04-22T13:40:45.000Z", "max_stars_repo_stars_event_max_datetime": "2021-04-11T15:08:55.000Z", "max_issues_repo_path": "dist/nger-core/lib/platform/platform_ref.js", "max_issues_repo_name": "meepobrother/nger", "max_issues_repo_issues_event_min_datetime": "2019-04-24T13:29:00.000Z", "max_issues_repo_issues_event_max_datetime": "2021-04-07T09:49:20.000Z", "max_forks_repo_path": "dist/nger-core/lib/platform/platform_ref.js", "max_forks_repo_name": "meepobrother/nger", "max_forks_repo_forks_event_min_datetime": "2019-04-23T11:19:09.000Z", "max_forks_repo_forks_event_max_datetime": "2020-08-19T13:24:52.000Z"}
{"max_stars_count": 61, "max_issues_count": 2, "max_forks_count": 16, "avg_line_length": 36.1630434783, "max_line_length": 99, "alphanum_fraction": 0.664262098}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ng_module_ref_1 = require("./ng_module_ref");
const ng_module_factory_1 = require("./ng_module_factory");
const error_handler_1 = require("./error_handler");
const lang_1 = require("./lang");
const application_init_status_1 = require("./application_init_status");
class NgModuleBootstrap {
}
exports.NgModuleBootstrap = NgModuleBootstrap;
class PlatformRef {
    constructor(_injector) {
        this._injector = _injector;
        this._modules = [];
        this._destroyListeners = [];
        this._destroyed = false;
    }
    get injector() { return this._injector; }
    get destroyed() { return this._destroyed; }
    async bootstrapModule(moduleType, compilerOptions = {}) {
        const options = lang_1.optionsReducer({}, compilerOptions);
        // 注册injector
        return compileNgModuleFactory(this.injector, options, moduleType)
            .then(moduleFactory => {
            return this.bootstrapModuleFactory(moduleFactory, options);
        });
    }
    async bootstrapModuleFactory(moduleFactory, options) {
        // todo 注入启动参数
        const injector = this.injector.create([], moduleFactory.moduleType.name);
        const moduleRef = moduleFactory.create(injector);
        const exceptionHandler = moduleRef.injector.get(error_handler_1.ErrorHandler, undefined);
        if (!exceptionHandler) {
            throw new Error('No ErrorHandler. Please Regist ErrorHandler');
        }
        moduleRef.onDestroy(() => lang_1.remove(this._modules, moduleRef));
        moduleRef.injector.setStatic([{
                provide: ng_module_ref_1.NgModuleRef,
                useValue: moduleRef
            }]);
        const initStatus = moduleRef.injector.get(application_init_status_1.ApplicationInitStatus);
        await initStatus.runInitializers();
        await this._moduleDoBootstrap(moduleRef);
        return moduleRef;
    }
    _moduleDoBootstrap(moduleRef) {
        const bootstrap = this.injector.get(NgModuleBootstrap, []);
        console.log(`总共有 ${bootstrap.length} 个启动项目`);
        const tasks = [];
        bootstrap.map((b) => {
            tasks.push(b.run(moduleRef));
        });
        return Promise.all(tasks);
    }
    // 注册销毁钩子
    onDestroy(callback) {
        this._destroyListeners.push(callback);
    }
    // 销毁
    destroy() {
        if (this._destroyed) {
            throw new Error('The platform has already been destroyed!');
        }
        this._modules.slice().forEach(module => module.destroy());
        this._destroyListeners.forEach(listener => listener());
        this._destroyed = true;
    }
}
exports.PlatformRef = PlatformRef;
function compileNgModuleFactory(injector, options, moduleType) {
    const compilerFactory = injector.get(CompilerFactory);
    const compiler = compilerFactory.createCompiler([options]);
    return compiler.compileModuleAsync(moduleType);
}
class CompilerFactory {
    createCompiler(options) {
        return new Compiler(options);
    }
}
exports.CompilerFactory = CompilerFactory;
// 编译器
class Compiler {
    constructor(options) {
        this.options = options;
    }
    // 编译
    async compileModuleAsync(moduleType) {
        return new ng_module_factory_1.NgModuleFactory(moduleType);
    }
}
exports.Compiler = Compiler;
