mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-01 03:05:36 +08:00
53 lines
1.1 KiB
TypeScript
53 lines
1.1 KiB
TypeScript
/**
|
|
* @since 1.0.0
|
|
*/
|
|
import type { Effect } from "effect/Effect"
|
|
import * as Runtime from "effect/Runtime"
|
|
|
|
/**
|
|
* @since 1.0.0
|
|
* @category Run main
|
|
*/
|
|
export const runMain: {
|
|
(
|
|
options?: {
|
|
readonly disableErrorReporting?: boolean | undefined
|
|
readonly teardown?: Runtime.Teardown | undefined
|
|
}
|
|
): <E, A>(effect: Effect<A, E>) => void
|
|
<E, A>(
|
|
effect: Effect<A, E>,
|
|
options?: {
|
|
readonly disableErrorReporting?: boolean | undefined
|
|
readonly teardown?: Runtime.Teardown | undefined
|
|
}
|
|
): void
|
|
} = Runtime.makeRunMain(({
|
|
fiber,
|
|
teardown
|
|
}) => {
|
|
let receivedSignal = false
|
|
|
|
fiber.addObserver((exit) => {
|
|
if (!receivedSignal) {
|
|
process.removeListener("SIGINT", onSigint)
|
|
process.removeListener("SIGTERM", onSigint)
|
|
}
|
|
teardown(exit, (code) => {
|
|
if (receivedSignal || code !== 0) {
|
|
process.exit(code)
|
|
}
|
|
})
|
|
})
|
|
|
|
function onSigint() {
|
|
receivedSignal = true
|
|
process.removeListener("SIGINT", onSigint)
|
|
process.removeListener("SIGTERM", onSigint)
|
|
fiber.interruptUnsafe(fiber.id)
|
|
}
|
|
|
|
process.on("SIGINT", onSigint)
|
|
process.on("SIGTERM", onSigint)
|
|
})
|