Skip to content

Commit db9a621

Browse files
authored
fix: verify isEggMultiInstancePrototype before proto exists (#164)
MultiInstanceProto also has proto. <!-- 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]. --> - [x] `npm test` passes - [x] tests and/or benchmarks are included - [ ] documentation is changed or added - [x] 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 375a092 commit db9a621

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

core/runtime/src/factory/EggContainerFactory.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ export class EggContainerFactory {
5454
*/
5555
static async getOrCreateEggObjectFromClazz(clazz: EggProtoImplClass, name?: EggObjectName, qualifiers?: QualifierInfo[]): Promise<EggObject> {
5656
let proto = PrototypeUtil.getClazzProto(clazz as EggProtoImplClass) as EggPrototype | undefined;
57-
if (proto) {
58-
name = name ?? proto.name;
59-
} else if (PrototypeUtil.isEggMultiInstancePrototype(clazz as EggProtoImplClass)) {
57+
if (PrototypeUtil.isEggMultiInstancePrototype(clazz as EggProtoImplClass)) {
6058
const defaultName = NameUtil.getClassName(clazz as EggProtoImplClass);
6159
name = name ?? defaultName;
6260
proto = EggPrototypeFactory.instance.getPrototype(name, undefined, qualifiers);
61+
} else if (proto) {
62+
name = name ?? proto.name;
6363
}
6464
if (!proto) {
6565
throw new Error(`can not get proto for clazz ${clazz.name}`);

core/runtime/test/LoadUnitInstance.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,16 @@ describe('test/LoadUnit/LoadUnitInstance.test.ts', () => {
8787
assert(foo2.loadUnitPath);
8888
assert(foo2.foo === 'foo2');
8989

90-
const obj = await EggContainerFactory.getOrCreateEggObjectFromClazz(FooLogger, 'foo', [{
90+
const obj1 = await EggContainerFactory.getOrCreateEggObjectFromClazz(FooLogger, 'foo', [{
91+
attribute: FOO_ATTRIBUTE,
92+
value: 'foo1',
93+
}]);
94+
const obj2 = await EggContainerFactory.getOrCreateEggObjectFromClazz(FooLogger, 'foo', [{
9195
attribute: FOO_ATTRIBUTE,
9296
value: 'foo2',
9397
}]);
94-
assert(foo2Obj === obj);
98+
assert(foo1Obj === obj1);
99+
assert(foo2Obj === obj2);
95100

96101
await TestUtil.destroyLoadUnitInstance(instance);
97102
});

plugin/tegg/app/extend/application.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ export default {
8888
};
8989
},
9090

91-
async getEggObject(clazz: EggProtoImplClass, name?: string, qualifiers?: QualifierInfo[]) {
91+
async getEggObject(clazz: EggProtoImplClass, name?: string, qualifiers?: QualifierInfo | QualifierInfo[]) {
92+
if (qualifiers) {
93+
qualifiers = Array.isArray(qualifiers) ? qualifiers : [ qualifiers ];
94+
}
9295
const eggObject = await EggContainerFactory.getOrCreateEggObjectFromClazz(clazz, name, qualifiers);
9396
return eggObject.obj;
9497
},

0 commit comments

Comments
 (0)