Skip to content

Commit

Permalink
Merge pull request #15 from garyb/export-cancelable
Browse files Browse the repository at this point in the history
Export makeAff'
  • Loading branch information
jdegoes committed Apr 14, 2015
2 parents 9d0987d + aef5f1b commit a8efcf2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
29 changes: 23 additions & 6 deletions MODULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
data Aff :: # ! -> * -> *
```

A computation with effects `e`. The computation either errors or
A computation with effects `e`. The computation either errors or
produces a value of type `a`.

This is moral equivalent of `ErrorT (ContT Unit (Eff e)) a`.
Expand All @@ -34,7 +34,7 @@ type Canceler e = Error -> Aff e Boolean
launchAff :: forall e a. Aff e a -> Eff e Unit
```

Converts the asynchronous computation into a synchronous one. All values
Converts the asynchronous computation into a synchronous one. All values
and errors are ignored.

#### `runAff`
Expand All @@ -43,7 +43,7 @@ and errors are ignored.
runAff :: forall e a. (Error -> Eff e Unit) -> (a -> Eff e Unit) -> Aff e a -> Eff e Unit
```

Runs the asynchronous computation. You must supply an error callback and a
Runs the asynchronous computation. You must supply an error callback and a
success callback.

#### `makeAff`
Expand All @@ -52,10 +52,20 @@ success callback.
makeAff :: forall e a. ((Error -> Eff e Unit) -> (a -> Eff e Unit) -> Eff e Unit) -> Aff e a
```

Creates an asynchronous effect from a function that accepts error and
Creates an asynchronous effect from a function that accepts error and
success callbacks. This function can be used for asynchronous computations
that cannot be canceled.

#### `makeAff'`

``` purescript
makeAff' :: forall e a. ((Error -> Eff e Unit) -> (a -> Eff e Unit) -> Eff e (Canceler e)) -> Aff e a
```

Creates an asynchronous effect from a function that accepts error and
success callbacks, and returns a canceler for the computation. This
function can be used for asynchronous computations that can be canceled.

#### `later`

``` purescript
Expand All @@ -78,7 +88,7 @@ Runs the asynchronous computation later (off the current execution context).
forkAff :: forall e a. Aff e a -> Aff e (Canceler e)
```

Forks the specified asynchronous computation so subsequent monadic binds
Forks the specified asynchronous computation so subsequent monadic binds
will not block on the result of the computation.

#### `attempt`
Expand Down Expand Up @@ -175,7 +185,7 @@ instance monadEffAff :: MonadEff e (Aff e)
instance monadErrorAff :: MonadError Error (Aff e)
```

Allows users to catch and throw errors on the error channel of the
Allows users to catch and throw errors on the error channel of the
asynchronous computation. See documentation in `purescript-transformers`.

#### `altAff`
Expand Down Expand Up @@ -209,6 +219,9 @@ instance monadPlusAff :: MonadPlus (Aff e)

## Module Control.Monad.Aff.AVar


A low-level primitive for building asynchronous code.

#### `AVAR`

``` purescript
Expand Down Expand Up @@ -300,6 +313,10 @@ instance monadAffAff :: MonadAff e (Aff e)

## Module Control.Monad.Aff.Par


A newtype over `Aff` that provides `Applicative` instances that run in
parallel.

#### `Par`

``` purescript
Expand Down
35 changes: 18 additions & 17 deletions src/Control/Monad/Aff.purs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Control.Monad.Aff
module Control.Monad.Aff
( Aff()
, Canceler()
, PureAff(..)
Expand All @@ -10,10 +10,11 @@ module Control.Monad.Aff
, launchAff
, liftEff'
, makeAff
, makeAff'
, nonCanceler
, runAff
)
where
where

import Data.Either(Either(..), either)
import Data.Function(Fn2(), Fn3(), runFn2, runFn3)
Expand All @@ -29,7 +30,7 @@ module Control.Monad.Aff
import Control.Monad.Eff.Class
import Control.Monad.Error.Class(MonadError, throwError)

-- | A computation with effects `e`. The computation either errors or
-- | A computation with effects `e`. The computation either errors or
-- | produces a value of type `a`.
-- |
-- | This is moral equivalent of `ErrorT (ContT Unit (Eff e)) a`.
Expand All @@ -40,24 +41,24 @@ module Control.Monad.Aff

type Canceler e = Error -> Aff e Boolean

-- | Converts the asynchronous computation into a synchronous one. All values
-- | Converts the asynchronous computation into a synchronous one. All values
-- | and errors are ignored.
launchAff :: forall e a. Aff e a -> Eff e Unit
launchAff = runAff (const (pure unit)) (const (pure unit))

-- | Runs the asynchronous computation. You must supply an error callback and a
-- | Runs the asynchronous computation. You must supply an error callback and a
-- | success callback.
runAff :: forall e a. (Error -> Eff e Unit) -> (a -> Eff e Unit) -> Aff e a -> Eff e Unit
runAff ex f aff = runFn3 _runAff ex f aff

-- | Creates an asynchronous effect from a function that accepts error and
-- | Creates an asynchronous effect from a function that accepts error and
-- | success callbacks. This function can be used for asynchronous computations
-- | that cannot be canceled.
makeAff :: forall e a. ((Error -> Eff e Unit) -> (a -> Eff e Unit) -> Eff e Unit) -> Aff e a
makeAff h = makeAff' (\e a -> const nonCanceler <$> h e a)

-- | Creates an asynchronous effect from a function that accepts error and
-- | success callbacks, and returns a canceler for the computation. This
-- | Creates an asynchronous effect from a function that accepts error and
-- | success callbacks, and returns a canceler for the computation. This
-- | function can be used for asynchronous computations that can be canceled.
makeAff' :: forall e a. ((Error -> Eff e Unit) -> (a -> Eff e Unit) -> Eff e (Canceler e)) -> Aff e a
makeAff' h = _makeAff h
Expand All @@ -70,7 +71,7 @@ module Control.Monad.Aff
later' :: forall e a. Number -> Aff e a -> Aff e a
later' n aff = runFn3 _setTimeout nonCanceler n aff

-- | Forks the specified asynchronous computation so subsequent monadic binds
-- | Forks the specified asynchronous computation so subsequent monadic binds
-- | will not block on the result of the computation.
forkAff :: forall e a. Aff e a -> Aff e (Canceler e)
forkAff aff = runFn2 _forkAff nonCanceler aff
Expand Down Expand Up @@ -114,7 +115,7 @@ module Control.Monad.Aff
instance monadEffAff :: MonadEff e (Aff e) where
liftEff eff = runFn2 _liftEff nonCanceler eff

-- | Allows users to catch and throw errors on the error channel of the
-- | Allows users to catch and throw errors on the error channel of the
-- | asynchronous computation. See documentation in `purescript-transformers`.
instance monadErrorAff :: MonadError Error (Aff e) where
throwError e = runFn2 _throwError nonCanceler e
Expand Down Expand Up @@ -149,7 +150,7 @@ module Control.Monad.Aff
return canceler(e)(success, error);
} else {
cancel = true;

clearTimeout(timeout);

try {
Expand Down Expand Up @@ -216,7 +217,7 @@ module Control.Monad.Aff
} catch (e) {
error(e);
}

return canceler;
}
}""" :: forall e a. Fn2 (Canceler e) a (Aff e a)
Expand All @@ -225,7 +226,7 @@ module Control.Monad.Aff
function _throwError(canceler, e) {
return function(success, error) {
error(e);

return canceler;
}
}""" :: forall e a. Fn2 (Canceler e) Error (Aff e a)
Expand All @@ -247,15 +248,15 @@ module Control.Monad.Aff
function _bind(aff, f) {
return function(success, error) {
var canceler;

canceler = aff(function(v) {
try {
try {
canceler = f(v)(success, error);
} catch (e) {
error(e);
}
}, error);

return function(e) {
return function(success, error) {
return canceler(e)(success, error);
Expand Down Expand Up @@ -306,7 +307,7 @@ module Control.Monad.Aff
} catch (e) {
error(e);
}

return canceler;
};
}""" :: forall e a. Fn2 (Canceler e) (Eff e a) (Aff e a)
Expand Down

0 comments on commit a8efcf2

Please sign in to comment.