Skip to content

Commit

Permalink
fix: add preload loadunit (#236)
Browse files Browse the repository at this point in the history
<!--
Thank you for your pull request. Please review below requirements.
Bug fixes and new features should include tests and possibly benchmarks.
Contributors guide:
https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md

感谢您贡献代码。请确认下列 checklist 的完成情况。
Bug 修复和新功能必须包含测试,必要时请附上性能测试。
Contributors guide:
https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
-->

##### Checklist
<!-- Remove items that do not apply. For completed items, change [ ] to
[x]. -->

- [ ] `npm test` passes
- [ ] tests and/or benchmarks are included
- [ ] documentation is changed or added
- [ ] commit message follows commit guidelines

##### Affected core subsystem(s)
<!-- Provide affected core subsystem(s). -->


##### Description of change
<!-- Provide a description of the change below this comment. -->

<!--
- any feature?
- close https://github.com/eggjs/egg/ISSUE_URL
-->

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## 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.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
akitaSummer authored Sep 14, 2024
1 parent c433897 commit 0e28972
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
38 changes: 25 additions & 13 deletions core/metadata/src/factory/LoadUnitFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,60 @@ 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;
}
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<LoadUnit> {
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<LoadUnit> {
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);
}
}
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 0e28972

Please sign in to comment.