Skip to content

Commit

Permalink
fix: should trigger lifecycle hook with only one time (#168)
Browse files Browse the repository at this point in the history
Co-authored-by: wanghx <[email protected]>
Co-authored-by: hyj1991 <[email protected]>
  • Loading branch information
3 people authored Aug 16, 2022
1 parent 364a65e commit 57fa1ff
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/lifecycle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export class LifecycleManager {
if (!Array.isArray(fnList)) {
return;
}
// lifecycle hook should only trigger one time
this.hookFnMap.delete(hookName);
for (const hookFn of fnList) {
await hookFn({
app: this.app,
Expand Down
38 changes: 38 additions & 0 deletions test/lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,42 @@ describe('test/lifecycle.test.ts', () => {
'app_beforeClose',
]);
});

it('should not trigger lifecyle multi times', async () => {
const appWithLifeCyclePath = path.resolve(__dirname, './fixtures/app_with_lifecycle');
const app = await createApp(appWithLifeCyclePath);
const lifecycleList = app.container.get(LifecycleList).lifecycleList;
await Promise.all([
app.run(),
app.run(),
app.run(),
]);

await Promise.all([
app.close(),
app.close(),
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',
]);
});
});

0 comments on commit 57fa1ff

Please sign in to comment.