Skip to content

Commit

Permalink
fix: add preload loadunit
Browse files Browse the repository at this point in the history
  • Loading branch information
akitaSummer committed Sep 14, 2024
1 parent 44f076b commit 7564f6c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
22 changes: 17 additions & 5 deletions core/metadata/src/factory/LoadUnitFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ export class LoadUnitFactory {
private static loadUnitMap: Map<string, LoadUnitPair> = new Map();
private static loadUnitIdMap: Map<Id, LoadUnit> = new Map();

static async createLoadUnit(unitPath: string, type: EggLoadUnitTypeLike, loader: Loader): Promise<LoadUnit> {
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<LoadUnit> {
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);
Expand All @@ -37,6 +41,14 @@ export class LoadUnitFactory {
return loadUnit;
}

static async createPreloadLoadUnit(unitPath: string, type: EggLoadUnitTypeLike, loader: Loader): Promise<LoadUnit> {
const ctx: LoadUnitLifecycleContext = {
unitPath,
loader,
};
return await this.getLoanUnit(ctx, type);
}

static async destroyLoadUnit(loadUnit: LoadUnit) {
const { ctx } = this.loadUnitMap.get(loadUnit.unitPath)!;
try {
Expand Down
22 changes: 14 additions & 8 deletions standalone/standalone/src/EggModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export class EggModuleLoader {
return appGraph;
}

private static async generateLoadUnits(moduleReferences: readonly ModuleReference[]) {
async load(): Promise<LoadUnit[]> {
const loadUnits: LoadUnit[] = [];
const loaderCache = new Map<string, Loader>();
const appGraph = EggModuleLoader.generateAppGraph(loaderCache, moduleReferences);
const appGraph = EggModuleLoader.generateAppGraph(loaderCache, this.moduleReferences);
appGraph.sort();
const moduleConfigList = appGraph.moduleConfigList;
for (const moduleConfig of moduleConfigList) {
Expand All @@ -42,13 +42,19 @@ export class EggModuleLoader {
return loadUnits;
}

async load(): Promise<LoadUnit[]> {
return await EggModuleLoader.generateLoadUnits(this.moduleReferences);
}

static async preLoad(moduleReferences: readonly ModuleReference[]): Promise<void> {
const loads = await EggModuleLoader.generateLoadUnits(moduleReferences);
for (const load of loads) {
const loadUnits: LoadUnit[] = [];
const loaderCache = new Map<string, Loader>();
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?.();
}
}
Expand Down

0 comments on commit 7564f6c

Please sign in to comment.