Skip to content

Add more process event handlers #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Node/Process.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ exports.onExit = function (callback) {
};
};

exports.onUncaughtException = function (callback) {
return function () {
process.on("uncaughtException", function (error) {
callback(error)();
});
};
};

exports.onUnhandledRejection = function (callback) {
return function () {
process.on("unhandledRejection", function (error, promise) {
callback(error)(promise)();
});
};
};

exports.onSignalImpl = function (signal) {
return function (callback) {
return function () {
Expand Down
24 changes: 21 additions & 3 deletions src/Node/Process.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module Node.Process
( onBeforeExit
, onExit
, onSignal
, onUncaughtException
, onUnhandledRejection
, argv
, execArgv
, execPath
Expand All @@ -24,17 +26,16 @@ module Node.Process

import Prelude

import Effect (Effect)

import Data.Maybe (Maybe)
import Data.Posix (Pid)
import Data.Posix.Signal (Signal)
import Data.Posix.Signal as Signal
import Effect (Effect)
import Effect.Exception (Error)
import Foreign.Object as FO
import Node.Platform (Platform)
import Node.Platform as Platform
import Node.Stream (Readable, Writable)

import Unsafe.Coerce (unsafeCoerce)

-- YOLO
Expand All @@ -56,6 +57,23 @@ foreign import onBeforeExit :: Effect Unit -> Effect Unit
-- | to exit with.
foreign import onExit :: (Int -> Effect Unit) -> Effect Unit

-- | Install a handler for uncaught exceptions. The callback will be called
-- | when the process emits the `uncaughtException` event. The handler
-- | currently does not expose the second `origin` argument from the Node 12
-- | version of this event to maintain compatibility with older Node versions.
foreign import onUncaughtException :: (Error -> Effect Unit) -> Effect Unit

-- | Install a handler for unhandled promise rejections. The callback will be
-- | called when the process emits the `unhandledRejection` event.
-- |
-- | The first argument to the handler can be whatever type the unhandled
-- | Promise yielded on rejection (typically, but not necessarily, an `Error`).
-- |
-- | The handler currently does not expose the type of the second argument,
-- | which is a `Promise`, in order to allow users of this library to choose
-- | their own PureScript `Promise` bindings.
foreign import onUnhandledRejection :: forall a b. (a -> b -> Effect Unit) -> Effect Unit

foreign import onSignalImpl :: String -> Effect Unit -> Effect Unit

-- | Install a handler for a particular signal.
Expand Down