Skip to content

Commit fe46347

Browse files
committed
more tests, bug fixes
1 parent b39e067 commit fe46347

File tree

5 files changed

+278
-61
lines changed

5 files changed

+278
-61
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ do e <- liftEff' myExcFunc
100100
liftEff $ either (const $ trace "Oh noes!") (const $ trace "Yays!") e
101101
```
102102

103-
## Failure
103+
## Dealing with Failure
104104

105105
The `Aff` monad has error handling baked in, so ordinarily you don't have to worry about it.
106106

@@ -150,7 +150,7 @@ block the current thread of execution:
150150
forkAff myAff
151151
```
152152

153-
Because Javascript is single-threaded, forking does not actually cause the computation to be run in a separate thread, it merely means the subsequent chain of computations will be executed without first waiting for the forked computation to complete.
153+
Because Javascript is single-threaded, forking does not actually cause the computation to be run in a separate thread. Forking just allows the subsequent actions to execute without waiting for the forked computation to complete.
154154

155155
## Variables
156156

@@ -159,9 +159,9 @@ be used as low-level building blocks for asynchronous programs.
159159

160160
```purescript
161161
do v <- makeVar
162-
forkAff (myRunner v)
163-
a <- takeVar
164-
return a
162+
forkAff (later $ putVar v 1.0)
163+
a <- takeVar v
164+
liftEff $ trace ("Succeeded with " ++ show a)
165165
```
166166

167167
# Documentation

examples/src/Examples.purs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Examples where
44
import Data.Either(either)
55

66
import Control.Monad.Aff
7+
import Control.Monad.Aff.Var
78
import Control.Monad.Eff.Class(liftEff)
89
import Control.Monad.Eff.Exception(error)
910
import Control.Monad.Error.Class(throwError)
@@ -28,16 +29,39 @@ module Examples where
2829
}
2930
""" :: forall e. Number -> Aff (time :: Time | e) Unit
3031

32+
type Test = forall e. Aff (trace :: Trace | e) Unit
33+
type TestVar = forall e. Aff (trace :: Trace, var :: VarFx | e) Unit
34+
35+
test_sequencing :: forall e. Number -> Aff (trace :: Trace, time :: Time | e) Unit
3136
test_sequencing 0 = liftEff $ trace "Done"
3237
test_sequencing n = do
3338
timeout 1000
3439
liftEff $ trace (show n ++ " seconds left")
3540
test_sequencing (n - 1)
3641

37-
test_attempt :: forall e. Aff (trace :: Trace | e) Unit
42+
test_attempt :: Test
3843
test_attempt = do
3944
e <- attempt (throwError (error "Oh noes!"))
40-
liftEff $ either (const $ trace "Exception caught") (const $ trace "Exception NOT caught!!!") e
45+
liftEff $ either (const $ trace "Success: Exception caught") (const $ trace "Failure: Exception NOT caught!!!") e
46+
47+
test_apathize :: Test
48+
test_apathize = do
49+
apathize $ throwError (error "Oh noes!")
50+
liftEff $ trace "Success: Didn't care about return value"
51+
52+
test_putTakeVar :: TestVar
53+
test_putTakeVar = do
54+
v <- makeVar
55+
forkAff (later $ putVar v 1.0)
56+
a <- takeVar v
57+
liftEff $ trace ("Success: Value " ++ show a)
58+
59+
test_killVar :: TestVar
60+
test_killVar = do
61+
v <- makeVar
62+
killVar v (error "DOA")
63+
e <- attempt $ takeVar v
64+
liftEff $ either (const $ trace "Success: Killed var dead") (const $ trace "Failure: Oh noes, Var survived!") e
4165

4266
main = launchAff $ do
4367
liftEff $ trace "Testing sequencing"
@@ -47,6 +71,13 @@ module Examples where
4771
test_attempt
4872

4973
liftEff $ trace "Testing later"
50-
later $ liftEff $ trace "It happend later"
74+
later $ liftEff $ trace "Success: It happened later"
75+
76+
liftEff $ trace "Testing apathize"
77+
test_apathize
78+
79+
liftEff $ trace "Testing Var - putVar, takeVar"
80+
test_putTakeVar
5181

52-
82+
liftEff $ trace "Testing killVar"
83+
test_killVar

0 commit comments

Comments
 (0)