11module 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