From 3385d571b076d3148978f252188f29d9cf2c6781 Mon Sep 17 00:00:00 2001 From: killa Date: Thu, 9 Feb 2023 15:58:35 +0800 Subject: [PATCH] feat: use SingletonProto for egg ctx object (#92) * feat: use SingletonProto for egg ctx object --- README.md | 25 ++++++ core/core-decorator/index.ts | 2 + .../src/decorator/EggQualifier.ts | 11 +++ core/core-decorator/src/enum/EggType.ts | 4 + .../eventbus-runtime/src/SingletonEventBus.ts | 19 +--- plugin/eventbus/lib/EventbusLoadUnitHook.ts | 5 +- plugin/tegg/app.ts | 7 ++ plugin/tegg/lib/EggAppLoader.ts | 90 ++++++++----------- plugin/tegg/lib/EggCompatibleObject.ts | 6 +- plugin/tegg/lib/EggCompatibleProtoImpl.ts | 10 +-- plugin/tegg/lib/EggContextCompatibleHook.ts | 6 -- plugin/tegg/lib/EggQualifierProtoHook.ts | 53 +++++++++++ plugin/tegg/test/EggCompatible.test.ts | 9 ++ .../apps/background-app/app/extend/context.ts | 5 -- .../BackgroundService.ts | 9 +- .../apps/egg-app/app/extend/application.ts | 7 ++ .../apps/egg-app/app/extend/context.ts | 6 ++ .../multi-module-service/EggTypeService.ts | 34 +++++++ .../SingletonFooService.ts | 10 +-- 19 files changed, 213 insertions(+), 105 deletions(-) create mode 100644 core/core-decorator/src/decorator/EggQualifier.ts create mode 100644 core/core-decorator/src/enum/EggType.ts create mode 100644 plugin/tegg/lib/EggQualifierProtoHook.ts delete mode 100644 plugin/tegg/test/fixtures/apps/background-app/app/extend/context.ts create mode 100644 plugin/tegg/test/fixtures/apps/egg-app/app/extend/application.ts create mode 100644 plugin/tegg/test/fixtures/apps/egg-app/modules/multi-module-service/EggTypeService.ts diff --git a/README.md b/README.md index 92372c64..47b603b1 100644 --- a/README.md +++ b/README.md @@ -350,6 +350,31 @@ export class HelloService { } ``` +### egg 内 ctx/app 命名冲突 + +egg 内可能出现 ctx 和 app 上有同名对象的存在,我们可以通过使用 `EggQualifier` 来明确指定注入的对象来自 ctx 还是 app。不指定时,默认注入 app 上的对象。 + +###### 定义 + +```typescript +@EggQualifier(eggType: EggType) +``` + +###### 示例 + +```typescript +import { EggLogger } from 'egg'; +import { Inject, EggQualifier, EggType } from '@eggjs/tegg'; + +@ContextProto() +export class HelloService { + @Inject() + // 明确指定注入 ctx 上的 foo 而不是 app 上的 foo + @EggQualifier(EggType.CONTEXT) + foo: Foo; +} +``` + ### 单测 #### 单测 Context diff --git a/core/core-decorator/index.ts b/core/core-decorator/index.ts index 7ba504e6..25bd3148 100644 --- a/core/core-decorator/index.ts +++ b/core/core-decorator/index.ts @@ -4,9 +4,11 @@ export * from './src/decorator/InitTypeQualifier'; export * from './src/decorator/ModuleQualifier'; export * from './src/decorator/ContextProto'; export * from './src/decorator/SingletonProto'; +export * from './src/decorator/EggQualifier'; export * from './src/enum/AccessLevel'; export * from './src/enum/ObjectInitType'; +export * from './src/enum/EggType'; export * from './src/model/EggPrototypeInfo'; export * from './src/model/InjectObjectInfo'; diff --git a/core/core-decorator/src/decorator/EggQualifier.ts b/core/core-decorator/src/decorator/EggQualifier.ts new file mode 100644 index 00000000..6814cd53 --- /dev/null +++ b/core/core-decorator/src/decorator/EggQualifier.ts @@ -0,0 +1,11 @@ +import { QualifierUtil } from '../util/QualifierUtil'; +import { EggProtoImplClass } from '../model/EggPrototypeInfo'; +import { EggType } from '../enum/EggType'; + +export const EggQualifierAttribute = Symbol.for('Qualifier.Egg'); + +export function EggQualifier(eggType: EggType) { + return function(target: any, propertyKey: PropertyKey) { + QualifierUtil.addProperQualifier(target.constructor as EggProtoImplClass, propertyKey, EggQualifierAttribute, eggType); + }; +} diff --git a/core/core-decorator/src/enum/EggType.ts b/core/core-decorator/src/enum/EggType.ts new file mode 100644 index 00000000..95097df2 --- /dev/null +++ b/core/core-decorator/src/enum/EggType.ts @@ -0,0 +1,4 @@ +export enum EggType { + APP = 'APP', + CONTEXT = 'CONTEXT', +} diff --git a/core/eventbus-runtime/src/SingletonEventBus.ts b/core/eventbus-runtime/src/SingletonEventBus.ts index 4a7df755..cd7b9ea0 100644 --- a/core/eventbus-runtime/src/SingletonEventBus.ts +++ b/core/eventbus-runtime/src/SingletonEventBus.ts @@ -1,4 +1,4 @@ -import { AccessLevel, InitTypeQualifier, Inject, ObjectInitType, SingletonProto } from '@eggjs/core-decorator'; +import { AccessLevel, Inject, SingletonProto } from '@eggjs/core-decorator'; import { EventBus, Events, EventWaiter, EventName, CORK_ID } from '@eggjs/eventbus-decorator'; import { ContextHandler, EggContext } from '@eggjs/tegg-runtime'; import type { EggLogger } from 'egg'; @@ -42,27 +42,12 @@ export class SingletonEventBus implements EventBus, EventWaiter { @Inject({ name: 'logger', }) - @InitTypeQualifier(ObjectInitType.CONTEXT) - private readonly ctxLogger: EggLogger; - - @Inject({ - name: 'logger', - }) - @InitTypeQualifier(ObjectInitType.SINGLETON) - private readonly singletonLogger: EggLogger; + private readonly logger: EggLogger; private corkIdSequence = 0; private readonly corkedEvents = new Map(); - get logger(): EggLogger { - try { - return this.ctxLogger; - } catch (_) { - return this.singletonLogger; - } - } - /** * only use for ensure event will happen */ diff --git a/plugin/eventbus/lib/EventbusLoadUnitHook.ts b/plugin/eventbus/lib/EventbusLoadUnitHook.ts index 8ae98d3b..f030ffba 100644 --- a/plugin/eventbus/lib/EventbusLoadUnitHook.ts +++ b/plugin/eventbus/lib/EventbusLoadUnitHook.ts @@ -1,4 +1,4 @@ -import { LifecycleHook } from '@eggjs/tegg'; +import { EggQualifierAttribute, EggType, LifecycleHook, QualifierUtil } from '@eggjs/tegg'; import { EggLoadUnitType, EggPrototypeCreatorFactory, @@ -14,6 +14,9 @@ const REGISTER_CLAZZ = [ SingletonEventBus, ]; +// EggQualifier only for egg plugin +QualifierUtil.addProperQualifier(SingletonEventBus, 'logger', EggQualifierAttribute, EggType.APP); + export class EventbusLoadUnitHook implements LifecycleHook { async postCreate(_ctx: LoadUnitLifecycleContext, loadUnit: LoadUnit): Promise { if (loadUnit.type === EggLoadUnitType.APP) { diff --git a/plugin/tegg/app.ts b/plugin/tegg/app.ts index a9929435..f5114da7 100644 --- a/plugin/tegg/app.ts +++ b/plugin/tegg/app.ts @@ -7,11 +7,13 @@ import { CompatibleUtil } from './lib/CompatibleUtil'; import { ModuleHandler } from './lib/ModuleHandler'; import { EggContextHandler } from './lib/EggContextHandler'; import { hijackRunInBackground } from './lib/run_in_background'; +import { EggQualifierProtoHook } from './lib/EggQualifierProtoHook'; export default class App { private readonly app: Application; private compatibleHook?: EggContextCompatibleHook; private eggContextHandler: EggContextHandler; + private eggQualifierProtoHook: EggQualifierProtoHook; constructor(app: Application) { this.app = app; @@ -24,7 +26,9 @@ export default class App { configDidLoad() { this.eggContextHandler = new EggContextHandler(this.app); this.app.eggContextHandler = this.eggContextHandler; + this.eggQualifierProtoHook = new EggQualifierProtoHook(this.app); this.eggContextHandler.register(); + this.app.loadUnitLifecycleUtil.registerLifecycle(this.eggQualifierProtoHook); this.app.moduleHandler = new ModuleHandler(this.app); } @@ -41,5 +45,8 @@ export default class App { if (this.compatibleHook) { this.app.eggContextLifecycleUtil.deleteLifecycle(this.compatibleHook); } + if (this.eggQualifierProtoHook) { + this.app.loadUnitLifecycleUtil.deleteLifecycle(this.eggQualifierProtoHook); + } } } diff --git a/plugin/tegg/lib/EggAppLoader.ts b/plugin/tegg/lib/EggAppLoader.ts index 7282af10..8fb999b5 100644 --- a/plugin/tegg/lib/EggAppLoader.ts +++ b/plugin/tegg/lib/EggAppLoader.ts @@ -2,7 +2,11 @@ import { Application } from 'egg'; import { Loader, TeggError } from '@eggjs/tegg-metadata'; import { AccessLevel, - EggProtoImplClass, InitTypeQualifierAttribute, LoadUnitNameQualifierAttribute, + EggProtoImplClass, + EggQualifierAttribute, + EggType, + InitTypeQualifierAttribute, + LoadUnitNameQualifierAttribute, ObjectInitType, PrototypeUtil, QualifierUtil, @@ -12,9 +16,9 @@ import { COMPATIBLE_PROTO_IMPLE_TYPE } from './EggCompatibleProtoImpl'; import { BackgroundTaskHelper } from '@eggjs/tegg-background-task'; import { EggObjectFactory } from '@eggjs/tegg-dynamic-inject-runtime'; -const APP_CLAZZ_BLACK_LIST = [ 'eggObjectFactory' ]; -const DEFAULT_APP_CLAZZ = []; -const DEFAULT_CONTEXT_CLAZZ = [ +export const APP_CLAZZ_BLACK_LIST = [ 'eggObjectFactory' ]; +export const DEFAULT_APP_CLAZZ: string[] = []; +export const DEFAULT_CONTEXT_CLAZZ = [ 'user', ]; @@ -25,46 +29,41 @@ export class EggAppLoader implements Loader { this.app = app; } - private buildAppClazz(name: string): EggProtoImplClass { + private buildClazz(name: string, eggType: EggType): EggProtoImplClass { const app = this.app; - const func: EggProtoImplClass = function() { - return app[name]; - } as any; - PrototypeUtil.setIsEggPrototype(func); - PrototypeUtil.setFilePath(func, 'mock_file_path'); - PrototypeUtil.setProperty(func, { - name, - initType: ObjectInitType.SINGLETON, - accessLevel: AccessLevel.PUBLIC, - protoImplType: COMPATIBLE_PROTO_IMPLE_TYPE, - }); - QualifierUtil.addProtoQualifier(func, LoadUnitNameQualifierAttribute, 'app'); - QualifierUtil.addProtoQualifier(func, InitTypeQualifierAttribute, ObjectInitType.SINGLETON); - return func; - } - - private buildCtxClazz(name: string): EggProtoImplClass { - const temp = { - [name]: function(ctx) { + let func: EggProtoImplClass; + if (eggType === EggType.APP) { + func = function() { + return app[name]; + } as any; + } else { + func = function() { + const ctx = app.currentContext; if (!ctx) { // ctx has been destroyed, throw humanize error info throw TeggError.create(`Can not read property \`${name}\` because egg ctx has been destroyed`, 'read_after_ctx_destroyed'); } return ctx[name]; - } as any, - }; - const func = temp[name]; + } as any; + } + Object.defineProperty(func, 'name', { + value: name, + writable: false, + enumerable: false, + configurable: true, + }); PrototypeUtil.setIsEggPrototype(func); PrototypeUtil.setFilePath(func, 'mock_file_path'); PrototypeUtil.setProperty(func, { name, - initType: ObjectInitType.CONTEXT, + initType: ObjectInitType.SINGLETON, accessLevel: AccessLevel.PUBLIC, protoImplType: COMPATIBLE_PROTO_IMPLE_TYPE, }); QualifierUtil.addProtoQualifier(func, LoadUnitNameQualifierAttribute, 'app'); - QualifierUtil.addProtoQualifier(func, InitTypeQualifierAttribute, ObjectInitType.CONTEXT); + QualifierUtil.addProtoQualifier(func, InitTypeQualifierAttribute, ObjectInitType.SINGLETON); + QualifierUtil.addProtoQualifier(func, EggQualifierAttribute, eggType); return func; } @@ -73,6 +72,12 @@ export class EggAppLoader implements Loader { const func: EggProtoImplClass = function() { return app.getLogger(name); } as any; + Object.defineProperty(func, 'name', { + value: name, + writable: false, + enumerable: false, + configurable: true, + }); PrototypeUtil.setIsEggPrototype(func); PrototypeUtil.setFilePath(func, 'mock_file_path'); PrototypeUtil.setProperty(func, { @@ -83,26 +88,7 @@ export class EggAppLoader implements Loader { }); QualifierUtil.addProtoQualifier(func, LoadUnitNameQualifierAttribute, 'app'); QualifierUtil.addProtoQualifier(func, InitTypeQualifierAttribute, ObjectInitType.SINGLETON); - return func; - } - - private buildCtxLoggerClazz(name: string): EggProtoImplClass { - const temp = { - [name]: function(ctx) { - return ctx.getLogger(name); - } as any, - }; - const func = temp[name]; - PrototypeUtil.setIsEggPrototype(func); - PrototypeUtil.setFilePath(func, 'mock_file_path'); - PrototypeUtil.setProperty(func, { - name, - initType: ObjectInitType.CONTEXT, - accessLevel: AccessLevel.PUBLIC, - protoImplType: COMPATIBLE_PROTO_IMPLE_TYPE, - }); - QualifierUtil.addProtoQualifier(func, LoadUnitNameQualifierAttribute, 'app'); - QualifierUtil.addProtoQualifier(func, InitTypeQualifierAttribute, ObjectInitType.CONTEXT); + QualifierUtil.addProtoQualifier(func, EggQualifierAttribute, EggType.APP); return func; } @@ -128,16 +114,14 @@ export class EggAppLoader implements Loader { ...DEFAULT_CONTEXT_CLAZZ, ])); const loggerNames = this.getLoggerNames(allContextClazzNames); - const allSingletonClazzs = allSingletonClazzNames.map(name => this.buildAppClazz(name)); - const allContextClazzs = allContextClazzNames.map(name => this.buildCtxClazz(name)); + const allSingletonClazzs = allSingletonClazzNames.map(name => this.buildClazz(name, EggType.APP)); + const allContextClazzs = allContextClazzNames.map(name => this.buildClazz(name, EggType.CONTEXT)); const appLoggerClazzs = loggerNames.map(name => this.buildAppLoggerClazz(name)); - const ctxLoggerClazzs = loggerNames.map(name => this.buildCtxLoggerClazz(name)); return [ ...allSingletonClazzs, ...allContextClazzs, ...appLoggerClazzs, - ...ctxLoggerClazzs, // inner helper class list // TODO: should auto the inner class diff --git a/plugin/tegg/lib/EggCompatibleObject.ts b/plugin/tegg/lib/EggCompatibleObject.ts index 03044256..265b63e5 100644 --- a/plugin/tegg/lib/EggCompatibleObject.ts +++ b/plugin/tegg/lib/EggCompatibleObject.ts @@ -1,6 +1,5 @@ import { EggCompatibleProtoImpl } from './EggCompatibleProtoImpl'; import { - ContextHandler, EggObject, EggObjectFactory, } from '@eggjs/tegg-runtime'; @@ -19,8 +18,7 @@ export class EggCompatibleObject implements EggObject { constructor(name: EggObjectName, proto: EggCompatibleProtoImpl) { this.proto = proto; this.name = name; - const ctx = ContextHandler.getContext(); - this.id = IdenticalUtil.createObjectId(this.proto.id, ctx?.id); + this.id = IdenticalUtil.createObjectId(this.proto.id); } // If the egg object is a getter, @@ -28,7 +26,7 @@ export class EggCompatibleObject implements EggObject { // So access egg object lazy. get obj() { if (!this[OBJ]) { - this[OBJ] = this.proto.constructorEggCompatibleObject(); + this[OBJ] = this.proto.constructEggObject(); } return this[OBJ]; } diff --git a/plugin/tegg/lib/EggCompatibleProtoImpl.ts b/plugin/tegg/lib/EggCompatibleProtoImpl.ts index 06dedab5..8e36a685 100644 --- a/plugin/tegg/lib/EggCompatibleProtoImpl.ts +++ b/plugin/tegg/lib/EggCompatibleProtoImpl.ts @@ -10,13 +10,11 @@ import { Id, IdenticalUtil, } from '@eggjs/tegg'; -import { ContextHandler } from '@eggjs/tegg-runtime'; import { EggPrototype, InjectObjectProto, EggPrototypeLifecycleContext, } from '@eggjs/tegg-metadata'; -import { EGG_CONTEXT } from '@eggjs/egg-module-common'; export const COMPATIBLE_PROTO_IMPLE_TYPE = 'EGG_COMPATIBLE'; @@ -65,13 +63,7 @@ export class EggCompatibleProtoImpl implements EggPrototype { } constructEggObject(): object { - return {}; - } - - constructorEggCompatibleObject() { - const teggContext = ContextHandler.getContext(); - const ctx = teggContext?.get(EGG_CONTEXT); - return Reflect.apply(this.clazz, null, [ ctx ]); + return Reflect.apply(this.clazz, null, []); } getMetaData(metadataKey: MetaDataKey): T | undefined { diff --git a/plugin/tegg/lib/EggContextCompatibleHook.ts b/plugin/tegg/lib/EggContextCompatibleHook.ts index 402b87e8..20904674 100644 --- a/plugin/tegg/lib/EggContextCompatibleHook.ts +++ b/plugin/tegg/lib/EggContextCompatibleHook.ts @@ -3,7 +3,6 @@ import { EggContainerFactory, EggContext, EggContextLifecycleContext } from '@eg import { EggPrototype } from '@eggjs/tegg-metadata'; import { ModuleHandler } from './ModuleHandler'; import { ROOT_PROTO } from '@eggjs/egg-module-common'; -import { EggCompatibleProtoImpl } from './EggCompatibleProtoImpl'; export class EggContextCompatibleHook implements LifecycleHook { private readonly moduleHandler: ModuleHandler; @@ -15,11 +14,6 @@ export class EggContextCompatibleHook implements LifecycleHook { + private readonly app: Application; + + constructor(app: Application) { + this.app = app; + } + + async preCreate(ctx: LoadUnitLifecycleContext): Promise { + const clazzList = ctx.loader.load(); + 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)) { + QualifierUtil.addProperQualifier(clazz, injectObject.refName, EggQualifierAttribute, EggType.CONTEXT); + } else if (this.isAppObject(injectObject.objName)) { + QualifierUtil.addProperQualifier(clazz, injectObject.refName, EggQualifierAttribute, EggType.APP); + } + } + } + } + + private isAppObject(name: PropertyKey) { + if (APP_CLAZZ_BLACK_LIST.includes(String(name))) { + return false; + } + if (DEFAULT_APP_CLAZZ.includes(String(name))) { + return true; + } + return this.app.hasOwnProperty(name); + } + + private isCtxObject(name: PropertyKey) { + if (DEFAULT_CONTEXT_CLAZZ.includes(String(name))) { + return true; + } + return this.app.context.hasOwnProperty(name); + } +} diff --git a/plugin/tegg/test/EggCompatible.test.ts b/plugin/tegg/test/EggCompatible.test.ts index ba251db5..8c2f414a 100644 --- a/plugin/tegg/test/EggCompatible.test.ts +++ b/plugin/tegg/test/EggCompatible.test.ts @@ -1,6 +1,7 @@ import mm from 'egg-mock'; import assert from 'assert'; import path from 'path'; +import EggTypeService from './fixtures/apps/egg-app/modules/multi-module-service/EggTypeService'; describe('test/EggCompatible.test.ts', () => { let app; @@ -126,4 +127,12 @@ describe('test/EggCompatible.test.ts', () => { assert(ctx.counter === 1); }); }); + + it('should support EggQualifier', async () => { + await app.mockModuleContextScope(async () => { + const eggTypeService = await app.getEggObject(EggTypeService); + const result = eggTypeService.testInject(); + assert.deepStrictEqual(result, { app: { from: 'app' }, ctx: { from: 'ctx' } }); + }); + }); }); diff --git a/plugin/tegg/test/fixtures/apps/background-app/app/extend/context.ts b/plugin/tegg/test/fixtures/apps/background-app/app/extend/context.ts deleted file mode 100644 index d7abb6f6..00000000 --- a/plugin/tegg/test/fixtures/apps/background-app/app/extend/context.ts +++ /dev/null @@ -1,5 +0,0 @@ -export default { - testObj: { - ok: true, - }, -}; diff --git a/plugin/tegg/test/fixtures/apps/background-app/modules/multi-module-background/BackgroundService.ts b/plugin/tegg/test/fixtures/apps/background-app/modules/multi-module-background/BackgroundService.ts index 52b8f4e2..984a35d6 100644 --- a/plugin/tegg/test/fixtures/apps/background-app/modules/multi-module-background/BackgroundService.ts +++ b/plugin/tegg/test/fixtures/apps/background-app/modules/multi-module-background/BackgroundService.ts @@ -1,9 +1,14 @@ -import { AccessLevel, SingletonProto, Inject } from '@eggjs/tegg'; +import { AccessLevel, SingletonProto, Inject, ContextProto } from '@eggjs/tegg'; import { BackgroundTaskHelper } from '@eggjs/tegg-background-task'; import { CountService } from './CountService'; import sleep from 'mz-modules/sleep'; import assert from 'assert'; +@ContextProto() +export class TestObj { + ok = true; +} + @SingletonProto({ accessLevel: AccessLevel.PUBLIC, }) @@ -12,7 +17,7 @@ export default class BackgroundService { private readonly backgroundTaskHelper:BackgroundTaskHelper; @Inject() - testObj: any; + testObj: TestObj; @Inject() private readonly countService: CountService; diff --git a/plugin/tegg/test/fixtures/apps/egg-app/app/extend/application.ts b/plugin/tegg/test/fixtures/apps/egg-app/app/extend/application.ts new file mode 100644 index 00000000..a77e8354 --- /dev/null +++ b/plugin/tegg/test/fixtures/apps/egg-app/app/extend/application.ts @@ -0,0 +1,7 @@ +export default { + get appDefineObject() { + return { + from: 'app', + }; + }, +}; diff --git a/plugin/tegg/test/fixtures/apps/egg-app/app/extend/context.ts b/plugin/tegg/test/fixtures/apps/egg-app/app/extend/context.ts index 934ed2a2..a794e5dd 100644 --- a/plugin/tegg/test/fixtures/apps/egg-app/app/extend/context.ts +++ b/plugin/tegg/test/fixtures/apps/egg-app/app/extend/context.ts @@ -11,4 +11,10 @@ export default { get user() { return {}; }, + + get appDefineObject() { + return { + from: 'ctx', + }; + }, }; diff --git a/plugin/tegg/test/fixtures/apps/egg-app/modules/multi-module-service/EggTypeService.ts b/plugin/tegg/test/fixtures/apps/egg-app/modules/multi-module-service/EggTypeService.ts new file mode 100644 index 00000000..2921b15b --- /dev/null +++ b/plugin/tegg/test/fixtures/apps/egg-app/modules/multi-module-service/EggTypeService.ts @@ -0,0 +1,34 @@ +import { AccessLevel, EggQualifier, EggType, Inject, SingletonProto } from '@eggjs/tegg'; +import { EggLogger } from 'egg-logger'; + +interface AppDefObj { + from: string; +} + +@SingletonProto({ + accessLevel: AccessLevel.PUBLIC, +}) +export default class EggTypeService { + @Inject({ + name: 'appDefineObject', + }) + @EggQualifier(EggType.APP) + appAppDefineObject: AppDefObj; + + @Inject({ + name: 'appDefineObject', + }) + @EggQualifier(EggType.CONTEXT) + ctxAppDefineObject: AppDefObj; + + @Inject() + @EggQualifier(EggType.APP) + logger: EggLogger; + + testInject() { + return { + app: this.appAppDefineObject, + ctx: this.ctxAppDefineObject, + }; + } +} diff --git a/plugin/tegg/test/fixtures/apps/egg-app/modules/multi-module-service/SingletonFooService.ts b/plugin/tegg/test/fixtures/apps/egg-app/modules/multi-module-service/SingletonFooService.ts index 84b32ddb..751e6bea 100644 --- a/plugin/tegg/test/fixtures/apps/egg-app/modules/multi-module-service/SingletonFooService.ts +++ b/plugin/tegg/test/fixtures/apps/egg-app/modules/multi-module-service/SingletonFooService.ts @@ -1,10 +1,4 @@ -import { - AccessLevel, - InitTypeQualifier, - Inject, - ObjectInitType, - SingletonProto, -} from '@eggjs/tegg'; +import { AccessLevel, EggQualifier, EggType, Inject, SingletonProto } from '@eggjs/tegg'; import { EggLogger } from 'egg-logger'; @SingletonProto({ @@ -12,7 +6,7 @@ import { EggLogger } from 'egg-logger'; }) export default class SingletonFooService { @Inject() - @InitTypeQualifier(ObjectInitType.SINGLETON) + @EggQualifier(EggType.APP) logger: EggLogger; async printLog(): Promise {