@@ -39,6 +39,7 @@ import Data.Foldable (class Foldable, foldl)
39
39
import Data.Function.Uncurried (Fn2 , Fn3 , runFn2 , runFn3 )
40
40
import Data.Monoid (class Monoid , mempty )
41
41
import Data.Newtype (class Newtype )
42
+ import Data.Tuple (Tuple (..), fst , snd )
42
43
43
44
import Unsafe.Coerce (unsafeCoerce )
44
45
@@ -244,11 +245,18 @@ instance altParAff :: Alt (ParAff e) where
244
245
alt (ParAff a1) (ParAff a2) = ParAff do
245
246
va <- makeVar -- the `a` value
246
247
ve <- makeVar -- the error count (starts at 0)
248
+ cs <- makeVar -- the cancelers
247
249
putVar ve 0
248
- c1 <- forkAff $ either (maybeKill va ve) (putVar va) =<< attempt a1
249
- c2 <- forkAff $ either (maybeKill va ve) (putVar va) =<< attempt a2
250
+ c1 <- forkAff $ either (maybeKill va ve) (done cs snd va) =<< attempt a1
251
+ c2 <- forkAff $ either (maybeKill va ve) (done cs fst va) =<< attempt a2
252
+ putVar cs (Tuple c1 c2)
250
253
takeVar va `cancelWith` (c1 <> c2)
251
254
where
255
+ done :: forall a . AVar (Tuple (Canceler e ) (Canceler e )) -> (forall x . Tuple x x -> x ) -> AVar a -> a -> Aff e Unit
256
+ done cs get va x = do
257
+ putVar va x
258
+ c <- get <$> takeVar cs
259
+ void $ cancel c (error " Alt early exit" )
252
260
maybeKill :: forall a . AVar a -> AVar Int -> Error -> Aff e Unit
253
261
maybeKill va ve err = do
254
262
e <- takeVar ve
0 commit comments