Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reverse emit function list when beforeClose hook #264

Merged
merged 2 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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