Skip to content

Commit c819fb8

Browse files
committed
Switch to purescript-posix-types
1 parent 4b8c051 commit c819fb8

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

bower.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"purescript-maps": "~0.5.3",
1515
"purescript-nullable": "~0.2.1",
1616
"purescript-unsafe-coerce": "~0.1.0",
17-
"purescript-node-fs": "~0.9.2"
17+
"purescript-node-fs": "~0.9.2",
18+
"purescript-posix-types": "~0.1.1",
19+
"purescript-exceptions": "~0.3.4"
1820
},
1921
"devDependencies": {
2022
"purescript-console": "~0.1.1"

src/Node/ChildProcess.purs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,23 @@ module Node.ChildProcess
2929

3030
import Prelude
3131

32+
import Control.Alt ((<|>))
33+
import Control.Bind ((>=>))
3234
import Control.Monad.Eff (Eff())
35+
import Control.Monad.Eff.Exception.Unsafe (unsafeThrow)
3336

3437
import Data.StrMap (StrMap())
3538
import Data.Function (Fn2(), runFn2)
3639
import Data.Nullable (Nullable(), toNullable, toMaybe)
3740
import Data.Maybe (Maybe(..), fromMaybe)
3841
import Data.Foreign (Foreign())
42+
import Data.Posix (Pid())
43+
import Data.Posix.Signal (Signal())
44+
import Data.Posix.Signal as Signal
3945
import Unsafe.Coerce (unsafeCoerce)
4046

4147
import Node.FS as FS
42-
import Node.Buffer (Buffer())
4348
import Node.Stream (Readable(), Writable(), Stream())
44-
import Node.ChildProcess.Signal (Signal())
4549

4650
-- | A handle for inter-process communication (IPC).
4751
foreign import data Handle :: *
@@ -60,9 +64,9 @@ type ChildProcessRec =
6064
{ stdin :: forall eff. Nullable (Writable () (cp :: CHILD_PROCESS | eff))
6165
, stdout :: forall eff. Nullable (Readable () (cp :: CHILD_PROCESS | eff))
6266
, stderr :: forall eff. Nullable (Readable () (cp :: CHILD_PROCESS | eff))
63-
, pid :: Int
67+
, pid :: Pid
6468
, connected :: Boolean
65-
, kill :: Signal -> Boolean
69+
, kill :: String -> Boolean
6670
, send :: forall r. Fn2 { | r} Handle Boolean
6771
, disconnect :: forall eff. Eff eff Unit
6872
}
@@ -92,7 +96,7 @@ foreign import unsafeFromNullable :: forall a. String -> Nullable a -> a
9296

9397
-- | The process ID of a child process. Note that if the process has already
9498
-- | exited, another process may have taken the same ID, so be careful!
95-
pid :: ChildProcess -> Int
99+
pid :: ChildProcess -> Pid
96100
pid = _.pid <<< runChildProcess
97101

98102
connected :: forall eff. ChildProcess -> Eff (cp :: CHILD_PROCESS | eff) Boolean
@@ -108,7 +112,7 @@ disconnect = _.disconnect <<< runChildProcess
108112
-- | that this function is called "kill", as sending a signal to a child
109113
-- | process won't necessarily kill it.
110114
kill :: forall eff. Signal -> ChildProcess -> Eff (cp :: CHILD_PROCESS | eff) Boolean
111-
kill sig (ChildProcess cp) = pure (cp.kill sig)
115+
kill sig (ChildProcess cp) = pure (cp.kill (Signal.toString sig))
112116

113117
-- | Specifies how a child process exited; normally (with an exit code), or
114118
-- | due to a signal.
@@ -129,24 +133,27 @@ type SpawnOptions =
129133
, gid :: Maybe Int
130134
}
131135

132-
mkChildProcessExit :: Nullable Int -> Nullable Signal -> ChildProcessExit
136+
mkChildProcessExit :: Nullable Int -> Nullable String -> ChildProcessExit
133137
mkChildProcessExit code signal =
134-
case toMaybe code of
135-
Just code -> Normally code
136-
Nothing -> BySignal (unsafeCoerce signal)
138+
case fromCode code <|> fromSignal signal of
139+
Just e -> e
140+
Nothing -> unsafeThrow "Node.ChildProcess.mkChildProcessExit: Invalid arguments"
141+
where
142+
fromCode = toMaybe >>> map Normally
143+
fromSignal = toMaybe >=> Signal.fromString >>> map BySignal
137144

138145
onExit :: forall eff. ChildProcess -> (ChildProcessExit -> Eff eff Unit) -> Eff eff Unit
139146
onExit = mkOnExit mkChildProcessExit
140147

141148
foreign import mkOnExit :: forall eff.
142-
(Nullable Int -> Nullable Signal -> ChildProcessExit)
149+
(Nullable Int -> Nullable String -> ChildProcessExit)
143150
-> ChildProcess -> (ChildProcessExit -> Eff eff Unit) -> Eff eff Unit
144151

145152
onClose :: forall eff. ChildProcess -> (ChildProcessExit -> Eff eff Unit) -> Eff eff Unit
146153
onClose = mkOnClose mkChildProcessExit
147154

148155
foreign import mkOnClose :: forall eff.
149-
(Nullable Int -> Nullable Signal -> ChildProcessExit)
156+
(Nullable Int -> Nullable String -> ChildProcessExit)
150157
-> ChildProcess -> (ChildProcessExit -> Eff eff Unit) -> Eff eff Unit
151158

152159
onMessage :: forall eff. ChildProcess -> (Foreign -> Maybe Handle -> Eff eff Unit) -> Eff eff Unit

0 commit comments

Comments
 (0)