Skip to content

Commit

Permalink
feat: extend gracefulShutdown options
Browse files Browse the repository at this point in the history
  • Loading branch information
markthree committed Jun 11, 2023
1 parent 8a55918 commit af62eed
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export async function execa(cmd: string[], options: Deno.CommandOptions = {}) {
if (!resolved) {
process.kill()
resolved = true
removeShutdownEvent()
}
})
const { success, code } = await process.status
Expand Down Expand Up @@ -53,18 +52,26 @@ export function execaUnInstall(pm: PM, deps: string[]) {
return execa([pm, isNpm ? "uninstall" : "remove", ...deps])
}

export function gracefulShutdown(shutdown: AnyFunction) {
export function gracefulShutdown(
shutdown: AnyFunction,
options: AddEventListenerOptions = {
once: true,
},
) {
async function exitWithShoutdown() {
await shutdown()
if (options.once) {
Deno.addSignalListener("SIGINT", exitWithShoutdown)
}
Deno.exit(130)
}

// Synchronization error
globalThis.addEventListener("error", shutdown)
globalThis.addEventListener("error", shutdown, options)
// Main process exit
globalThis.addEventListener("unload", shutdown)
globalThis.addEventListener("unload", shutdown, options)
// Asynchronous error
globalThis.addEventListener("unhandledrejection", shutdown)
globalThis.addEventListener("unhandledrejection", shutdown, options)

Deno.addSignalListener("SIGINT", exitWithShoutdown)

Expand Down

0 comments on commit af62eed

Please sign in to comment.