|
| 1 | +module Node.ChildProcess.Aff where |
| 2 | + |
| 3 | +import Prelude |
| 4 | + |
| 5 | +import Control.Parallel (parOneOf) |
| 6 | +import Data.Either (Either(..)) |
| 7 | +import Data.Maybe (fromJust) |
| 8 | +import Data.Posix (Pid) |
| 9 | +import Effect.Aff (Aff, effectCanceler, makeAff) |
| 10 | +import Effect.Ref as Ref |
| 11 | +import Node.ChildProcess (ChildProcess, pid) |
| 12 | +import Node.ChildProcess as CP |
| 13 | +import Node.Errors.SystemError (SystemError) |
| 14 | +import Node.EventEmitter (once) |
| 15 | +import Partial.Unsafe (unsafePartial) |
| 16 | + |
| 17 | +-- | Blocks until either a `spawn` or `error` event is fired. |
| 18 | +-- | If a `spawn` event fired, child process was successfully started |
| 19 | +-- | and the `pid` of the process can be obtained. |
| 20 | +-- | If an `error` event fires, child process was not started successfully. |
| 21 | +waitSpawned :: ChildProcess -> Aff (Either SystemError Pid) |
| 22 | +waitSpawned cp = parOneOf [ pidOnSpawn, errored ] |
| 23 | + where |
| 24 | + pidOnSpawn = makeAff \done -> do |
| 25 | + ref <- Ref.new mempty |
| 26 | + removeListener <- cp # once CP.spawnH do |
| 27 | + join $ Ref.read ref |
| 28 | + pid' <- pid cp |
| 29 | + done $ Right $ Right $ unsafePartial $ fromJust pid' |
| 30 | + Ref.write removeListener ref |
| 31 | + pure $ effectCanceler do |
| 32 | + removeListener |
| 33 | + |
| 34 | + errored = makeAff \done -> do |
| 35 | + ref <- Ref.new mempty |
| 36 | + removeListener <- cp # once CP.errorH \sysErr -> do |
| 37 | + join $ Ref.read ref |
| 38 | + done $ Right $ Left sysErr |
| 39 | + Ref.write removeListener ref |
| 40 | + pure $ effectCanceler do |
| 41 | + removeListener |
0 commit comments