Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'delay' combinator #482

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions conduit/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ChangeLog for conduit

## 1.3.5

* Add `delay` [#481](https://github.com/snoyberg/conduit/issues/481)

## 1.3.4.2

* Fix GHC 9.2 build [#473](https://github.com/snoyberg/conduit/pull/473)
Expand Down
10 changes: 10 additions & 0 deletions conduit/src/Data/Conduit/Combinators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ module Data.Conduit.Combinators
, mapAccumS
, peekForever
, peekForeverE
, delay
) where

-- BEGIN IMPORTS
Expand All @@ -213,6 +214,7 @@ import Data.ByteString.Lazy.Internal (defaultChunkSize)
import Control.Applicative (Alternative(..), (<$>))
import Control.Exception (catch, throwIO, finally, bracket, try, evaluate)
import Control.Category (Category (..))
import Control.Concurrent (threadDelay)
import Control.Monad (unless, when, (>=>), liftM, forever)
import Control.Monad.IO.Unlift (MonadIO (..), MonadUnliftIO, withRunInIO)
import Control.Monad.Primitive (PrimMonad, PrimState, unsafePrimToPrim)
Expand Down Expand Up @@ -2554,3 +2556,11 @@ peekForeverE inner =
case mx of
Nothing -> return ()
Just _ -> inner >> loop

-- | Delays the emission of values for a given number of microseconds.
--
-- @since 1.3.5
delay :: MonadIO m => Int -> ConduitT a a m ()
delay n = awaitForever $ \v -> do
liftIO $ threadDelay n
yield v
1 change: 1 addition & 0 deletions conduit/src/Data/Conduit/Combinators/Unqualified.hs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ module Data.Conduit.Combinators.Unqualified
, CC.mapAccumS
, CC.peekForever
, CC.peekForeverE
, CC.delay
) where

-- BEGIN IMPORTS
Expand Down
15 changes: 13 additions & 2 deletions conduit/test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ module Spec (spec) where

import Conduit
import Prelude hiding (FilePath)
import Data.Maybe (listToMaybe)
import Data.Maybe (listToMaybe, isNothing)
import Data.Conduit.Combinators (slidingWindow, chunksOfE, chunksOfExactlyE)
import Data.List (intersperse, sort, find, mapAccumL)
import Safe (tailSafe)
import System.CPUTime (getCPUTime)
import System.FilePath (takeExtension, (</>))
import Test.Hspec
import Test.Hspec.QuickCheck
Expand All @@ -21,7 +22,7 @@ import Data.IORef
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Storable as VS
import Control.Monad (liftM)
import Control.Monad (liftM, when, void)
import Control.Monad.ST (runST)
import Control.Monad.Trans.Writer
import qualified System.IO as IO
Expand Down Expand Up @@ -637,6 +638,16 @@ spec = do
res1 <- runConduit $ yieldMany strs .| linesUnboundedC .| sinkList
res2 <- runConduit $ yieldMany strs .| peekForeverE (lineC $ foldC >>= yield) .| sinkList
res2 `shouldBe` res1
it "delay" $ do
let duration = 100000 :: Int
elapsed <- runConduit $ yield () .| delay duration .| do
start <- liftIO $ getCPUTime
mValue <- await
void $ when (isNothing mValue) $
error "expected 'Just ()'"
end <- liftIO $ getCPUTime
return (end - start)
elapsed `shouldSatisfy` (>= (toInteger duration))
StreamSpec.spec

evenInt :: Int -> Bool
Expand Down