Skip to content

Instantly share code, notes, and snippets.

@fox1t
Created March 2, 2021 11:01
Show Gist options
  • Save fox1t/7be1b459c71d87a10c21ffa73584097a to your computer and use it in GitHub Desktop.
Save fox1t/7be1b459c71d87a10c21ffa73584097a to your computer and use it in GitHub Desktop.
declare namespace closeWithGrace {
interface Options {
/**
* The numbers of milliseconds before abruptly close the process
* @default 10000
*/
delay: number;
}
type Signals = "SIGTERM" | "SIGINT";
interface CloseWithGraceCallback {
(
options: { err?: Error; signal?: Signals; manual?: boolean },
cb: (error?: Error) => void
): void;
}
interface CloseWithGraceAsyncCallback {
(options: {
err?: Error;
signal?: Signals;
manual?: boolean;
}): Promise<void>;
}
}
/**
@example
import * as closeWithGrace from 'close-with-grace'
// delay is the number of milliseconds for the graceful close to
// finish.
closeWithGrace({ delay: 500 }, async function ({ signal, err, manual }) {
if (err) {
console.error(err)
}
await closeYourServer()
})
*/
declare interface CloseWithGraceReturn {
/**
* Close the process, the manual argument will be set to true.
*/
close: () => void;
/**
* Remove all global listeners
*/
uninstall: () => void;
}
declare function closeWithGrace(
opts:
| closeWithGrace.CloseWithGraceAsyncCallback
| closeWithGrace.CloseWithGraceCallback
| closeWithGrace.Options
): CloseWithGraceReturn;
declare function closeWithGrace(
opts: closeWithGrace.Options,
fn?:
| closeWithGrace.CloseWithGraceCallback
| closeWithGrace.CloseWithGraceAsyncCallback
): CloseWithGraceReturn;
export = closeWithGrace;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment