@@ -2,14 +2,16 @@ module Test.Main where
22
33import Prelude
44
5- import Control.Monad.Aff (Aff (), runAff , later' )
5+ import Control.Monad.Aff (Aff (), runAff , later' , forkAll , cancel )
6+ import Control.Monad.Aff.AVar (AVAR (), makeVar' , modifyVar , takeVar )
67import Control.Monad.Cont.Class (callCC )
78import Control.Monad.Eff (Eff ())
89import Control.Monad.Eff.Class (liftEff )
910import Control.Monad.Eff.Console (CONSOLE (), log , print )
10- import Control.Monad.Eff.Exception (EXCEPTION (), throwException )
11+ import Control.Monad.Eff.Exception (EXCEPTION (), throwException , error )
1112import Control.Monad.Rec.Class (tailRecM )
1213
14+ import Data.Array (replicate )
1315import Data.Either (Either (..))
1416
1517loop :: forall eff . Int -> Aff (console :: CONSOLE | eff ) Unit
@@ -20,13 +22,28 @@ loop n = tailRecM go n
2022 return (Right unit)
2123 go n = return (Left (n - 1 ))
2224
25+ all :: forall eff . Int -> Aff (console :: CONSOLE , avar :: AVAR | eff ) Unit
26+ all n = do
27+ var <- makeVar' 0
28+ forkAll $ replicate n (modifyVar (+ 1 ) var)
29+ count <- takeVar var
30+ liftEff $ log (" Forked " <> show count)
31+
32+ cancelAll :: forall eff . Int -> Aff (console :: CONSOLE , avar :: AVAR | eff ) Unit
33+ cancelAll n = do
34+ canceler <- forkAll $ replicate n (later' 100000 (liftEff $ log " oops" ))
35+ canceled <- cancel canceler (error " bye" )
36+ liftEff $ log (" Cancelled all: " <> show canceled)
37+
2338delay :: forall eff . Int -> Aff eff Unit
2439delay n = callCC \cont ->
2540 later' n (cont unit)
2641
27- main :: Eff (console :: CONSOLE , err :: EXCEPTION ) Unit
42+ main :: Eff (console :: CONSOLE , avar :: AVAR , err :: EXCEPTION ) Unit
2843main = runAff throwException (const (pure unit)) $ do
2944 liftEff $ log " pre-delay"
3045 delay 1000
3146 liftEff $ log " post-delay"
3247 loop 1000000
48+ all 100000
49+ cancelAll 100000
0 commit comments