Skip to content

Commit

Permalink
fix: reverse emit function list when beforeClose hook (#264)
Browse files Browse the repository at this point in the history
* fix: reverse emit function list when beforeClose hook

* test: reversed lifecycle test case
  • Loading branch information
noahziheng authored Aug 31, 2023
1 parent d9528e0 commit 7dc2e05
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ export class ArtusApplication implements Application {

async close(exit = false) {
try {
await this.lifecycleManager.emitHook('beforeClose');
// reverse emitHook to avoid plugin closed before app hook
await this.lifecycleManager.emitHook('beforeClose', null, true);
} catch (e) {
throw new Error(e);
}
Expand Down
5 changes: 4 additions & 1 deletion src/lifecycle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class LifecycleManager {
}
}

async emitHook<T = unknown>(hookName: string, payload?: T) {
async emitHook<T = unknown>(hookName: string, payload?: T, reverse = false) {
if (!this.enable) {
return;
}
Expand All @@ -83,6 +83,9 @@ export class LifecycleManager {
}
// lifecycle hook should only trigger one time
this.hookFnMap.delete(hookName);
if (reverse) {
fnList.reverse();
}
for (const hookFn of fnList) {
await hookFn({
app: this.app,
Expand Down
8 changes: 4 additions & 4 deletions test/lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ describe('test/lifecycle.test.ts', () => {
'pluginA_didReady',
'pluginB_didReady',
'app_didReady',
'pluginA_beforeClose',
'pluginB_beforeClose',
'app_beforeClose',
'pluginB_beforeClose',
'pluginA_beforeClose',
]);
});

Expand Down Expand Up @@ -84,9 +84,9 @@ describe('test/lifecycle.test.ts', () => {
'pluginA_didReady',
'pluginB_didReady',
'app_didReady',
'pluginA_beforeClose',
'pluginB_beforeClose',
'app_beforeClose',
'pluginB_beforeClose',
'pluginA_beforeClose',
]);
});

Expand Down

0 comments on commit 7dc2e05

Please sign in to comment.