Skip to content

Commit 3234825

Browse files
committed
add async trace and improve docs
1 parent 519bcfd commit 3234825

File tree

7 files changed

+182
-105
lines changed

7 files changed

+182
-105
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ module.exports = function(grunt) {
4747
grunt.loadNpmTasks('grunt-jsvalidate');
4848

4949
grunt.registerTask("make", ["pscMake", "jsvalidate", "dotPsci", "pscDocs"]);
50-
grunt.registerTask("test", ["jsvalidate", "psc"]);
50+
grunt.registerTask("test", ["jsvalidate", "psc", "pscDocs"]);
5151
grunt.registerTask("default", ["make"]);
5252
};

MODULES.md

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
data Aff :: # ! -> * -> *
99
```
1010

11-
A computation with effects `e`. The computation either errors or
12-
produces a value of type `a`.
11+
An asynchronous computation with effects `e`. The computation either
12+
errors or produces a value of type `a`.
1313

1414
This is moral equivalent of `ErrorT (ContT Unit (Eff e)) a`.
1515

@@ -19,7 +19,8 @@ This is moral equivalent of `ErrorT (ContT Unit (Eff e)) a`.
1919
type PureAff a = forall e. Aff e a
2020
```
2121

22-
A pure asynchronous computation, having no effects.
22+
A pure asynchronous computation, having no effects other than
23+
asynchronous computation.
2324

2425
#### `Canceler`
2526

@@ -30,7 +31,9 @@ newtype Canceler e
3031

3132
A canceler is asynchronous function that can be used to attempt the
3233
cancelation of a computation. Returns a boolean flag indicating whether
33-
or not the cancellation was successful.
34+
or not the cancellation was successful. Many computations may be composite,
35+
in such cases the flag indicates whether any part of the computation was
36+
successfully canceled. The flag should not be used for communication.
3437

3538
#### `cancel`
3639

@@ -102,17 +105,21 @@ Runs the asynchronous computation off the current execution context.
102105
later' :: forall e a. Number -> Aff e a -> Aff e a
103106
```
104107

105-
Runs the asynchronous computation later (off the current execution context).
108+
Runs the specified asynchronous computation later, by the specified
109+
number of milliseconds.
106110

107111
#### `forkAff`
108112

109113
``` purescript
110114
forkAff :: forall e a. Aff e a -> Aff e (Canceler e)
111115
```
112116

113-
Forks the specified asynchronous computation so subsequent monadic binds
117+
Forks the specified asynchronous computation so subsequent computations
114118
will not block on the result of the computation.
115119

120+
Returns a canceler that can be used to attempt cancellation of the
121+
forked computation.
122+
116123
#### `attempt`
117124

118125
``` purescript
@@ -143,7 +150,7 @@ Lifts a synchronous computation and makes explicit any failure from exceptions.
143150
nonCanceler :: forall e. Canceler e
144151
```
145152

146-
A constant function that always returns a pure false value.
153+
A constant canceller that always returns false.
147154

148155
#### `semigroupAff`
149156

@@ -344,6 +351,18 @@ instance monadAffAff :: MonadAff e (Aff e)
344351

345352

346353

354+
## Module Control.Monad.Aff.Debug.Trace
355+
356+
#### `trace`
357+
358+
``` purescript
359+
trace :: forall e a. (Show a) => a -> Aff (trace :: T.Trace | e) a
360+
```
361+
362+
Traces any `Show`-able value to the console. This basically saves you
363+
from writing `liftEff $ trace x` everywhere.
364+
365+
347366
## Module Control.Monad.Aff.Par
348367

349368
#### `Par`

examples/src/Examples.purs

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,148 +1,149 @@
11
module Examples where
2-
import Debug.Trace(trace, Trace())
2+
import Debug.Trace(Trace())
33

44
import Data.Either(either)
55

66
import Control.Monad.Aff
77
import Control.Monad.Aff.AVar
88
import Control.Monad.Aff.Par
9+
import Control.Monad.Aff.Debug.Trace(trace)
910
import Control.Apply((*>))
1011
import Control.Alt(Alt, (<|>))
1112
import Control.Monad.Eff.Class(liftEff)
1213
import Control.Monad.Eff.Exception(error)
1314
import Control.Monad.Error.Class(throwError)
1415

15-
type Test = forall e. Aff (trace :: Trace | e) Unit
16-
type TestAVar = forall e. Aff (trace :: Trace, avar :: AVAR | e) Unit
16+
type Test a = forall e. Aff (trace :: Trace | e) a
17+
type TestAVar a = forall e. Aff (trace :: Trace, avar :: AVAR | e) a
1718

18-
test_sequencing :: Number -> Test
19-
test_sequencing 0 = liftEff $ trace "Done"
19+
test_sequencing :: Number -> Test _
20+
test_sequencing 0 = trace "Done"
2021
test_sequencing n = do
21-
later' 100 (liftEff $ trace (show (n / 10) ++ " seconds left"))
22+
later' 100 (trace (show (n / 10) ++ " seconds left"))
2223
test_sequencing (n - 1)
2324

24-
test_pure :: Test
25+
test_pure :: Test _
2526
test_pure = do
2627
pure unit
2728
pure unit
2829
pure unit
29-
liftEff $ trace "Success: Got all the way past 4 pures"
30+
trace "Success: Got all the way past 4 pures"
3031

31-
test_attempt :: Test
32+
test_attempt :: Test _
3233
test_attempt = do
3334
e <- attempt (throwError (error "Oh noes!"))
34-
liftEff $ either (const $ trace "Success: Exception caught") (const $ trace "Failure: Exception NOT caught!!!") e
35+
either (const $ trace "Success: Exception caught") (const $ trace "Failure: Exception NOT caught!!!") e
3536

36-
test_apathize :: Test
37+
test_apathize :: Test _
3738
test_apathize = do
3839
apathize $ throwError (error "Oh noes!")
39-
liftEff $ trace "Success: Exceptions don't stop the apathetic"
40+
trace "Success: Exceptions don't stop the apathetic"
4041

41-
test_putTakeVar :: TestAVar
42+
test_putTakeVar :: TestAVar _
4243
test_putTakeVar = do
4344
v <- makeVar
4445
forkAff (later $ putVar v 1.0)
4546
a <- takeVar v
46-
liftEff $ trace ("Success: Value " ++ show a)
47+
trace ("Success: Value " ++ show a)
4748

48-
test_killFirstForked :: Test
49+
test_killFirstForked :: Test _
4950
test_killFirstForked = do
5051
c <- forkAff (later' 100 $ pure "Failure: This should have been killed!")
5152
b <- c `cancel` (error "Just die")
52-
liftEff $ trace (if b then "Success: Killed first forked" else "Failure: Couldn't kill first forked")
53+
trace (if b then "Success: Killed first forked" else "Failure: Couldn't kill first forked")
5354

5455

55-
test_killVar :: TestAVar
56+
test_killVar :: TestAVar _
5657
test_killVar = do
5758
v <- makeVar
5859
killVar v (error "DOA")
5960
e <- attempt $ takeVar v
60-
liftEff $ either (const $ trace "Success: Killed queue dead") (const $ trace "Failure: Oh noes, queue survived!") e
61+
either (const $ trace "Success: Killed queue dead") (const $ trace "Failure: Oh noes, queue survived!") e
6162

62-
test_parRace :: TestAVar
63+
test_parRace :: TestAVar _
6364
test_parRace = do
6465
s <- runPar (Par (later' 100 $ pure "Success: Early bird got the worm") <|>
6566
Par (later' 200 $ pure "Failure: Late bird got the worm"))
66-
liftEff $ trace s
67+
trace s
6768

68-
test_parRaceKill1 :: TestAVar
69+
test_parRaceKill1 :: TestAVar _
6970
test_parRaceKill1 = do
7071
s <- runPar (Par (later' 100 $ throwError (error ("Oh noes!"))) <|>
7172
Par (later' 200 $ pure "Success: Early error was ignored in favor of late success"))
72-
liftEff $ trace s
73+
trace s
7374

74-
test_parRaceKill2 :: TestAVar
75+
test_parRaceKill2 :: TestAVar _
7576
test_parRaceKill2 = do
7677
e <- attempt $ runPar (Par (later' 100 $ throwError (error ("Oh noes!"))) <|>
7778
Par (later' 200 $ throwError (error ("Oh noes!"))))
78-
liftEff $ either (const $ trace "Success: Killing both kills it dead") (const $ trace "Failure: It's alive!!!") e
79+
either (const $ trace "Success: Killing both kills it dead") (const $ trace "Failure: It's alive!!!") e
7980

80-
test_semigroupCanceler :: Test
81+
test_semigroupCanceler :: Test _
8182
test_semigroupCanceler =
8283
let
8384
c = Canceler (const (pure true)) <> Canceler (const (pure true))
8485
in do
8586
v <- cancel c (error "CANCEL")
86-
liftEff $ trace (if v then "Success: Canceled semigroup composite canceler"
87+
trace (if v then "Success: Canceled semigroup composite canceler"
8788
else "Failure: Could not cancel semigroup composite canceler")
8889

89-
test_cancelLater :: TestAVar
90+
test_cancelLater :: TestAVar _
9091
test_cancelLater = do
9192
c <- forkAff $ (do pure "Binding"
92-
_ <- later' 100 $ liftEff $ trace ("Failure: Later was not canceled!")
93+
_ <- later' 100 $ trace ("Failure: Later was not canceled!")
9394
pure "Binding")
9495
v <- cancel c (error "Cause")
95-
liftEff $ trace (if v then "Success: Canceled later" else "Failure: Did not cancel later")
96+
trace (if v then "Success: Canceled later" else "Failure: Did not cancel later")
9697

97-
test_cancelPar :: TestAVar
98+
test_cancelPar :: TestAVar _
9899
test_cancelPar = do
99-
c <- forkAff <<< runPar $ Par (later' 100 $ liftEff $ trace "Failure: #1 should not get through") <|>
100-
Par (later' 100 $ liftEff $ trace "Failure: #2 should not get through")
100+
c <- forkAff <<< runPar $ Par (later' 100 $ trace "Failure: #1 should not get through") <|>
101+
Par (later' 100 $ trace "Failure: #2 should not get through")
101102
v <- c `cancel` (error "Must cancel")
102-
liftEff $ trace (if v then "Success: Canceling composite of two Par succeeded"
103+
trace (if v then "Success: Canceling composite of two Par succeeded"
103104
else "Failure: Canceling composite of two Par failed")
104105

105106
main = launchAff $ do
106-
liftEff $ trace "Testing sequencing"
107+
trace "Testing sequencing"
107108
test_sequencing 3
108109

109-
liftEff $ trace "Testing pure"
110+
trace "Testing pure"
110111
test_pure
111112

112-
liftEff $ trace "Testing attempt"
113+
trace "Testing attempt"
113114
test_attempt
114115

115-
liftEff $ trace "Testing later"
116-
later $ liftEff $ trace "Success: It happened later"
116+
trace "Testing later"
117+
later $ trace "Success: It happened later"
117118

118-
liftEff $ trace "Testing kill of later"
119+
trace "Testing kill of later"
119120
test_cancelLater
120121

121-
liftEff $ trace "Testing kill of first forked"
122+
trace "Testing kill of first forked"
122123
test_killFirstForked
123124

124-
liftEff $ trace "Testing apathize"
125+
trace "Testing apathize"
125126
test_apathize
126127

127-
liftEff $ trace "Testing semigroup canceler"
128+
trace "Testing semigroup canceler"
128129
test_semigroupCanceler
129130

130-
liftEff $ trace "Testing AVar - putVar, takeVar"
131+
trace "Testing AVar - putVar, takeVar"
131132
test_putTakeVar
132133

133-
liftEff $ trace "Testing AVar killVar"
134+
trace "Testing AVar killVar"
134135
test_killVar
135136

136-
liftEff $ trace "Testing Par (<|>)"
137+
trace "Testing Par (<|>)"
137138
test_parRace
138139

139-
liftEff $ trace "Testing Par (<|>) - kill one"
140+
trace "Testing Par (<|>) - kill one"
140141
test_parRaceKill1
141142

142-
liftEff $ trace "Testing Par (<|>) - kill two"
143+
trace "Testing Par (<|>) - kill two"
143144
test_parRaceKill2
144145

145-
liftEff $ trace "Testing cancel of Par (<|>)"
146+
trace "Testing cancel of Par (<|>)"
146147
test_cancelPar
147148

148-
liftEff $ trace "Done testing"
149+
trace "Done testing"

0 commit comments

Comments
 (0)