@@ -2,14 +2,16 @@ module Test.Main where
2
2
3
3
import Prelude
4
4
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 )
6
7
import Control.Monad.Cont.Class (callCC )
7
8
import Control.Monad.Eff (Eff ())
8
9
import Control.Monad.Eff.Class (liftEff )
9
10
import Control.Monad.Eff.Console (CONSOLE (), log , print )
10
- import Control.Monad.Eff.Exception (EXCEPTION (), throwException )
11
+ import Control.Monad.Eff.Exception (EXCEPTION (), throwException , error )
11
12
import Control.Monad.Rec.Class (tailRecM )
12
13
14
+ import Data.Array (replicate )
13
15
import Data.Either (Either (..))
14
16
15
17
loop :: forall eff . Int -> Aff (console :: CONSOLE | eff ) Unit
@@ -20,13 +22,28 @@ loop n = tailRecM go n
20
22
return (Right unit)
21
23
go n = return (Left (n - 1 ))
22
24
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
+
23
38
delay :: forall eff . Int -> Aff eff Unit
24
39
delay n = callCC \cont ->
25
40
later' n (cont unit)
26
41
27
- main :: Eff (console :: CONSOLE , err :: EXCEPTION ) Unit
42
+ main :: Eff (console :: CONSOLE , avar :: AVAR , err :: EXCEPTION ) Unit
28
43
main = runAff throwException (const (pure unit)) $ do
29
44
liftEff $ log " pre-delay"
30
45
delay 1000
31
46
liftEff $ log " post-delay"
32
47
loop 1000000
48
+ all 100000
49
+ cancelAll 100000
0 commit comments