import * as GracefulType from "@unboundedsystems/node-graceful" ; import { MaybePromise } from "./common_types" ; import { processGlobal } from "./process_global" ; export type ExitHandler = ( signal : string , details ? : Error | number ) => MaybePromise < void | any > ; export type RemoveHandler = ( ) => void ; interface ExitGlobals { capExit : any ; graceful : typeof GracefulType . default ; } const exitGlobals = processGlobal < ExitGlobals > ( "exitGlobals" , ( ) => { const g = { capExit : require ( "capture-exit" ) , graceful : require ( "@unboundedsystems/node-graceful" ) , } ; g . capExit . captureExit ( ) ; g . graceful . captureRejections = true ; return g ; } ) ; const { capExit , graceful } = exitGlobals ; remarks export function onExit ( handler ) { const capHandler = ( code ) => handler ( "exit" , code ) ; capExit . onExit ( capHandler ) ; const graceHandler = ( sig , details ) => { capExit . offExit ( capHandler ) ; return handler ( sig , details ) ; } ; graceful . on ( "exit" , graceHandler ) ; return ( ) => { capExit . offExit ( capHandler ) ; graceful . off ( "exit" , graceHandler ) ; } ; }