From 0e2897200a9bc3bc6aa1028c8549bdbf45bbaaa3 Mon Sep 17 00:00:00 2001 From: akitaSummer Date: Sat, 14 Sep 2024 20:49:12 +0800 Subject: [PATCH] fix: add preload loadunit (#236) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ##### Checklist - [ ] `npm test` passes - [ ] tests and/or benchmarks are included - [ ] documentation is changed or added - [ ] commit message follows commit guidelines ##### Affected core subsystem(s) ##### Description of change ## Summary by CodeRabbit - **New Features** - Introduced a new method for preloading load units, enhancing flexibility in handling load unit creation. - **Improvements** - Streamlined the loading process by consolidating module loading logic into a single method, improving efficiency and clarity. - Enhanced the organization of load unit retrieval and creation logic, promoting better maintainability. - **Bug Fixes** - Improved the retrieval and creation logic for load units, reducing potential duplication and enhancing overall functionality. --- core/metadata/src/factory/LoadUnitFactory.ts | 38 +++++++++++++------- standalone/standalone/src/EggModuleLoader.ts | 22 +++++++----- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/core/metadata/src/factory/LoadUnitFactory.ts b/core/metadata/src/factory/LoadUnitFactory.ts index 4e89c720..50dbe560 100644 --- a/core/metadata/src/factory/LoadUnitFactory.ts +++ b/core/metadata/src/factory/LoadUnitFactory.ts @@ -14,48 +14,60 @@ 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; - } - const creator = this.loadUnitCreatorMap.get(type); + protected static async getLoanUnit(ctx: LoadUnitLifecycleContext, type: EggLoadUnitTypeLike) { + const creator = LoadUnitFactory.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 (LoadUnitFactory.loadUnitMap.has(unitPath)) { + return LoadUnitFactory.loadUnitMap.get(unitPath)!.loadUnit; + } const ctx: LoadUnitLifecycleContext = { unitPath, loader, }; - const loadUnit = await creator(ctx); + const loadUnit = await LoadUnitFactory.getLoanUnit(ctx, type); await LoadUnitLifecycleUtil.objectPreCreate(ctx, loadUnit); if (loadUnit.init) { await loadUnit.init(ctx); } await LoadUnitLifecycleUtil.objectPostCreate(ctx, loadUnit); - this.loadUnitMap.set(unitPath, { loadUnit, ctx }); - this.loadUnitIdMap.set(loadUnit.id, loadUnit); + LoadUnitFactory.loadUnitMap.set(unitPath, { loadUnit, ctx }); + LoadUnitFactory.loadUnitIdMap.set(loadUnit.id, loadUnit); return loadUnit; } + static async createPreloadLoadUnit(unitPath: string, type: EggLoadUnitTypeLike, loader: Loader): Promise { + const ctx: LoadUnitLifecycleContext = { + unitPath, + loader, + }; + return await LoadUnitFactory.getLoanUnit(ctx, type); + } + static async destroyLoadUnit(loadUnit: LoadUnit) { - const { ctx } = this.loadUnitMap.get(loadUnit.unitPath)!; + const { ctx } = LoadUnitFactory.loadUnitMap.get(loadUnit.unitPath)!; try { await LoadUnitLifecycleUtil.objectPreDestroy(ctx, loadUnit); if (loadUnit.destroy) { await loadUnit.destroy(ctx); } } finally { - this.loadUnitMap.delete(loadUnit.unitPath); - this.loadUnitIdMap.delete(loadUnit.id); + LoadUnitFactory.loadUnitMap.delete(loadUnit.unitPath); + LoadUnitFactory.loadUnitIdMap.delete(loadUnit.id); LoadUnitLifecycleUtil.clearObjectLifecycle(loadUnit); } } static getLoadUnitById(id: Id): LoadUnit | undefined { - return this.loadUnitIdMap.get(id); + return LoadUnitFactory.loadUnitIdMap.get(id); } static registerLoadUnitCreator(type: EggLoadUnitTypeLike, creator: LoadUnitCreator) { - this.loadUnitCreatorMap.set(type, creator); + LoadUnitFactory.loadUnitCreatorMap.set(type, creator); } } 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?.(); } }