Skip to content

Commit 2043750

Browse files
committed
Fix onExit, onClose
Previously, they were not calling the callback.
1 parent cc7c581 commit 2043750

File tree

3 files changed

+52
-38
lines changed

3 files changed

+52
-38
lines changed

src/Node/ChildProcess.js

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,24 @@ exports.spawnImpl = function spawnImpl(command) {
1212
};
1313
};
1414
};
15-
exports.mkOnExit = function mkOnExit(nothing){
16-
return function(just){
17-
return function(signalConstr){
18-
return function onExit(cp){
19-
return function(cb){
20-
return function(){
21-
cp.on("exit", function(code, signal){
22-
cb(code ? just(code) : nothing, signal ? just(signalConstr(signal)) : nothing);
23-
});
24-
};
25-
};
15+
exports.mkOnExit = function mkOnExit(mkChildExit){
16+
return function onExit(cp){
17+
return function(cb){
18+
return function(){
19+
cp.on("exit", function(code, signal){
20+
cb(mkChildExit(code)(signal))();
21+
});
2622
};
2723
};
2824
};
2925
};
30-
exports.mkOnClose = function mkOnClose(nothing){
31-
return function(just){
32-
return function(signalConstr){
33-
return function onClose(cp){
34-
return function(cb){
35-
return function(){
36-
cp.on("close", function(code, signal){
37-
cb(code ? just(code) : nothing, signal ? just(signalConstr(signal)) : nothing);
38-
});
39-
};
40-
};
26+
exports.mkOnClose = function mkOnClose(mkChildExit){
27+
return function onClose(cp){
28+
return function(cb){
29+
return function(){
30+
cp.on("exit", function(code, signal){
31+
cb(mkChildExit(code)(signal))();
32+
});
4133
};
4234
};
4335
};

src/Node/ChildProcess.purs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@ module Node.ChildProcess
1111
, send
1212
, disconnect
1313
, ChildProcessError()
14+
, ChildProcessExit()
1415
, onExit
1516
, onClose
1617
, onDisconnect
1718
, onMessage
1819
, onError
1920
, spawn
2021
, SpawnOptions()
21-
, StdIOBehaviour()
2222
, defaultSpawnOptions
23+
, StdIOBehaviour(..)
24+
, pipe
25+
, inherit
26+
, ignore
2327
) where
2428

2529
import Prelude
@@ -28,7 +32,7 @@ import Control.Monad.Eff (Eff())
2832

2933
import Data.StrMap (StrMap())
3034
import Data.Function (Fn2(), runFn2)
31-
import Data.Nullable (Nullable(), toNullable)
35+
import Data.Nullable (Nullable(), toNullable, toMaybe)
3236
import Data.Maybe (Maybe(..), fromMaybe)
3337
import Data.Foreign (Foreign())
3438
import Unsafe.Coerce (unsafeCoerce)
@@ -97,6 +101,16 @@ disconnect = _.disconnect <<< runChildProcess
97101
kill :: forall eff. Signal -> ChildProcess -> Eff (cp :: CHILD_PROCESS | eff) Boolean
98102
kill sig (ChildProcess cp) = pure (cp.kill sig)
99103

104+
-- | Specifies how a child process exited; normally (with an exit code), or
105+
-- | due to a signal.
106+
data ChildProcessExit
107+
= Normally Int
108+
| BySignal Signal
109+
110+
instance showChildProcessExit :: Show ChildProcessExit where
111+
show (Normally x) = "Normally " <> show x
112+
show (BySignal sig) = "BySignal " <> show sig
113+
100114
type SpawnOptions =
101115
{ cwd :: Maybe String
102116
, stdio :: Array (Maybe StdIOBehaviour)
@@ -106,19 +120,25 @@ type SpawnOptions =
106120
, gid :: Maybe Int
107121
}
108122

109-
onExit :: forall eff. ChildProcess -> (Maybe Int -> Maybe Signal -> Eff eff Unit) -> Eff eff Unit
110-
onExit = mkOnExit Nothing Just Signal
123+
mkChildProcessExit :: Nullable Int -> Nullable Signal -> ChildProcessExit
124+
mkChildProcessExit code signal =
125+
case toMaybe code of
126+
Just code -> Normally code
127+
Nothing -> BySignal (unsafeCoerce signal)
128+
129+
onExit :: forall eff. ChildProcess -> (ChildProcessExit -> Eff eff Unit) -> Eff eff Unit
130+
onExit = mkOnExit mkChildProcessExit
111131

112-
foreign import mkOnExit :: forall a eff.
113-
Maybe a -> (a -> Maybe a) -> (String -> Signal) ->
114-
ChildProcess -> (Maybe Int -> Maybe Signal -> Eff eff Unit) -> Eff eff Unit
132+
foreign import mkOnExit :: forall eff.
133+
(Nullable Int -> Nullable Signal -> ChildProcessExit)
134+
-> ChildProcess -> (ChildProcessExit -> Eff eff Unit) -> Eff eff Unit
115135

116-
onClose :: forall eff. ChildProcess -> (Maybe Int -> Maybe Signal -> Eff eff Unit) -> Eff eff Unit
117-
onClose = mkOnClose Nothing Just Signal
136+
onClose :: forall eff. ChildProcess -> (ChildProcessExit -> Eff eff Unit) -> Eff eff Unit
137+
onClose = mkOnClose mkChildProcessExit
118138

119-
foreign import mkOnClose :: forall a eff.
120-
Maybe a -> (a -> Maybe a) -> (String -> Signal) ->
121-
ChildProcess -> (Maybe Int -> Maybe Signal -> Eff eff Unit) -> Eff eff Unit
139+
foreign import mkOnClose :: forall eff.
140+
(Nullable Int -> Nullable Signal -> ChildProcessExit)
141+
-> ChildProcess -> (ChildProcessExit -> Eff eff Unit) -> Eff eff Unit
122142

123143
onMessage :: forall eff. ChildProcess -> (Foreign -> Maybe Handle -> Eff eff Unit) -> Eff eff Unit
124144
onMessage = mkOnMessage Nothing Just
@@ -130,6 +150,10 @@ foreign import mkOnMessage :: forall a eff.
130150
foreign import onDisconnect :: forall eff. ChildProcess -> Eff eff Unit -> Eff eff Unit
131151
foreign import onError :: forall eff. ChildProcess -> (ChildProcessError -> Eff eff Unit) -> Eff eff Unit
132152

153+
-- | Spawn a child process. Note that, in the event that a child process could
154+
-- | not be spawned (for example, if the executable was not found) this will
155+
-- | not throw an error. Instead, the `ChildProcess` will be created anyway,
156+
-- | but it will immediately emit an 'error' event.
133157
spawn :: forall eff. String -> Array String -> SpawnOptions -> Eff (cp :: CHILD_PROCESS | eff) ChildProcess
134158
spawn cmd args opts = spawnImpl cmd args (convertOpts opts)
135159
where

test/Main.purs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import Node.Stream (onData)
1313

1414
main = do
1515
ls <- spawn "ls" ["-la"] defaultSpawnOptions
16-
onClose ls \code sig ->
17-
log $ "ls exited with code: " ++ (show code) ++ "\nfrom signal: " ++ (show sig)
16+
onExit ls \exit ->
17+
log $ "ls exited: " <> show exit
1818
onData (stdout ls) (Buffer.toString UTF8 >=> log)
19-
kill sigterm ls
20-
log "Killed."

0 commit comments

Comments
 (0)