Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions core/runtime/src/factory/EggContainerFactory.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { EggPrototype } from '@eggjs/tegg-metadata';
import { EggPrototype, EggPrototypeFactory } from '@eggjs/tegg-metadata';
import { EggContainer } from '../model/EggContainer';
import { LifecycleContext } from '@eggjs/tegg-lifecycle';
import { EggObjectName, ObjectInitTypeLike, PrototypeUtil, EggProtoImplClass } from '@eggjs/core-decorator';
import {
EggObjectName,
ObjectInitTypeLike,
PrototypeUtil,
EggProtoImplClass,
QualifierInfo,
} from '@eggjs/core-decorator';
import { EggObject } from '../model/EggObject';
import { ContextHandler } from '../model/ContextHandler';
import { ContextInitiator } from '../impl/ContextInitiator';
import { NameUtil } from '@eggjs/tegg-common-util';

export type ContainerGetMethod = (proto: EggPrototype) => EggContainer<LifecycleContext>;

Expand Down Expand Up @@ -45,8 +52,15 @@ export class EggContainerFactory {
* If get singleton egg object in context,
* will create context egg object for it.
*/
static async getOrCreateEggObjectFromClazz(clazz: EggProtoImplClass, name?: EggObjectName): Promise<EggObject> {
const proto = PrototypeUtil.getClazzProto(clazz) as EggPrototype;
static async getOrCreateEggObjectFromClazz(clazz: EggProtoImplClass, name?: EggObjectName, qualifiers?: QualifierInfo[]): Promise<EggObject> {
let proto = PrototypeUtil.getClazzProto(clazz as EggProtoImplClass) as EggPrototype | undefined;
if (proto) {
name = name ?? proto.name;
} else if (PrototypeUtil.isEggMultiInstancePrototype(clazz as EggProtoImplClass)) {
const defaultName = NameUtil.getClassName(clazz as EggProtoImplClass);
name = name ?? defaultName;
proto = EggPrototypeFactory.instance.getPrototype(name, undefined, qualifiers);
}
if (!proto) {
throw new Error(`can not get proto for clazz ${clazz.name}`);
}
Expand Down
7 changes: 7 additions & 0 deletions core/runtime/test/LoadUnitInstance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ describe('test/LoadUnit/LoadUnitInstance.test.ts', () => {
assert(foo1.foo === 'foo1');
assert(foo2.loadUnitPath);
assert(foo2.foo === 'foo2');

const obj = await EggContainerFactory.getOrCreateEggObjectFromClazz(FooLogger, 'foo', [{
attribute: FOO_ATTRIBUTE,
value: 'foo2',
}]);
assert(foo2Obj === obj);

await TestUtil.destroyLoadUnitInstance(instance);
});
});
Expand Down
11 changes: 3 additions & 8 deletions plugin/tegg/app/extend/application.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
EggPrototype,
EggPrototypeCreatorFactory,
EggPrototypeFactory,
EggPrototypeLifecycleUtil,
Expand All @@ -16,7 +15,7 @@ import {
LoadUnitInstanceLifecycleUtil,
} from '@eggjs/tegg-runtime';
import { LoaderFactory } from '@eggjs/tegg-loader';
import { EggProtoImplClass, PrototypeUtil, IdenticalUtil, RuntimeConfig } from '@eggjs/tegg';
import { EggProtoImplClass, IdenticalUtil, RuntimeConfig, QualifierInfo } from '@eggjs/tegg';
import type { Application } from 'egg';

export default {
Expand Down Expand Up @@ -89,12 +88,8 @@ export default {
};
},

async getEggObject(clazz: EggProtoImplClass) {
const proto = PrototypeUtil.getClazzProto(clazz);
if (!proto) {
throw new Error(`can not get proto for clazz ${clazz.name}`);
}
const eggObject = await EggContainerFactory.getOrCreateEggObject(proto as EggPrototype);
async getEggObject(clazz: EggProtoImplClass, name?: string, qualifiers?: QualifierInfo[]) {
const eggObject = await EggContainerFactory.getOrCreateEggObjectFromClazz(clazz, name, qualifiers);
return eggObject.obj;
},
};
4 changes: 2 additions & 2 deletions plugin/tegg/app/extend/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ export default {
return this[TEGG_CONTEXT];
},

async getEggObject(this: Context, clazz: EggProtoImplClass) {
async getEggObject(this: Context, clazz: EggProtoImplClass, name?: string) {
const protoObj = PrototypeUtil.getClazzProto(clazz);
if (!protoObj) {
throw new Error(`can not get proto for clazz ${clazz.name}`);
}
const proto = protoObj as EggPrototype;
const eggObject = await this.app.eggContainerFactory.getOrCreateEggObject(proto, proto.name);
const eggObject = await this.app.eggContainerFactory.getOrCreateEggObject(proto, name ?? proto.name);
return eggObject.obj;
},
};