From 7564f6c3eabe0ac135402de036c9afdc72736759 Mon Sep 17 00:00:00 2001 From: akitaSummer Date: Sat, 14 Sep 2024 16:46:30 +0800 Subject: [PATCH] fix: add preload loadunit --- core/metadata/src/factory/LoadUnitFactory.ts | 22 +++++++++++++++----- standalone/standalone/src/EggModuleLoader.ts | 22 +++++++++++++------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/core/metadata/src/factory/LoadUnitFactory.ts b/core/metadata/src/factory/LoadUnitFactory.ts index 4e89c720..710a65d3 100644 --- a/core/metadata/src/factory/LoadUnitFactory.ts +++ b/core/metadata/src/factory/LoadUnitFactory.ts @@ -14,19 +14,23 @@ export class LoadUnitFactory { private static loadUnitMap: Map = new Map(); private static loadUnitIdMap: Map = new Map(); - static async createLoadUnit(unitPath: string, type: EggLoadUnitTypeLike, loader: Loader): Promise { - if (this.loadUnitMap.has(unitPath)) { - return this.loadUnitMap.get(unitPath)!.loadUnit; - } + protected static async getLoanUnit(ctx: LoadUnitLifecycleContext, type: EggLoadUnitTypeLike) { const creator = this.loadUnitCreatorMap.get(type); if (!creator) { throw new Error(`not find creator for load unit type ${type}`); } + return await creator(ctx); + } + + static async createLoadUnit(unitPath: string, type: EggLoadUnitTypeLike, loader: Loader): Promise { + if (this.loadUnitMap.has(unitPath)) { + return this.loadUnitMap.get(unitPath)!.loadUnit; + } const ctx: LoadUnitLifecycleContext = { unitPath, loader, }; - const loadUnit = await creator(ctx); + const loadUnit = await this.getLoanUnit(ctx, type); await LoadUnitLifecycleUtil.objectPreCreate(ctx, loadUnit); if (loadUnit.init) { await loadUnit.init(ctx); @@ -37,6 +41,14 @@ export class LoadUnitFactory { return loadUnit; } + static async createPreloadLoadUnit(unitPath: string, type: EggLoadUnitTypeLike, loader: Loader): Promise { + const ctx: LoadUnitLifecycleContext = { + unitPath, + loader, + }; + return await this.getLoanUnit(ctx, type); + } + static async destroyLoadUnit(loadUnit: LoadUnit) { const { ctx } = this.loadUnitMap.get(loadUnit.unitPath)!; try { diff --git a/standalone/standalone/src/EggModuleLoader.ts b/standalone/standalone/src/EggModuleLoader.ts index ba2e23b8..7b181afb 100644 --- a/standalone/standalone/src/EggModuleLoader.ts +++ b/standalone/standalone/src/EggModuleLoader.ts @@ -27,10 +27,10 @@ export class EggModuleLoader { return appGraph; } - private static async generateLoadUnits(moduleReferences: readonly ModuleReference[]) { + async load(): Promise { const loadUnits: LoadUnit[] = []; const loaderCache = new Map(); - const appGraph = EggModuleLoader.generateAppGraph(loaderCache, moduleReferences); + const appGraph = EggModuleLoader.generateAppGraph(loaderCache, this.moduleReferences); appGraph.sort(); const moduleConfigList = appGraph.moduleConfigList; for (const moduleConfig of moduleConfigList) { @@ -42,13 +42,19 @@ export class EggModuleLoader { return loadUnits; } - async load(): Promise { - return await EggModuleLoader.generateLoadUnits(this.moduleReferences); - } - static async preLoad(moduleReferences: readonly ModuleReference[]): Promise { - const loads = await EggModuleLoader.generateLoadUnits(moduleReferences); - for (const load of loads) { + const loadUnits: LoadUnit[] = []; + const loaderCache = new Map(); + const appGraph = EggModuleLoader.generateAppGraph(loaderCache, moduleReferences); + appGraph.sort(); + const moduleConfigList = appGraph.moduleConfigList; + for (const moduleConfig of moduleConfigList) { + const modulePath = moduleConfig.path; + const loader = loaderCache.get(modulePath)!; + const loadUnit = await LoadUnitFactory.createPreloadLoadUnit(modulePath, EggLoadUnitType.MODULE, loader); + loadUnits.push(loadUnit); + } + for (const load of loadUnits) { await load.preLoad?.(); } }