-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: impl MultiInstanceInfo decorator (#239)
Add multi instances info for constructor inject mode. <!-- 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 --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced the `MultiInstanceInfo` decorator for enhanced multi-instance management. - Added support for multi-instance constructor attributes and indexing in relevant classes. - Enhanced `getEggObject` method to accept additional parameters for improved flexibility. - **Bug Fixes** - Improved the construction logic for `EggObject` to handle multi-instance information. - **Tests** - Added a test case to verify the loading of multiple instances using the new constructor pattern. - **Documentation** - Updated interface definitions to include properties related to multi-instance management. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
Showing
10 changed files
with
144 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { PrototypeUtil } from '../util/PrototypeUtil'; | ||
import { QualifierAttribute } from '@eggjs/tegg-types'; | ||
|
||
export function MultiInstanceInfo(attributes: QualifierAttribute[]) { | ||
return function(target: any, _propertyKey: PropertyKey | undefined, parameterIndex: number) { | ||
PrototypeUtil.setMultiInstanceConstructorIndex(target, parameterIndex); | ||
PrototypeUtil.setMultiInstanceConstructorAttributes(target, attributes); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
core/runtime/test/fixtures/modules/multi-instance-module/MultiInstanceConstructor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { | ||
AccessLevel, | ||
ObjectInitType, | ||
QualifierValue, | ||
ObjectInfo | ||
} from '@eggjs/tegg-types'; | ||
import { Inject, MultiInstanceInfo, MultiInstanceProto, SingletonProto } from '@eggjs/core-decorator'; | ||
|
||
export const FOO_ATTRIBUTE = Symbol.for('FOO_ATTRIBUTE'); | ||
|
||
@SingletonProto() | ||
export class Bar { | ||
bar = 'bar'; | ||
} | ||
|
||
@MultiInstanceProto({ | ||
accessLevel: AccessLevel.PUBLIC, | ||
initType: ObjectInitType.SINGLETON, | ||
objects: [{ | ||
name: 'fooConstructor', | ||
qualifiers: [{ | ||
attribute: FOO_ATTRIBUTE, | ||
value: 'foo1', | ||
}], | ||
}, { | ||
name: 'fooConstructor', | ||
qualifiers: [{ | ||
attribute: FOO_ATTRIBUTE, | ||
value: 'foo2', | ||
}], | ||
}], | ||
}) | ||
export class FooLoggerConstructor { | ||
foo: QualifierValue | undefined; | ||
bar: string; | ||
|
||
constructor(@Inject() bar: Bar, @MultiInstanceInfo([ FOO_ATTRIBUTE ]) objInfo: ObjectInfo) { | ||
this.foo = objInfo.qualifiers.find(t => t.attribute === FOO_ATTRIBUTE)?.value; | ||
this.bar = bar.bar; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters