6090508bad
- Add curly braces to all if/else/for/while statements - Fix indentation and trailing spaces - Auto-fixed 372 linting errors using eslint --fix - Remaining issues are warnings only (non-null assertions, explicit any types)
35 lines
801 B
TypeScript
35 lines
801 B
TypeScript
type ShutdownHandler = () => Promise<void>;
|
|
|
|
export class Lifecycle {
|
|
private shutdownHandlers: ShutdownHandler[] = [];
|
|
private shuttingDown = false;
|
|
private _isRunning = true;
|
|
|
|
get isRunning(): boolean {
|
|
return this._isRunning;
|
|
}
|
|
|
|
onShutdown(handler: ShutdownHandler): void {
|
|
this.shutdownHandlers.push(handler);
|
|
}
|
|
|
|
async shutdown(): Promise<void> {
|
|
if (this.shuttingDown) {return;}
|
|
this.shuttingDown = true;
|
|
this._isRunning = false;
|
|
|
|
console.log('Shutting down...');
|
|
|
|
// Execute handlers in reverse order (LIFO)
|
|
for (const handler of [...this.shutdownHandlers].reverse()) {
|
|
try {
|
|
await handler();
|
|
} catch (error) {
|
|
console.error('Shutdown handler error:', error);
|
|
}
|
|
}
|
|
|
|
console.log('Shutdown complete');
|
|
}
|
|
}
|