Skip to content

Commit

Permalink
fix: should await lifecycle configdidload (#166)
Browse files Browse the repository at this point in the history
* fix: lifecycle configdidload

* fix: lint

* test: optimze test logic

Co-authored-by: wanghx <[email protected]>
  • Loading branch information
whxaxes and wanghx authored Aug 10, 2022
1 parent f2a7142 commit d068007
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class ArtusApplication implements Application {
id: ArtusInjectEnum.Config,
value: this.configurationHandler.getAllConfig(),
});
this.lifecycleManager.emitHook('configDidLoad');
return this.lifecycleManager.emitHook('configDidLoad');
},
})
.addLoaderListener('framework-config', {
Expand Down
57 changes: 57 additions & 0 deletions test/fixtures/app_with_lifecycle/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
ArtusApplication,
ApplicationLifecycle, LifecycleHookUnit, LifecycleHook, ArtusInjectEnum,
} from '../../../src/index';

import { Container, Inject } from '@artus/injection';
import LifecycleList from './lifecyclelist';
export const TEST_LIFE_CYCLE_LIST = 'TEST_LIFE_CYCLE_LIST';

@LifecycleHookUnit()
export default class AppLifecycle implements ApplicationLifecycle {
@Inject(ArtusInjectEnum.Application)
app: ArtusApplication;

@Inject()
container: Container;

@Inject()
lifecycleList: LifecycleList;

@LifecycleHook()
async configWillLoad() {
this.container.set({ id: TEST_LIFE_CYCLE_LIST, value: this.lifecycleList });
await new Promise(resolve => setTimeout(resolve, 100));
this.lifecycleList.add('app_configWillLoad');
}

@LifecycleHook('configDidLoad')
async customNameConfigDidLoad() {
await new Promise(resolve => setTimeout(resolve, 100));
this.lifecycleList.add('app_configDidLoad');
}

@LifecycleHook('didLoad')
async didLoad() {
await new Promise(resolve => setTimeout(resolve, 100));
this.lifecycleList.add('app_didLoad');
}

@LifecycleHook('willReady')
async willReady() {
await new Promise(resolve => setTimeout(resolve, 100));
this.lifecycleList.add('app_willReady');
}

@LifecycleHook('didReady')
async didReady() {
await new Promise(resolve => setTimeout(resolve, 100));
this.lifecycleList.add('app_didReady');
}

@LifecycleHook()
async beforeClose() {
await new Promise(resolve => setTimeout(resolve, 100));
this.lifecycleList.add('app_beforeClose');
}
}
1 change: 1 addition & 0 deletions test/fixtures/app_with_lifecycle/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};
13 changes: 13 additions & 0 deletions test/fixtures/app_with_lifecycle/config/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import path from 'path';

export default {
pluginA: {
enable: true,
path: path.resolve(__dirname, '../plugins/plugin-a'),
},

pluginB: {
enable: true,
path: path.resolve(__dirname, '../plugins/plugin-b'),
},
};
10 changes: 10 additions & 0 deletions test/fixtures/app_with_lifecycle/lifecyclelist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Injectable } from '../../../src';

@Injectable()
export default class LifecycleList {
lifecycleList: string[] = [];

async add(name: string) {
this.lifecycleList.push(name);
}
}
55 changes: 55 additions & 0 deletions test/fixtures/app_with_lifecycle/plugins/plugin-a/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {
ArtusApplication,
ApplicationLifecycle, LifecycleHookUnit, LifecycleHook, ArtusInjectEnum,
} from '../../../../../src';

import { Container, Inject } from '@artus/injection';
import LifecycleList from '../../lifecyclelist';

@LifecycleHookUnit()
export default class AppLifecycle implements ApplicationLifecycle {
@Inject(ArtusInjectEnum.Application)
app: ArtusApplication;

@Inject()
container: Container;

@Inject()
lifecycleList: LifecycleList;

@LifecycleHook()
async configWillLoad() {
await new Promise(resolve => setTimeout(resolve, 100));
this.lifecycleList.add('pluginA_configWillLoad');
}

@LifecycleHook('configDidLoad')
async customNameConfigDidLoad() {
await new Promise(resolve => setTimeout(resolve, 100));
this.lifecycleList.add('pluginA_configDidLoad');
}

@LifecycleHook('didLoad')
async didLoad() {
await new Promise(resolve => setTimeout(resolve, 100));
this.lifecycleList.add('pluginA_didLoad');
}

@LifecycleHook('willReady')
async willReady() {
await new Promise(resolve => setTimeout(resolve, 100));
this.lifecycleList.add('pluginA_willReady');
}

@LifecycleHook('didReady')
async didReady() {
await new Promise(resolve => setTimeout(resolve, 100));
this.lifecycleList.add('pluginA_didReady');
}

@LifecycleHook()
async beforeClose() {
await new Promise(resolve => setTimeout(resolve, 100));
this.lifecycleList.add('pluginA_beforeClose');
}
}
3 changes: 3 additions & 0 deletions test/fixtures/app_with_lifecycle/plugins/plugin-a/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "pluginA"
}
49 changes: 49 additions & 0 deletions test/fixtures/app_with_lifecycle/plugins/plugin-b/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
ArtusApplication,
ApplicationLifecycle, LifecycleHookUnit, LifecycleHook, ArtusInjectEnum,
} from '../../../../../src';

import { Container, Inject } from '@artus/injection';
import LifecycleList from '../../lifecyclelist';

@LifecycleHookUnit()
export default class AppLifecycle implements ApplicationLifecycle {
@Inject(ArtusInjectEnum.Application)
app: ArtusApplication;

@Inject()
container: Container;

@Inject()
lifecycleList: LifecycleList;

@LifecycleHook('configWillLoad')
customConfigWillLoad() {
this.lifecycleList.add('pluginB_configWillLoad');
}

@LifecycleHook('configDidLoad')
customNameConfigDidLoad() {
this.lifecycleList.add('pluginB_configDidLoad');
}

@LifecycleHook('didLoad')
didLoad() {
this.lifecycleList.add('pluginB_didLoad');
}

@LifecycleHook('willReady')
willReady() {
this.lifecycleList.add('pluginB_willReady');
}

@LifecycleHook('didReady')
didReady() {
this.lifecycleList.add('pluginB_didReady');
}

@LifecycleHook()
beforeClose() {
this.lifecycleList.add('pluginB_beforeClose');
}
}
3 changes: 3 additions & 0 deletions test/fixtures/app_with_lifecycle/plugins/plugin-b/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "pluginB"
}
53 changes: 53 additions & 0 deletions test/lifecycle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import path from 'path';
import assert from 'assert';
import { createApp } from './utils';
import LifecycleList from './fixtures/app_with_lifecycle/lifecyclelist';

describe('test/lifecycle.test.ts', () => {
it('should lifecycle works without error', async () => {
const appWithLifeCyclePath = path.resolve(__dirname, './fixtures/app_with_lifecycle');
const app = await createApp(appWithLifeCyclePath);
const lifecycleList = app.container.get(LifecycleList).lifecycleList;

assert.deepStrictEqual(lifecycleList, [
'pluginA_configWillLoad',
'pluginB_configWillLoad',
'app_configWillLoad',
'pluginA_configDidLoad',
'pluginB_configDidLoad',
'app_configDidLoad',
'pluginA_didLoad',
'pluginB_didLoad',
'app_didLoad',
'pluginA_willReady',
'pluginB_willReady',
'app_willReady',
'pluginA_didReady',
'pluginB_didReady',
'app_didReady',
]);

await app.close();

assert.deepStrictEqual(lifecycleList, [
'pluginA_configWillLoad',
'pluginB_configWillLoad',
'app_configWillLoad',
'pluginA_configDidLoad',
'pluginB_configDidLoad',
'app_configDidLoad',
'pluginA_didLoad',
'pluginB_didLoad',
'app_didLoad',
'pluginA_willReady',
'pluginB_willReady',
'app_willReady',
'pluginA_didReady',
'pluginB_didReady',
'app_didReady',
'pluginA_beforeClose',
'pluginB_beforeClose',
'app_beforeClose',
]);
});
});
4 changes: 2 additions & 2 deletions test/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import assert from 'assert';
import path from 'path';
import { Container } from '@artus/injection';
import { LoaderFactory } from '../src';
import { createApp } from './fixtures/custom_instance/index';
import Custom from './fixtures/custom_instance/custom';
import { createApp } from './utils';

describe('test/loader.test.ts', () => {
describe('module with ts', () => {
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('test/loader.test.ts', () => {

describe('custom instance', () => {
it('should not overide custom instance', async () => {
const app = await createApp();
const app = await createApp(path.resolve(__dirname, './fixtures/custom_instance'));
expect(app.container.get(Custom).getName()).toBe('foo');
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Scanner, ArtusApplication } from '../../../src/index';
import 'reflect-metadata';
import { Scanner, ArtusApplication } from '../../src';

export async function createApp() {
const baseDir = __dirname;
export async function createApp(baseDir: string) {
const scanner = new Scanner({
needWriteFile: false,
configDir: 'config',
Expand Down

0 comments on commit d068007

Please sign in to comment.