From e14bdb257eaebc0b0a4c37c6073a5c3237718718 Mon Sep 17 00:00:00 2001 From: killa Date: Thu, 9 Feb 2023 15:58:07 +0800 Subject: [PATCH] feat: remove context egg object factory (#93) --- core/dynamic-inject-runtime/index.ts | 3 +- .../src/AbstractEggObjectFactory.ts | 10 ------- ...onObjectFactory.ts => EggObjectFactory.ts} | 7 ++--- .../src/EggObjectFactoryObject.ts | 15 ++++------ .../src/impl/EggContextObjectFactory.ts | 28 ------------------- plugin/tegg/lib/EggAppLoader.ts | 5 ++-- 6 files changed, 12 insertions(+), 56 deletions(-) delete mode 100644 core/dynamic-inject-runtime/src/AbstractEggObjectFactory.ts rename core/dynamic-inject-runtime/src/{impl/EggSingletonObjectFactory.ts => EggObjectFactory.ts} (75%) delete mode 100644 core/dynamic-inject-runtime/src/impl/EggContextObjectFactory.ts diff --git a/core/dynamic-inject-runtime/index.ts b/core/dynamic-inject-runtime/index.ts index 8076c48a..f8f69293 100644 --- a/core/dynamic-inject-runtime/index.ts +++ b/core/dynamic-inject-runtime/index.ts @@ -1,5 +1,4 @@ -export * from './src/impl/EggContextObjectFactory'; -export * from './src/impl/EggSingletonObjectFactory'; +export * from './src/EggObjectFactory'; import './src/EggObjectFactoryPrototype'; import './src/EggObjectFactoryObject'; diff --git a/core/dynamic-inject-runtime/src/AbstractEggObjectFactory.ts b/core/dynamic-inject-runtime/src/AbstractEggObjectFactory.ts deleted file mode 100644 index c8d563e8..00000000 --- a/core/dynamic-inject-runtime/src/AbstractEggObjectFactory.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { EggAbstractClazz, EggObjectFactory } from '@eggjs/tegg-dynamic-inject'; -import type { EggContainerFactory, EggContext } from '@eggjs/tegg-runtime'; -import { QualifierValue } from '@eggjs/core-decorator'; - -export abstract class AbstractEggObjectFactory implements EggObjectFactory { - eggContext?: EggContext; - eggContainerFactory: typeof EggContainerFactory; - - abstract getEggObject(abstractClazz: EggAbstractClazz, qualifierValue: QualifierValue): Promise; -} diff --git a/core/dynamic-inject-runtime/src/impl/EggSingletonObjectFactory.ts b/core/dynamic-inject-runtime/src/EggObjectFactory.ts similarity index 75% rename from core/dynamic-inject-runtime/src/impl/EggSingletonObjectFactory.ts rename to core/dynamic-inject-runtime/src/EggObjectFactory.ts index 30463945..136a7d7d 100644 --- a/core/dynamic-inject-runtime/src/impl/EggSingletonObjectFactory.ts +++ b/core/dynamic-inject-runtime/src/EggObjectFactory.ts @@ -1,15 +1,14 @@ import type { EggContainerFactory } from '@eggjs/tegg-runtime'; -import { EggAbstractClazz, QualifierImplUtil } from '@eggjs/tegg-dynamic-inject'; +import { EggAbstractClazz, EggObjectFactory as IEggObjectFactory, QualifierImplUtil } from '@eggjs/tegg-dynamic-inject'; import { AccessLevel, PrototypeUtil, QualifierValue, SingletonProto } from '@eggjs/core-decorator'; -import { AbstractEggObjectFactory } from '../AbstractEggObjectFactory'; -import { EGG_OBJECT_FACTORY_PROTO_IMPLE_TYPE } from '../EggObjectFactoryPrototype'; +import { EGG_OBJECT_FACTORY_PROTO_IMPLE_TYPE } from './EggObjectFactoryPrototype'; @SingletonProto({ protoImplType: EGG_OBJECT_FACTORY_PROTO_IMPLE_TYPE, name: 'eggObjectFactory', accessLevel: AccessLevel.PUBLIC, }) -export class EggSingletonObjectFactory extends AbstractEggObjectFactory { +export class EggObjectFactory implements IEggObjectFactory { eggContainerFactory: typeof EggContainerFactory; async getEggObject(abstractClazz: EggAbstractClazz, qualifierValue: QualifierValue) { diff --git a/core/dynamic-inject-runtime/src/EggObjectFactoryObject.ts b/core/dynamic-inject-runtime/src/EggObjectFactoryObject.ts index 89ec9f46..53535046 100644 --- a/core/dynamic-inject-runtime/src/EggObjectFactoryObject.ts +++ b/core/dynamic-inject-runtime/src/EggObjectFactoryObject.ts @@ -2,14 +2,13 @@ import { EggContainerFactory, EggContext, EggObject, - EggObjectLifeCycleContext, EggObjectFactory as TEggObjectFactory, } from '@eggjs/tegg-runtime'; import { EggObjectFactoryPrototype } from './EggObjectFactoryPrototype'; import { EggObjectName } from '@eggjs/core-decorator'; import { IdenticalUtil } from '@eggjs/tegg-lifecycle'; import { EggPrototype } from '@eggjs/tegg-metadata'; -import { AbstractEggObjectFactory } from './AbstractEggObjectFactory'; +import { EggObjectFactory } from './EggObjectFactory'; const OBJ = Symbol('EggObjectFactoryObject#obj'); @@ -18,26 +17,24 @@ export class EggObjectFactoryObject implements EggObject { readonly name: EggObjectName; readonly ctx?: EggContext; readonly id: string; - private [OBJ]: AbstractEggObjectFactory; + private [OBJ]: EggObjectFactory; - constructor(name: EggObjectName, proto: EggObjectFactoryPrototype, ctx?: EggContext) { + constructor(name: EggObjectName, proto: EggObjectFactoryPrototype) { this.proto = proto; this.name = name; - this.ctx = ctx; this.id = IdenticalUtil.createObjectId(this.proto.id, this.ctx?.id); } get obj() { if (!this[OBJ]) { - this[OBJ] = this.proto.constructEggObject() as AbstractEggObjectFactory; + this[OBJ] = this.proto.constructEggObject() as EggObjectFactory; this[OBJ].eggContainerFactory = EggContainerFactory; - this[OBJ].eggContext = this.ctx; } return this[OBJ]; } - static async createObject(name: EggObjectName, proto: EggPrototype, _: EggObjectLifeCycleContext, ctx?: EggContext): Promise { - return new EggObjectFactoryObject(name, proto as EggObjectFactoryPrototype, ctx); + static async createObject(name: EggObjectName, proto: EggPrototype): Promise { + return new EggObjectFactoryObject(name, proto as EggObjectFactoryPrototype); } readonly isReady: true; diff --git a/core/dynamic-inject-runtime/src/impl/EggContextObjectFactory.ts b/core/dynamic-inject-runtime/src/impl/EggContextObjectFactory.ts deleted file mode 100644 index 2e686c63..00000000 --- a/core/dynamic-inject-runtime/src/impl/EggContextObjectFactory.ts +++ /dev/null @@ -1,28 +0,0 @@ -import type { EggContainerFactory, EggContext } from '@eggjs/tegg-runtime'; -import { EggAbstractClazz, QualifierImplUtil } from '@eggjs/tegg-dynamic-inject'; -import { EGG_OBJECT_FACTORY_PROTO_IMPLE_TYPE } from '../EggObjectFactoryPrototype'; -import { AccessLevel, ContextProto, PrototypeUtil, QualifierValue } from '@eggjs/core-decorator'; -import { AbstractEggObjectFactory } from '../AbstractEggObjectFactory'; - -@ContextProto({ - protoImplType: EGG_OBJECT_FACTORY_PROTO_IMPLE_TYPE, - name: 'eggObjectFactory', - accessLevel: AccessLevel.PUBLIC, -}) -export class EggContextObjectFactory extends AbstractEggObjectFactory { - eggContext?: EggContext; - eggContainerFactory: typeof EggContainerFactory; - - async getEggObject(abstractClazz: EggAbstractClazz, qualifierValue: QualifierValue) { - const implClazz = QualifierImplUtil.getQualifierImp(abstractClazz, qualifierValue); - if (!implClazz) { - throw new Error(`has no impl for ${abstractClazz.name} with qualifier ${qualifierValue}`); - } - const protoObj: any = PrototypeUtil.getClazzProto(implClazz); - if (!protoObj) { - throw new Error(`can not get proto for clazz ${implClazz.name}`); - } - const eggObject = await this.eggContainerFactory.getOrCreateEggObject(protoObj, protoObj.name); - return eggObject.obj as T; - } -} diff --git a/plugin/tegg/lib/EggAppLoader.ts b/plugin/tegg/lib/EggAppLoader.ts index 1de19ddf..7282af10 100644 --- a/plugin/tegg/lib/EggAppLoader.ts +++ b/plugin/tegg/lib/EggAppLoader.ts @@ -10,7 +10,7 @@ import { import { ObjectUtils } from '@eggjs/tegg-common-util'; import { COMPATIBLE_PROTO_IMPLE_TYPE } from './EggCompatibleProtoImpl'; import { BackgroundTaskHelper } from '@eggjs/tegg-background-task'; -import { EggContextObjectFactory, EggSingletonObjectFactory } from '@eggjs/tegg-dynamic-inject-runtime'; +import { EggObjectFactory } from '@eggjs/tegg-dynamic-inject-runtime'; const APP_CLAZZ_BLACK_LIST = [ 'eggObjectFactory' ]; const DEFAULT_APP_CLAZZ = []; @@ -142,8 +142,7 @@ export class EggAppLoader implements Loader { // inner helper class list // TODO: should auto the inner class BackgroundTaskHelper, - EggContextObjectFactory, - EggSingletonObjectFactory, + EggObjectFactory, ]; } }