@@ -57,9 +57,12 @@ module Streamly.Internal.Data.Stream.Time
5757 )
5858where
5959
60+ #include "inline.hs"
61+
6062import Control.Exception (assert )
6163import Control.Monad.IO.Class (MonadIO (.. ))
6264import Data.Heap (Entry (.. ))
65+ import Data.Int (Int64 )
6366import Data.Kind (Type )
6467import Data.Map (Map )
6568import Data.Maybe (isJust , isNothing )
@@ -70,7 +73,7 @@ import Streamly.Internal.Data.Array (Array)
7073import Streamly.Internal.Data.Fold (Fold (.. ))
7174import Streamly.Internal.Data.IsMap (IsMap (.. ))
7275import Streamly.Internal.Data.Channel.Types (Rate )
73- import Streamly.Internal.Data.Stream (Stream )
76+ import Streamly.Internal.Data.Stream (Stream , Step ( .. ) )
7477import Streamly.Internal.Data.Stream.Channel.Type (rate )
7578import Streamly.Internal.Data.Time.Units
7679 ( AbsTime
@@ -79,7 +82,9 @@ import Streamly.Internal.Data.Time.Units
7982 , toAbsTime
8083 , toRelTime
8184 )
82- import Streamly.Internal.Data.Time.Units (NanoSecond64 (.. ), toRelTime64 )
85+ import Streamly.Internal.Data.Time.Clock (Clock (Monotonic ), getTime )
86+ import Streamly.Internal.Data.Time.Units
87+ (NanoSecond64 (.. ), addToAbsTime64 , toRelTime64 )
8388
8489import qualified Data.Heap as H
8590import qualified Streamly.Internal.Data.Array as Array
@@ -90,7 +95,8 @@ import qualified Streamly.Data.Unfold as Unfold
9095import qualified Streamly.Internal.Data.Fold as Fold (Step (.. ))
9196import qualified Streamly.Internal.Data.IsMap as IsMap
9297import qualified Streamly.Internal.Data.Stream as Stream
93- ( scanlMAfter'
98+ ( Stream (Stream )
99+ , scanlMAfter'
94100 , sleep
95101 , timeIndexed
96102 , timestamped
@@ -179,19 +185,23 @@ ticksRate r = parBuffered (rate (Just r)) $ Stream.repeatM (return ())
179185interject :: MonadAsync m => m a -> Double -> Stream m a -> Stream m a
180186interject f n xs = parListEagerFst [xs, periodic f n]
181187
182- -- XXX No element should be yielded if the duration is zero.
183-
184188-- | @takeInterval interval@ runs the stream only upto the specified time
185- -- @interval@ in seconds.
189+ -- @interval@ in seconds. No element is yielded if the interval is zero,
190+ -- negative or NaN.
186191--
187192-- The interval starts when the stream is evaluated for the first time.
188193--
189194{-# INLINE takeInterval #-}
190195takeInterval :: MonadAsync m => Double -> Stream m a -> Stream m a
191- takeInterval d =
192- Stream. catMaybes
193- . Stream. takeWhile isJust
194- . interject (return Nothing ) d . fmap Just
196+ takeInterval d xs
197+ | not (d > 0 ) = Stream. nil
198+ | otherwise =
199+ Stream. catMaybes
200+ $ Stream. takeWhile isJust
201+ $ parListEagerFst
202+ [ fmap Just xs
203+ , Stream. fromEffect (Stream. sleep d >> return Nothing )
204+ ]
195205
196206-- | Take time interval @i@ seconds at the end of the stream.
197207--
@@ -203,19 +213,68 @@ takeLastInterval :: -- MonadAsync m =>
203213 Double -> Stream m a -> Stream m a
204214takeLastInterval = undefined
205215
206- -- XXX All elements should be yielded if the duration is zero.
216+ data DropIntervalState s
217+ = DropIntervalInit s
218+ | DropIntervalDrop ! AbsTime s
219+ | DropIntervalDropAll s
220+ | DropIntervalYield s
207221
208222-- | @dropInterval interval@ drops all the stream elements that are generated
209- -- before the specified @interval@ in seconds has passed.
223+ -- before the specified @interval@ in seconds has passed. No element is dropped
224+ -- if the interval is zero, negative or NaN. All the elements are dropped if the
225+ -- interval is too large to be represented in nanoseconds as an 'Int64'.
210226--
211227-- The interval begins when the stream is evaluated for the first time.
212228--
229+ -- An element is dropped or yielded depending on the time at which it is
230+ -- generated, not on the time at which the action generating it starts.
231+ --
213232{-# INLINE dropInterval #-}
214- dropInterval :: MonadAsync m => Double -> Stream m a -> Stream m a
215- dropInterval d =
216- Stream. catMaybes
217- . Stream. dropWhile isJust
218- . interject (return Nothing ) d . fmap Just
233+ dropInterval :: MonadIO m => Double -> Stream m a -> Stream m a
234+ dropInterval d (Stream. Stream step state) =
235+ Stream. Stream step1 (DropIntervalInit state)
236+
237+ where
238+
239+ -- The interval in nanoseconds must fit in an Int64, with a margin of one
240+ -- second as it is added to the nanoseconds of the current time.
241+ maxInterval :: Double
242+ maxInterval = fromIntegral (maxBound :: Int64 ) / 1000000000 - 1
243+
244+ -- The clock is read only until the interval has passed.
245+ {-# INLINE_LATE step1 #-}
246+ step1 _ (DropIntervalInit st)
247+ | not (d > 0 ) = return $ Skip $ DropIntervalYield st
248+ | d >= maxInterval = return $ Skip $ DropIntervalDropAll st
249+ | otherwise = do
250+ now <- liftIO $ getTime Monotonic
251+ let i = toRelTime64 (NanoSecond64 (round (d * 1000000000 )))
252+ return $ Skip $ DropIntervalDrop (addToAbsTime64 now i) st
253+ step1 gst (DropIntervalDrop end st) = do
254+ r <- step gst st
255+ case r of
256+ Yield x s -> do
257+ now <- liftIO $ getTime Monotonic
258+ return
259+ $ if now >= end
260+ then Yield x (DropIntervalYield s)
261+ else Skip (DropIntervalDrop end s)
262+ Skip s -> return $ Skip (DropIntervalDrop end s)
263+ Stop -> return Stop
264+ step1 gst (DropIntervalDropAll st) = do
265+ r <- step gst st
266+ return
267+ $ case r of
268+ Yield _ s -> Skip (DropIntervalDropAll s)
269+ Skip s -> Skip (DropIntervalDropAll s)
270+ Stop -> Stop
271+ step1 gst (DropIntervalYield st) = do
272+ r <- step gst st
273+ return
274+ $ case r of
275+ Yield x s -> Yield x (DropIntervalYield s)
276+ Skip s -> Skip (DropIntervalYield s)
277+ Stop -> Stop
219278
220279-- | Drop time interval @i@ seconds at the end of the stream.
221280--
0 commit comments