Skip to content

Commit

Permalink
fix: get app/ctx properties when load unit beforeCreate (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
killagu authored Feb 14, 2023
1 parent 5801061 commit 76ef679
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions plugin/tegg/lib/EggQualifierProtoHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,50 @@ import { ObjectUtils } from '@eggjs/tegg-common-util';

export class EggQualifierProtoHook implements LifecycleHook<LoadUnitLifecycleContext, LoadUnit> {
private readonly app: Application;
private readonly appProperties: string[];
private readonly ctxProperties: string[];

constructor(app: Application) {
this.app = app;
this.appProperties = ObjectUtils.getProperties(this.app);
this.ctxProperties = ObjectUtils.getProperties(this.app.context);
}

async preCreate(ctx: LoadUnitLifecycleContext): Promise<void> {
const clazzList = ctx.loader.load();
const appProperties = ObjectUtils.getProperties(this.app);
const ctxProperties = ObjectUtils.getProperties(this.app.context);
for (const clazz of clazzList) {
for (const injectObject of PrototypeUtil.getInjectObjects(clazz) || []) {
const propertyQualifiers = QualifierUtil.getProperQualifiers(clazz, injectObject.refName);
const hasEggQualifier = propertyQualifiers.find(t => t.attribute === EggQualifierAttribute);
if (hasEggQualifier) {
continue;
}
if (this.isCtxObject(injectObject.objName)) {
if (this.isCtxObject(injectObject.objName, ctxProperties)) {
QualifierUtil.addProperQualifier(clazz, injectObject.refName, EggQualifierAttribute, EggType.CONTEXT);
} else if (this.isAppObject(injectObject.objName)) {
} else if (this.isAppObject(injectObject.objName, appProperties)) {
QualifierUtil.addProperQualifier(clazz, injectObject.refName, EggQualifierAttribute, EggType.APP);
}
}
}
}

private isAppObject(name: PropertyKey) {
private isAppObject(name: PropertyKey, appProperties: string[]) {
name = String(name);
if (APP_CLAZZ_BLACK_LIST.includes(name)) {
return false;
}
if (DEFAULT_APP_CLAZZ.includes(name)) {
return true;
}
return this.appProperties.includes(name);
return appProperties.includes(name);
}

private isCtxObject(name: PropertyKey) {
private isCtxObject(name: PropertyKey, ctxProperties: string[]) {
name = String(name);
if (CONTEXT_CLAZZ_BLACK_LIST.includes(name)) {
return false;
}
if (DEFAULT_CONTEXT_CLAZZ.includes(name)) {
return true;
}
return this.ctxProperties.includes(name);
return ctxProperties.includes(name);
}
}

0 comments on commit 76ef679

Please sign in to comment.