-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: should await lifecycle configdidload (#166)
* fix: lifecycle configdidload * fix: lint * test: optimze test logic Co-authored-by: wanghx <[email protected]>
- Loading branch information
Showing
12 changed files
with
250 additions
and
6 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,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'); | ||
} | ||
} |
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 @@ | ||
export default {}; |
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,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'), | ||
}, | ||
}; |
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,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
55
test/fixtures/app_with_lifecycle/plugins/plugin-a/bootstrap.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,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'); | ||
} | ||
} |
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,3 @@ | ||
{ | ||
"name": "pluginA" | ||
} |
49 changes: 49 additions & 0 deletions
49
test/fixtures/app_with_lifecycle/plugins/plugin-b/bootstrap.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,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'); | ||
} | ||
} |
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,3 @@ | ||
{ | ||
"name": "pluginB" | ||
} |
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,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', | ||
]); | ||
}); | ||
}); |
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