@@ -8,8 +8,9 @@ import Data.Maybe (fromJust)
8
8
import Data.Posix (Pid )
9
9
import Effect.Aff (Aff , effectCanceler , makeAff )
10
10
import Effect.Ref as Ref
11
- import Node.ChildProcess (ChildProcess , pid )
12
- import Node.ChildProcess as CP
11
+ import Node.ChildProcess.Types (UnsafeChildProcess )
12
+ import Node.UnsafeChildProcess.Safe as CPSafe
13
+ import Node.ChildProcess (ChildProcess , toUnsafeChildProcess )
13
14
import Node.Errors.SystemError (SystemError )
14
15
import Node.EventEmitter (once )
15
16
import Partial.Unsafe (unsafePartial )
@@ -19,21 +20,30 @@ import Partial.Unsafe (unsafePartial)
19
20
-- | and the `pid` of the process can be obtained.
20
21
-- | If an `error` event fires, child process was not started successfully.
21
22
waitSpawned :: ChildProcess -> Aff (Either SystemError Pid )
22
- waitSpawned cp = parOneOf [ pidOnSpawn, errored ]
23
+ waitSpawned = toUnsafeChildProcess >>> waitSpawned'
24
+
25
+ -- | Same as `waitSpawned` but works on `UnsafeChildProcess`
26
+ -- |
27
+ -- | Blocks until either a `spawn` or `error` event is fired.
28
+ -- | If a `spawn` event fired, child process was successfully started
29
+ -- | and the `pid` of the process can be obtained.
30
+ -- | If an `error` event fires, child process was not started successfully.
31
+ waitSpawned' :: UnsafeChildProcess -> Aff (Either SystemError Pid )
32
+ waitSpawned' cp = parOneOf [ pidOnSpawn, errored ]
23
33
where
24
34
pidOnSpawn = makeAff \done -> do
25
35
ref <- Ref .new mempty
26
- removeListener <- cp # once CP .spawnH do
36
+ removeListener <- cp # once CPSafe .spawnH do
27
37
join $ Ref .read ref
28
- pid' <- pid cp
38
+ pid' <- CPSafe . pid cp
29
39
done $ Right $ Right $ unsafePartial $ fromJust pid'
30
40
Ref .write removeListener ref
31
41
pure $ effectCanceler do
32
42
removeListener
33
43
34
44
errored = makeAff \done -> do
35
45
ref <- Ref .new mempty
36
- removeListener <- cp # once CP .errorH \sysErr -> do
46
+ removeListener <- cp # once CPSafe .errorH \sysErr -> do
37
47
join $ Ref .read ref
38
48
done $ Right $ Left sysErr
39
49
Ref .write removeListener ref
0 commit comments