-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: init all context advice if root proto miss
- Loading branch information
Showing
7 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Application } from 'egg'; | ||
import { EggProtoImplClass, LifecycleHook, ObjectInitType, PrototypeUtil } from '@eggjs/tegg'; | ||
import { EggContext, EggContextLifecycleContext } from '@eggjs/tegg-runtime'; | ||
import { AspectInfoUtil } from '@eggjs/aop-decorator'; | ||
import { EggPrototype, TeggError } from '@eggjs/tegg-metadata'; | ||
import { ROOT_PROTO } from '@eggjs/egg-module-common'; | ||
|
||
export interface EggPrototypeWithClazz extends EggPrototype { | ||
clazz?: EggProtoImplClass; | ||
} | ||
|
||
export interface ProtoToCreate { | ||
name: string; | ||
proto: EggPrototype; | ||
} | ||
|
||
export class AopContextHook implements LifecycleHook<EggContextLifecycleContext, EggContext> { | ||
private readonly moduleHandler: Application['moduleHandler']; | ||
private requestProtoList: Array<ProtoToCreate> = []; | ||
|
||
constructor(moduleHandler: Application['moduleHandler']) { | ||
this.moduleHandler = moduleHandler; | ||
for (const loadUnitInstance of this.moduleHandler.loadUnitInstances) { | ||
const iterator = loadUnitInstance.loadUnit.iterateEggPrototype(); | ||
for (const proto of iterator) { | ||
const protoWithClazz = proto as EggPrototypeWithClazz; | ||
const clazz = protoWithClazz.clazz; | ||
if (!clazz) continue; | ||
const aspects = AspectInfoUtil.getAspectList(clazz); | ||
for (const aspect of aspects) { | ||
for (const advice of aspect.adviceList) { | ||
const adviceProto = PrototypeUtil.getClazzProto(advice.clazz) as EggPrototype | undefined; | ||
if (!adviceProto) { | ||
throw TeggError.create(`Aop Advice(${advice.clazz.name}) not found in loadUnits`, 'advice_not_found'); | ||
} | ||
if (adviceProto.initType === ObjectInitType.CONTEXT) { | ||
this.requestProtoList.push({ | ||
name: advice.name, | ||
proto: adviceProto, | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
async preCreate(_, ctx: EggContext): Promise<void> { | ||
// compatible with egg controller | ||
// add context aspect to ctx | ||
if (!ctx.get(ROOT_PROTO)) { | ||
for (const proto of this.requestProtoList) { | ||
ctx.addProtoToCreate(proto.name, proto.proto); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters