@@ -10,6 +10,16 @@
1010{-# LANGUAGE TypeFamilies #-}
1111{-# LANGUAGE UndecidableInstances #-}
1212
13+-- |
14+-- Module : Asyncly.AsyncT
15+-- Copyright : (c) 2017 Harendra Kumar
16+--
17+-- License : MIT-style
18+-- Maintainer : harendra.kumar@gmail.com
19+-- Stability : experimental
20+-- Portability : GHC
21+--
22+--
1323module Asyncly.AsyncT
1424 ( AsyncT (..)
1525 , MonadAsync
@@ -27,11 +37,11 @@ module Asyncly.AsyncT
2737 , makeAsync
2838 , each
2939
30− , discardAndThen
40+ , before
3141 , (*>>)
32− , thenDiscard
42+ , thereafter
3343 , (>>*)
34− , afterFirst
44+ , afterfirst
3545 , (>>|)
3646
3747 -- internal
@@ -438,10 +448,11 @@ runAsyncTask forceAsync action = do
438448-- action was an infinite loop for example.
439449--
440450-- | In an 'Alternative' composition, force the action to run asynchronously.
441−-- The <|> operator implies "can be parallel", whereas async implies "must be
442−-- parallel". Note that async is not useful and should never be used outside an
443−-- 'Alternative' composition. Even in an 'Alternative' composition 'async' is
444−-- not useful in the last action.
451+-- The @\<|\>@ operator implies "can be parallel", whereas 'async' implies
452+-- "must be parallel". Note that outside an 'Alternative' composition 'async'
453+-- is not useful and should not be used. Even in an 'Alternative' composition
454+-- 'async' is not useful as the last action as the last action always runs in
455+-- the current thread.
445456async :: MonadAsync m => AsyncT m a -> AsyncT m a
446457async action = AsyncT $ runAsyncTask True action
447458
@@ -477,9 +488,9 @@ each xs = foldl (<|>) empty $ map return xs
477488------------------------------------------------------------------------------
478489
479490-- XXX Should n be Word32 instead?
480−-- | Runs a computation under a given thread limit. A limit of 0 means new
481−-- tasks start synchronously in the current thread. New threads are created by
482−-- 'parallel', and APIs that use parallel.
491+-- | Runs a computation under a given thread limit. A limit of 0 means all new
492+-- tasks start synchronously in the current thread unless overridden by
493+-- 'async'.
483494threads :: MonadAsync m => Int -> AsyncT m a -> AsyncT m a
484495threads n process = AsyncT $ do
485496 oldCr <- gets threadCredit
@@ -563,44 +574,40 @@ instance (Num a, Monad (AsyncT m)) => Num (AsyncT m a) where
563574infixr 1 >>*, *>>, >>|
564575
565576-- XXX This can be moved to utility functions as it is purely app level
566−-- | Run @b@ once, discarding its result when the first task in task set @a@
567−-- has finished. Useful to start a singleton task after the first task has been
568−-- setup.
569−afterFirst :: MonadAsync m => AsyncT m a -> AsyncT m b -> AsyncT m a
570−afterFirst ma mb = do
577+-- | Run @m b@ right after the first event in @m a@ is generated but before it
578+-- is yielded. The result of @m b@ is discarded.
579+afterfirst :: MonadAsync m => AsyncT m a -> AsyncT m b -> AsyncT m a
580+afterfirst ma mb = do
571581 ref <- liftIO $ newIORef False
572582 x <- ma
573583 done <- liftIO $ readIORef ref
574584 when (not done) $ (liftIO $ writeIORef ref True) >>* mb
575585 return x
576586
587+-- | Same as 'afterfirst'.
577588(>>|) :: MonadAsync m => AsyncT m a -> AsyncT m b -> AsyncT m a
578−(>>|) = afterFirst
589+(>>|) = afterfirst
579590
580−-- | Run 'm a' in "isolation" and discard its result, and then run 'm b' and
581−-- return its result. Isolation means that any alternative actions inside 'm
582−-- a' are not continued to 'm b'.
583−discardAndThen :: MonadAsync m => AsyncT m a -> AsyncT m b -> AsyncT m b
584−discardAndThen ma mb = AsyncT $ do
591+-- | Run @m a@ before running @m b@. The result of @m a@ is discarded.
592+before :: MonadAsync m => AsyncT m a -> AsyncT m b -> AsyncT m b
593+before ma mb = AsyncT $ do
585594 _ <- runAsyncT (ma >> mzero)
586595 runAsyncT mb
587596
588−-- | Same as 'discardAndThen'.
597+-- | Same as 'before'.
589598(*>>) :: MonadAsync m => AsyncT m a -> AsyncT m b -> AsyncT m b
590−(*>>) = discardAndThen
599+(*>>) = before
591600
592−-- | Run 'm a' and then run 'm b' in "isolation" and return the result of 'm
593−-- a'. Isolation means that any alternative actions inside 'm a' are not
594−-- continued to 'm b' and the results of 'm b' are discarded.
595−thenDiscard :: MonadAsync m => AsyncT m a -> AsyncT m b -> AsyncT m a
596−thenDiscard ma mb = AsyncT $ do
601+-- | Run @m b@ after running @m a@. The result of @m b@ is discarded.
602+thereafter :: MonadAsync m => AsyncT m a -> AsyncT m b -> AsyncT m a
603+thereafter ma mb = AsyncT $ do
597604 a <- runAsyncT ma
598605 _ <- runAsyncT (mb >> mzero)
599606 return a
600607
601−-- | Same as 'thenDiscard'.
608+-- | Same as 'thereafter'.
602609(>>*) :: MonadAsync m => AsyncT m a -> AsyncT m b -> AsyncT m a
603−(>>*) = thenDiscard
610+(>>*) = thereafter
604611
605612-------------------------------------------------------------------------------
606613-- AsyncT transformer