Skip to content

Commit

Permalink
fix: 2.x fix lifecycle bug (#435)
Browse files Browse the repository at this point in the history
* fix circular bug

* feat: lifecycle onReady add container

* fix bug
  • Loading branch information
kurten authored Mar 19, 2020
1 parent ca2b03a commit 22d3e12
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 21 deletions.
16 changes: 14 additions & 2 deletions packages/midway-core/src/context/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { CONFIGURATION_KEY, InjectionConfigurationOptions, getClassMetadata } from '@midwayjs/decorator';
import { CONFIGURATION_KEY, InjectionConfigurationOptions, getClassMetadata, LIFECYCLE_IDENTIFIER_PREFIX, classNamed, saveModule, saveProviderId } from '@midwayjs/decorator';
import * as is from 'is-type-of';
import { dirname, isAbsolute, join } from 'path';
import { IContainerConfiguration, IMidwayContainer, MAIN_MODULE_KEY } from '../interface';
import { isPath, safeRequire } from '../common/util';
import { isPath, safeRequire, generateProvideId } from '../common/util';

const debug = require('debug')('midway:container:configuration');

Expand Down Expand Up @@ -131,10 +131,22 @@ export class ContainerConfiguration implements IContainerConfiguration {
this.addImports(configurationOptions.imports, baseDir);
this.addImportObjects(configurationOptions.importObjects);
this.addImportConfigs(configurationOptions.importConfigs, baseDir);
this.bindConfigurationClass(configurationExport);
}
}
}
}
/**
* 用于 ready 或者 stop 时处理 lifecycle 实现
* @param clzz configuration class
*/
bindConfigurationClass(clzz) {
const clzzName = `${LIFECYCLE_IDENTIFIER_PREFIX}${classNamed(clzz.name)}`;
const id = generateProvideId(clzzName, this.namespace);
saveProviderId(id, clzz, true);
this.container.bind(id, clzz, { namespace: this.namespace });
saveModule(CONFIGURATION_KEY, clzz);
}

getImportDirectory() {
return this.loadDirs;
Expand Down
1 change: 1 addition & 0 deletions packages/midway-core/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export interface IContainerConfiguration {
loadConfiguration(configuration: IContainerConfiguration, baseDir: string);
getImportDirectory(): string[];
getImportObjects(): any;
bindConfigurationClass(clzz: any);
}

export interface IMidwayContainer extends IContainer {
Expand Down
14 changes: 8 additions & 6 deletions packages/midway-core/test/context/midwayContainer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { TestBinding, LifeCycleTest, LifeCycleTest1 } from '../fixtures/lifecycl
import sinon = require('sinon');
import mm = require('mm');
import * as decs from '@midwayjs/decorator';
const { LIFECYCLE_IDENTIFIER_PREFIX } = decs;

describe('/test/midwayContainer.test.ts', () => {

Expand Down Expand Up @@ -94,21 +95,22 @@ describe('/test/midwayContainer.test.ts', () => {
const container = new MidwayContainer();

it('lifecycle should be ok', async () => {
const cfg = container.createConfiguration();
container.bind(TestBinding);
container.bind(LifeCycleTest);
container.bind(LifeCycleTest1);
cfg.bindConfigurationClass(LifeCycleTest);
cfg.bindConfigurationClass(LifeCycleTest1);

expect(container.isReady).false;
await container.ready();
expect(container.isReady).true;

const aa = await container.getAsync<LifeCycleTest>('__lifecycle__lifeCycleTest');
const aa = await container.getAsync<LifeCycleTest>(LIFECYCLE_IDENTIFIER_PREFIX + 'lifeCycleTest');
expect(aa.ts).eq('hello');
expect(aa.ready).true;
// container.registerObject('hellotest111', '12312312');
expect(container.get('hellotest111')).eq('12312312');

const aa1 = await container.getAsync<LifeCycleTest1>('__lifecycle__lifeCycleTest1');
const aa1 = await container.getAsync<LifeCycleTest1>(LIFECYCLE_IDENTIFIER_PREFIX + 'lifeCycleTest1');
expect(aa1.tts).eq('hello');
expect(aa1.ready).true;

Expand All @@ -117,9 +119,9 @@ describe('/test/midwayContainer.test.ts', () => {
callback(m);
});

expect(container.registry.hasObject('__lifecycle__lifeCycleTest')).true;
expect(container.registry.hasObject(LIFECYCLE_IDENTIFIER_PREFIX + 'lifeCycleTest')).true;
await container.stop();
expect(container.registry.hasObject('__lifecycle__lifeCycleTest')).false;
expect(container.registry.hasObject(LIFECYCLE_IDENTIFIER_PREFIX + 'lifeCycleTest')).false;
expect(callback.withArgs('on stop').calledOnce).true;

mm.restore();
Expand Down
5 changes: 4 additions & 1 deletion packages/midway-core/test/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '@midwayjs/decorator';
import * as assert from 'assert';
import * as path from 'path';
import { ContainerLoader, MidwayRequestContainer } from '../src';
import { ContainerLoader, MidwayRequestContainer, clearAllModule } from '../src';
import * as mm from 'mm';

@Provide()
Expand All @@ -17,6 +17,9 @@ class TestModule {
}

describe('/test/loader.test.ts', () => {
beforeEach(() => {
clearAllModule();
});
it('should create new loader', async () => {
const loader = new ContainerLoader({
baseDir: path.join(__dirname, './fixtures/base-app/src'),
Expand Down
6 changes: 1 addition & 5 deletions packages/midway-decorator/src/annotation/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { saveClassMetadata, saveModule, saveProviderId, LIFECYCLE_IDENTIFIER_PREFIX, CONFIGURATION_KEY, classNamed } from '../common';
import { saveClassMetadata, CONFIGURATION_KEY } from '../common';

export interface InjectionConfigurationOptions {
imports?: string[];
Expand All @@ -11,10 +11,6 @@ export function Configuration(
options: InjectionConfigurationOptions
): ClassDecorator {
return (target: any) => {
saveModule(CONFIGURATION_KEY, target);
saveClassMetadata(CONFIGURATION_KEY, options, target);

const identifier = `${LIFECYCLE_IDENTIFIER_PREFIX}${classNamed(target.name)}`;
saveProviderId(identifier, target);
};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import { expect } from 'chai';
import { Configuration, getClassMetadata, CONFIGURATION_KEY, getProviderId, LIFECYCLE_IDENTIFIER_PREFIX, listModule } from '../../src';
import { Configuration, getClassMetadata, CONFIGURATION_KEY } from '../../src';

@Configuration({
importConfigs: ['./config.default'],
Expand All @@ -19,11 +19,5 @@ describe('/test/annotation/configuration.test.ts', () => {
imports: ['./nodes'],
namespace: 'hello'
});

const id = getProviderId(Test);
expect(id).eq(LIFECYCLE_IDENTIFIER_PREFIX + 'test');

const ms = listModule(CONFIGURATION_KEY);
expect(ms.length).eq(1);
});
});

0 comments on commit 22d3e12

Please sign in to comment.