feat: stopped state in application (#1543)

This commit is contained in:
ChengLei Shao 2023-03-06 21:19:49 +08:00 committed by GitHub
parent 2e4354fcf3
commit db6d572638
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -172,6 +172,8 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
declare middleware: any;
stopped: boolean = false;
constructor(public options: ApplicationOptions) {
super();
this.init();
@ -434,6 +436,7 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
}
await this.emitAsync('afterStart', this, options);
this.stopped = false;
}
listen(...args): Server {
@ -441,6 +444,11 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
}
async stop(options: any = {}) {
if (this.stopped) {
this.log.warn(`Application ${this.name} already stopped`);
return;
}
await this.emitAsync('beforeStop', this, options);
try {
@ -460,6 +468,7 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
}
await this.emitAsync('afterStop', this, options);
this.stopped = true;
}
async destroy(options: any = {}) {