Skip to content

Commit

Permalink
fix: configuration with ctx
Browse files Browse the repository at this point in the history
* fix circular bug

* feat: lifecycle onReady add container

* fix bug

* fix bug

* add test case node module

* fix case bug
  • Loading branch information
kurten committed Mar 27, 2020
1 parent 3d9a0c8 commit 4c7ff6a
Show file tree
Hide file tree
Showing 25 changed files with 185 additions and 7 deletions.
4 changes: 4 additions & 0 deletions packages/midway-core/src/context/applicationContext.ts
Expand Up @@ -105,6 +105,7 @@ export class BaseApplicationContext implements IApplicationContext, IObjectFacto
baseDir: string = null;
parent: IApplicationContext = null;
messageSource: IMessageSource = null;
disableClassConflict = false;

constructor(baseDir = '', parent?: IApplicationContext) {
this.parent = parent;
Expand Down Expand Up @@ -224,6 +225,9 @@ export class BaseApplicationContext implements IApplicationContext, IObjectFacto
* @param {IObjectDefinition} definition
*/
registerDefinition(identifier: ObjectIdentifier, definition: IObjectDefinition) {
// if (!this.disableClassConflict && this.registry.hasDefinition(identifier)) {
// throw new Error(`${identifier} is exist!`);
// }
this.registry.registerDefinition(identifier, definition);
this.createObjectDependencyTree(identifier, definition);
}
Expand Down
10 changes: 9 additions & 1 deletion packages/midway-core/src/context/configuration.ts
Expand Up @@ -117,7 +117,7 @@ export class ContainerConfiguration implements IContainerConfiguration {
);
debug('configuration export %j.', configurationOptions);
if (configurationOptions) {
if (this.namespace !== MAIN_MODULE_KEY && configurationOptions.namespace) {
if (this.namespace !== MAIN_MODULE_KEY && configurationOptions.namespace !== undefined) {
this.namespace = configurationOptions.namespace;
}

Expand All @@ -134,6 +134,14 @@ export class ContainerConfiguration implements IContainerConfiguration {
this.bindConfigurationClass(configurationExport);
}
}
} else {
if (this.container.containsConfiguration(this.namespace)) {
debug(`configuration ${this.namespace} exist than ignore.`);
return;
} else {
debug(`configuration ${this.namespace} not exist than add.`);
this.container.addConfiguration(this);
}
}
}
/**
Expand Down
5 changes: 4 additions & 1 deletion packages/midway-core/src/context/midwayContainer.ts
Expand Up @@ -82,6 +82,10 @@ export class MidwayContainer extends Container implements IMidwayContainer {
pattern?: string | string[];
ignore?: string | string[];
}) {
// 添加全局白名单
this.midwayIdentifiers.push(PIPELINE_IDENTIFIER);
this.midwayIdentifiers.push('ctx');

// create main module configuration
const configuration = this.createConfiguration();
configuration.namespace = MAIN_MODULE_KEY;
Expand Down Expand Up @@ -404,7 +408,6 @@ export class MidwayContainer extends Container implements IMidwayContainer {
loadDefinitions() {
// 默认加载 pipeline
this.bindModule(pipelineFactory);
this.midwayIdentifiers.push(PIPELINE_IDENTIFIER);
}

private async loadAndReadyLifeCycles() {
Expand Down
3 changes: 2 additions & 1 deletion packages/midway-core/src/features/index.ts
Expand Up @@ -2,5 +2,6 @@ export {
IPipelineContext,
IPipelineHandler,
IPipelineOptions,
IPipelineResult
IPipelineResult,
IValveHandler
} from './pipeline';
1 change: 1 addition & 0 deletions packages/midway-core/src/interface.ts
Expand Up @@ -133,6 +133,7 @@ export interface IResource {
* IoC上下文抽象
*/
export interface IApplicationContext extends IObjectFactory {
disableClassConflict: boolean;
baseDir: string;
parent: IApplicationContext;
props: IProperties;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -9,7 +9,9 @@ import { Configuration } from '@midwayjs/decorator';
'../../midway-plugin-mock/src',
'../../midway-plugin-ok/src',
'midway-plugin-mod',
'@midwayjs/midway-plugin-atmod'
'@midwayjs/midway-plugin-atmod',
'@midwayjs/midway-plugin-btmod',
'@midwayjs/midway-plugin-btmod'
],
})
export class AutoConfiguraion {}
Expand Up @@ -5,7 +5,14 @@ export class UserManager {
@Inject('@midwayjs/midway-plugin-atmod:articleManager')
articleManager: any;

@Inject('@midwayjs/midway-plugin-btmod:articleManager')
articleManager1: any;

async getUser() {
return 'harry' + (await this.articleManager.getOne()) ;
}

async getTest() {
return 'test' + (await this.articleManager1.getOne());
}
}
@@ -1,3 +1,4 @@
import { ILifeCycle } from '../../../../../src';
import { Configuration } from '@midwayjs/decorator';

@Configuration({
Expand All @@ -9,6 +10,10 @@ import { Configuration } from '@midwayjs/decorator';
aa: 123
}
})
class AutoConfiguraion {}
class AutoConfiguraion implements ILifeCycle {
async onReady() {
console.log('------auto configuration ready now');
}
}

module.exports = AutoConfiguraion;
@@ -1,7 +1,6 @@
import { Configuration } from '@midwayjs/decorator';

@Configuration({
namespace: '',
importObjects: {
bb: 123,
cc: 'mock'
Expand Down
@@ -1,7 +1,13 @@
import { Config, Provide } from '@midwayjs/decorator';
import { Config, Provide, Inject} from '@midwayjs/decorator';

@Provide()
export class ReplaceManager {
hello;
constructor(@Inject() ctx: any) {
this.hello = ctx;
}
@Inject()
ctx: any;

@Config('ok.text')
config;
Expand Down
7 changes: 7 additions & 0 deletions packages/midway-core/test/loader.test.ts
Expand Up @@ -8,6 +8,7 @@ import * as assert from 'assert';
import * as path from 'path';
import { ContainerLoader, MidwayRequestContainer, clearAllModule } from '../src';
import * as mm from 'mm';
import sinon = require('sinon');

@Provide()
class TestModule {
Expand Down Expand Up @@ -232,6 +233,10 @@ describe('/test/loader.test.ts', () => {

it('should load config.*.ts by process.env MIDWAY_SERVER_ENV', async () => {
mm(process.env, 'MIDWAY_SERVER_ENV', 'local');
const callback = sinon.spy();
mm(console, 'log', (m) => {
callback(m);
});
const loader = new ContainerLoader({
baseDir: path.join(
__dirname,
Expand All @@ -245,6 +250,7 @@ describe('/test/loader.test.ts', () => {
const appCtx = loader.getApplicationContext();
const replaceManager: any = await appCtx.getAsync('@ok:replaceManager');
assert((await replaceManager.getOne()) === 'ok1');
assert.ok(callback.withArgs('------auto configuration ready now').calledOnce);
mm.restore();
});

Expand Down Expand Up @@ -302,6 +308,7 @@ describe('/test/loader.test.ts', () => {

const userManager: any = await appCtx.getAsync('userManager');
assert((await userManager.getUser()) === 'harryone article atmod');
assert((await userManager.getTest()) === 'testone article atmod bt');

const repm: any = await appCtx.getAsync(
'@midway-plugin-mod:replaceManager'
Expand Down
@@ -0,0 +1,50 @@
import { IValveHandler, IPipelineContext } from '@midwayjs/core';
import { Provide } from '@midwayjs/decorator';

@Provide()
export class StageOne implements IValveHandler {
async invoke(ctx: IPipelineContext): Promise<any> {
if (ctx.args.aa !== 123) {
throw new Error('args aa is undefined');
}
ctx.set('stageone', 'this is stage one');
ctx.set('stageone_date', Date.now());
if (ctx.info.current !== 'stageOne') {
throw new Error('current stage is not stageOne');
}
if (ctx.info.next !== 'stageTwo') {
throw new Error('next stage is not stageTwo');
}
if (ctx.info.prev) {
throw new Error('stageOne prev stage is not undefined');
}

return 'stageone';
}
}

@Provide()
export class StageTwo implements IValveHandler {
async invoke(ctx: IPipelineContext): Promise<any> {
const keys = ctx.keys();
if (keys.length !== 2) {
throw new Error('keys is not equal');
}
ctx.set('stagetwo', ctx.get('stageone') + 1);
ctx.set('stagetwo_date', Date.now());
if (ctx.info.prevValue !== 'stageone') {
throw new Error('stageone result empty');
}
if (ctx.info.current !== 'stageTwo') {
throw new Error('current stage is not stageTwo');
}
if (ctx.info.next) {
throw new Error('stageTwo next stage is not undefined');
}
if (ctx.info.prev !== 'stageOne') {
throw new Error('prev stage is not stageOne');
}

return 'stagetwo';
}
}

0 comments on commit 4c7ff6a

Please sign in to comment.