@@ -8,8 +8,9 @@ import Data.Maybe (fromJust)
88import Data.Posix (Pid )
99import Effect.Aff (Aff , effectCanceler , makeAff )
1010import 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 )
1314import Node.Errors.SystemError (SystemError )
1415import Node.EventEmitter (once )
1516import Partial.Unsafe (unsafePartial )
@@ -19,21 +20,30 @@ import Partial.Unsafe (unsafePartial)
1920-- | and the `pid` of the process can be obtained.
2021-- | If an `error` event fires, child process was not started successfully.
2122waitSpawned :: 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 ]
2333 where
2434 pidOnSpawn = makeAff \done -> do
2535 ref <- Ref .new mempty
26- removeListener <- cp # once CP .spawnH do
36+ removeListener <- cp # once CPSafe .spawnH do
2737 join $ Ref .read ref
28- pid' <- pid cp
38+ pid' <- CPSafe . pid cp
2939 done $ Right $ Right $ unsafePartial $ fromJust pid'
3040 Ref .write removeListener ref
3141 pure $ effectCanceler do
3242 removeListener
3343
3444 errored = makeAff \done -> do
3545 ref <- Ref .new mempty
36- removeListener <- cp # once CP .errorH \sysErr -> do
46+ removeListener <- cp # once CPSafe .errorH \sysErr -> do
3747 join $ Ref .read ref
3848 done $ Right $ Left sysErr
3949 Ref .write removeListener ref
0 commit comments