Skip to content

Commit 5d50182

Browse files
committed
Fixes #16
1 parent 9d0987d commit 5d50182

File tree

4 files changed

+56
-24
lines changed

4 files changed

+56
-24
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ using the returned canceler:
167167

168168
```purescript
169169
canceler <- forkAff myAff
170-
canceled <- canceler $ error "Just had to cancel"
170+
canceled <- canceler `cancel` (error "Just had to cancel")
171171
_ <- liftEff $ if canceled then (trace "Canceled") else (trace "Not Canceled")
172172
```
173173

examples/src/Examples.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ module Examples where
4848
test_killFirstForked :: Test
4949
test_killFirstForked = do
5050
c <- forkAff (later' 100 $ pure "Failure: This should have been killed!")
51-
b <- c (error "Just die")
51+
b <- c `cancel` (error "Just die")
5252
liftEff $ trace (if b then "Success: Killed first forked" else "Failure: Couldn't kill first forked")
5353

5454

output/examples.js

Lines changed: 36 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Control/Monad/Aff.purs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module Control.Monad.Aff
22
( Aff()
3-
, Canceler()
3+
, Canceler(..)
44
, PureAff(..)
55
, apathize
66
, attempt
7+
, cancel
78
, forkAff
89
, later
910
, later'
@@ -38,7 +39,14 @@ module Control.Monad.Aff
3839
-- | A pure asynchronous computation, having no effects.
3940
type PureAff a = forall e. Aff e a
4041

41-
type Canceler e = Error -> Aff e Boolean
42+
-- | A canceler is asynchronous function that can be used to attempt the
43+
-- | cancelation of a computation. Returns a boolean flag indicating whether
44+
-- | or not the cancellation was successful.
45+
newtype Canceler e = Canceler (Error -> Aff e Boolean)
46+
47+
-- | Unwraps the canceler function from the newtype that wraps it.
48+
cancel :: forall e. Canceler e -> Error -> Aff e Boolean
49+
cancel (Canceler f) = f
4250

4351
-- | Converts the asynchronous computation into a synchronous one. All values
4452
-- | and errors are ignored.
@@ -89,7 +97,7 @@ module Control.Monad.Aff
8997

9098
-- | A constant function that always returns a pure false value.
9199
nonCanceler :: forall e. Canceler e
92-
nonCanceler = const (pure false)
100+
nonCanceler = Canceler (const (pure false))
93101

94102
instance semigroupAff :: (Semigroup a) => Semigroup (Aff e a) where
95103
(<>) a b = (<>) <$> a <*> b
@@ -131,6 +139,12 @@ module Control.Monad.Aff
131139

132140
instance monadPlusAff :: MonadPlus (Aff e)
133141

142+
instance semigroupCanceler :: Semigroup (Canceler e) where
143+
(<>) (Canceler f1) (Canceler f2) = Canceler (\e -> (&&) <$> f1 e <*> f2 e)
144+
145+
instance monoidCanceler :: Monoid (Canceler e) where
146+
mempty = Canceler (const (pure true))
147+
134148
foreign import _setTimeout """
135149
function _setTimeout(nonCanceler, millis, aff) {
136150
return function(success, error) {
@@ -149,7 +163,7 @@ module Control.Monad.Aff
149163
return canceler(e)(success, error);
150164
} else {
151165
cancel = true;
152-
166+
153167
clearTimeout(timeout);
154168
155169
try {

0 commit comments

Comments
 (0)