Skip to content

Commit 95843c7

Browse files
authored
feat: impl getObjectFromName (#167)
<!-- Thank you for your pull request. Please review below requirements. Bug fixes and new features should include tests and possibly benchmarks. Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md 感谢您贡献代码。请确认下列 checklist 的完成情况。 Bug 修复和新功能必须包含测试,必要时请附上性能测试。 Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md --> ##### Checklist <!-- Remove items that do not apply. For completed items, change [ ] to [x]. --> - [ ] `npm test` passes - [ ] tests and/or benchmarks are included - [ ] documentation is changed or added - [ ] commit message follows commit guidelines ##### Affected core subsystem(s) <!-- Provide affected core subsystem(s). --> ##### Description of change <!-- Provide a description of the change below this comment. --> <!-- - any feature? - close https://github.com/eggjs/egg/ISSUE_URL -->
1 parent a013a51 commit 95843c7

File tree

7 files changed

+57
-4
lines changed

7 files changed

+57
-4
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,17 +632,23 @@ export interface Application {
632632
export interface Application {
633633
/**
634634
* 通过一个类来获取实例
635-
* 注:app.getEggObject 只能获取 Singleton 实例
636635
*/
637636
getEggObject<T> (clazz: EggProtoImplClass<T> ): Promise <T>;
637+
/**
638+
* 通过对象名称来获取实例
639+
*/
640+
getEggObjectFromName<T>(name: string, qualifiers?: QualifierInfo | QualifierInfo[]): Promise<unknown>;
638641
}
639642

640643
export interface Context {
641644
/**
642645
* 通过一个类来获取实例
643-
* 注:ctx.getEggObject 可以获取 Singleton/Context 实例
644646
*/
645647
getEggObject<T> (clazz: EggProtoImplClass<T> ): Promise <T>;
648+
/**
649+
* 通过对象名称来获取实例
650+
*/
651+
getEggObjectFromName<T>(name: string, qualifiers?: QualifierInfo | QualifierInfo[]): Promise<unknown>;
646652
}
647653
```
648654

core/runtime/src/factory/EggContainerFactory.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ export class EggContainerFactory {
6767
return await this.getOrCreateEggObject(proto, name);
6868
}
6969

70+
/**
71+
* get or create egg object from the Name
72+
* If get singleton egg object in context,
73+
* will create context egg object for it.
74+
*/
75+
static async getOrCreateEggObjectFromName(name: EggObjectName, qualifiers?: QualifierInfo[]): Promise<EggObject> {
76+
const proto = EggPrototypeFactory.instance.getPrototype(name, undefined, qualifiers);
77+
if (!proto) {
78+
throw new Error(`can not get proto for clazz ${String(name)}`);
79+
}
80+
return await this.getOrCreateEggObject(proto, name);
81+
}
82+
7083
static getEggObject(proto: EggPrototype, name?: EggObjectName): EggObject {
7184
const container = this.getContainer(proto);
7285
name = name || proto.name;

core/runtime/test/EggObject.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ describe('test/EggObject.test.ts', () => {
7474
assert.equal(barObj2, barObj);
7575
assert.equal(barObj2.obj, barObj.obj);
7676

77+
// get obj from name
78+
const barObj3 = await EggContainerFactory.getOrCreateEggObjectFromName('bar');
79+
assert.equal(barObj3, barObj);
80+
assert.equal(barObj3.obj, barObj.obj);
81+
7782
await TestUtil.destroyLoadUnitInstance(instance);
7883
const called = bar.getLifecycleCalled();
7984
assert.deepStrictEqual(called, [

plugin/tegg/app/extend/application.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,12 @@ export default {
9595
const eggObject = await EggContainerFactory.getOrCreateEggObjectFromClazz(clazz, name, qualifiers);
9696
return eggObject.obj;
9797
},
98+
99+
async getEggObjectFromName(name: string, qualifiers?: QualifierInfo | QualifierInfo[]) {
100+
if (qualifiers) {
101+
qualifiers = Array.isArray(qualifiers) ? qualifiers : [ qualifiers ];
102+
}
103+
const eggObject = await EggContainerFactory.getOrCreateEggObjectFromName(name, qualifiers);
104+
return eggObject.obj;
105+
},
98106
};

plugin/tegg/app/extend/context.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Context } from 'egg';
22
import { EggContext } from '@eggjs/tegg-runtime';
33
import { TEGG_CONTEXT } from '@eggjs/egg-module-common';
44
import ctxLifecycleMiddleware from '../../lib/ctx_lifecycle_middleware';
5-
import { EggProtoImplClass, PrototypeUtil } from '@eggjs/tegg';
5+
import { EggProtoImplClass, PrototypeUtil, QualifierInfo } from '@eggjs/tegg';
66
import { EggPrototype } from '@eggjs/tegg-metadata';
77

88
export interface TEggPluginContext extends Context {
@@ -30,4 +30,12 @@ export default {
3030
const eggObject = await this.app.eggContainerFactory.getOrCreateEggObject(proto, name ?? proto.name);
3131
return eggObject.obj;
3232
},
33+
34+
async getEggObjectFromName(this: Context, name: string, qualifiers?: QualifierInfo | QualifierInfo[]) {
35+
if (qualifiers) {
36+
qualifiers = Array.isArray(qualifiers) ? qualifiers : [ qualifiers ];
37+
}
38+
const eggObject = await this.app.eggContainerFactory.getOrCreateEggObjectFromName(name, qualifiers);
39+
return eggObject.obj;
40+
},
3341
};

plugin/tegg/test/app/extend/context.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ describe('test/app/extend/context.test.ts', () => {
4242
});
4343
});
4444

45+
describe('getEggObjectFromName', () => {
46+
it('should work', async () => {
47+
await app.mockModuleContextScope(async ctx => {
48+
const appService = await ctx.getEggObjectFromName('appService');
49+
assert(appService instanceof AppService);
50+
51+
const persistenceService = await ctx.getEggObjectFromName('persistenceService');
52+
assert(persistenceService instanceof PersistenceService);
53+
});
54+
});
55+
});
56+
4557
describe('beginModuleScope', () => {
4658
it('should be reentrant', async () => {
4759
await app.mockModuleContextScope(async ctx => {

plugin/tegg/typings/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
EggContext,
2020
} from '@eggjs/tegg-runtime';
2121
import { LoaderFactory } from '@eggjs/tegg-loader';
22-
import { IdenticalUtil, EggProtoImplClass } from '@eggjs/tegg';
22+
import { IdenticalUtil, EggProtoImplClass, QualifierInfo } from '@eggjs/tegg';
2323
import { ModuleHandler } from '../lib/ModuleHandler';
2424
import { EggContextHandler } from '../lib/EggContextHandler';
2525

@@ -60,6 +60,7 @@ declare module 'egg' {
6060
module: EggModule & EggApplicationModule;
6161

6262
getEggObject<T>(clazz: EggProtoImplClass<T>): Promise<T>;
63+
getEggObjectFromName<T>(name: string, qualifiers?: QualifierInfo | QualifierInfo[]): Promise<unknown>;
6364
}
6465

6566
export interface TEggContext {

0 commit comments

Comments
 (0)