Skip to content

Commit 7428d85

Browse files
committed
Merge pull request #56 from spencerjanssen/par_error
Propagate Par errors
2 parents 406d6cf + 45535d5 commit 7428d85

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/Control/Monad/Aff/Par.purs

+7-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import Prelude
1111
import Control.Alt (Alt)
1212
import Control.Alternative (Alternative)
1313
import Control.Monad.Aff (attempt, cancelWith, forkAff)
14-
import Control.Monad.Aff.AVar (AffAVar(), makeVar, makeVar', takeVar, putVar, killVar)
14+
import Control.Monad.Aff.AVar (AffAVar(), AVar(), makeVar, makeVar', takeVar, putVar, killVar)
15+
import Control.Monad.Eff.Exception (Error())
1516
import Control.Plus (Plus, empty)
1617

17-
import Data.Either (either)
18+
import Data.Either (Either(), either)
1819
import Data.Monoid (Monoid, mempty)
1920

2021
newtype Par e a = Par (AffAVar e a)
@@ -34,10 +35,12 @@ instance functorPar :: Functor (Par e) where
3435

3536
instance applyPar :: Apply (Par e) where
3637
apply (Par ff) (Par fa) = Par do
38+
let putOrKill :: forall b. AVar b -> Either Error b -> AffAVar e Unit
39+
putOrKill v = either (killVar v) (putVar v)
3740
vf <- makeVar
3841
va <- makeVar
39-
c1 <- forkAff (ff >>= putVar vf)
40-
c2 <- forkAff (fa >>= putVar va)
42+
c1 <- forkAff (attempt ff >>= putOrKill vf)
43+
c2 <- forkAff (attempt fa >>= putOrKill va)
4144
(takeVar vf <*> takeVar va) `cancelWith` (c1 <> c2)
4245

4346
instance applicativePar :: Applicative (Par e) where

test/Test/Main.purs

+8
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ test_parRace = do
8686
Par (later' 200 $ pure "Failure: Late bird got the worm"))
8787
log s
8888

89+
test_parError :: TestAVar Unit
90+
test_parError = do
91+
e <- attempt $ runPar (Par (throwError (error ("Oh noes!"))) *> pure unit)
92+
either (const $ log "Success: Exception propagated") (const $ log "Failure: Exception missing") e
93+
8994
test_parRaceKill1 :: TestAVar Unit
9095
test_parRaceKill1 = do
9196
s <- runPar (Par (later' 100 $ throwError (error ("Oh noes!"))) <|>
@@ -199,6 +204,9 @@ main = runAff throwException (const (pure unit)) $ do
199204
log "Testing finally"
200205
test_finally
201206

207+
log "Test Par (*>)"
208+
test_parError
209+
202210
log "Testing Par (<|>)"
203211
test_parRace
204212

0 commit comments

Comments
 (0)