Skip to content

Commit

Permalink
fix: inject class when use component by import string (#996)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurten authored Apr 17, 2021
1 parent bfd8232 commit 8bfda7d
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 19 deletions.
32 changes: 16 additions & 16 deletions packages/core/src/context/midwayContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,6 @@ export class MidwayContainer
loadDirKey = loadDirs.join('-');
}

if (MidwayContainer.parentDefinitionMetadata.has(loadDirKey)) {
this.restoreDefinitions(
MidwayContainer.parentDefinitionMetadata.get(loadDirKey)
);
} else {
this.loadDirectory(opts);
// 保存元信息最新的上下文中,供其他容器复用,减少重复扫描
MidwayContainer.parentDefinitionMetadata.set(
loadDirKey,
this.getDefinitionMetaList()
);
}

this.debugLogger('main:main configuration register import objects');
this.registerImportObjects(configuration.getImportObjects());

// load configuration
for (const [packageName, containerConfiguration] of this.configurationMap) {
// 老版本 configuration 才加载
Expand All @@ -193,6 +177,22 @@ export class MidwayContainer
}
}

if (MidwayContainer.parentDefinitionMetadata.has(loadDirKey)) {
this.restoreDefinitions(
MidwayContainer.parentDefinitionMetadata.get(loadDirKey)
);
} else {
this.loadDirectory(opts);
// 保存元信息最新的上下文中,供其他容器复用,减少重复扫描
MidwayContainer.parentDefinitionMetadata.set(
loadDirKey,
this.getDefinitionMetaList()
);
}

this.debugLogger('main:main configuration register import objects');
this.registerImportObjects(configuration.getImportObjects());

// register base config hook
this.registerDataHandler(CONFIG_KEY, (key: string) => {
if (key === ALL) {
Expand Down
13 changes: 13 additions & 0 deletions packages/core/test/baseFramework.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ describe('/test/baseFramework.test.ts', () => {
"name": "伊卡狛格"
}
]);
await framework.stop();
});

it('should create logger and match property between framework and app', async () => {
Expand Down Expand Up @@ -696,6 +697,18 @@ describe('/test/baseFramework.test.ts', () => {
const userController = await appCtx.getAsync('userController');
const books = await (userController as any).getBooksByUser();
expect(books).toEqual([
{
"name": "无限可能str",
"isbn": "9787115549440str"
},
{
"name": "明智的孩子str",
"isbn": "9787305236525str"
},
{
"name": "伊卡狛格str",
"isbn": "9787020166916str"
},
{
"isbn": "9787115549440",
"name": "无限可能"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "book-componentstr",
"main": "src/index.ts"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// src/service/bookService
import { Provide, ScopeEnum } from '@midwayjs/decorator';
import { providerWrapper, IMidwayContainer } from '../../../../../src';

@Provide()
export class BookServiceOne {

async getAllBooks() {
return [
{
"name": "无限可能str",
"isbn": "9787115549440str"
},
{
"name": "明智的孩子str",
"isbn": "9787305236525str"
},
{
"name": "伊卡狛格str",
"isbn": "9787020166916str"
}
]
}
}

export async function dynamicCacheServiceHandler(container: IMidwayContainer) {
return () => {
return 'abcstr';
}
}

providerWrapper([
{
id: 'dynamicCacheServicestr',
provider: dynamicCacheServiceHandler,
scope: ScopeEnum.Singleton,
}
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Configuration } from '@midwayjs/decorator';

@Configuration({
namespace: 'bookstr'
})
export class BookConfigurationStr {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { BookConfigurationStr as Configuration } from './configuration';
export { BookServiceOne, dynamicCacheServiceHandler } from './bookService';
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Configuration } from '@midwayjs/decorator';
import { ILifeCycle } from "../../../../../src";
import * as bookComponent from '../../book';
import * as path from 'path';

@Configuration({
imports: [
bookComponent
bookComponent,
path.resolve(path.join(__dirname, '../../bookstr'))
],
})
export class AutoConfiguration implements ILifeCycle {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import { Provide, Controller, Inject, Get } from '@midwayjs/decorator';
import { BookService, dynamicCacheServiceHandler } from '../../../book';
import { BookServiceOne } from '../../../bookstr/src/bookService';

@Provide()
@Controller('/user')
export class UserController {
@Inject()
bookService: BookService;

@Inject()
bookServiceOne: BookServiceOne;

@Inject('book:dynamicCacheService')
cacheService: typeof dynamicCacheServiceHandler;

@Get('/list_books')
async getBooksByUser() {
console.log(this.cacheService);
return this.bookService.getAllBooks();
if (!this.cacheService) {
throw new Error('cache service is undefined');
}
const books = await this.bookServiceOne.getAllBooks();
return books.concat(await this.bookService.getAllBooks());
}
}

0 comments on commit 8bfda7d

Please sign in to comment.