{-# OPTIONS_HADDOCK hide #-}
module Streamly.Internal.Data.Time
{-# DEPRECATED
"Please use the \"rate\" combinator instead of the functions in this module"
#-}
( periodic
, withClock
)
where
import Control.Monad (when)
import Control.Concurrent (threadDelay)
{-# DEPRECATED periodic "Please use the \"rate\" combinator instead" #-}
periodic :: Int -> IO () -> IO ()
periodic freq action = do
action
threadDelay (1000000 `div` freq)
periodic freq action
{-# DEPRECATED withClock "Please use the \"rate\" combinator instead" #-}
withClock :: IO Int -> Int -> (Int -> IO ()) -> IO ()
withClock clock freq action = do
t <- clock
go t period period t 0
where
period = 1000000 `div` freq
go lastAdj delay tick localTime n = do
action localTime
when (delay > 0) $ threadDelay delay
if n == freq
then do
(t, newTick, newDelay) <- adjustClock lastAdj localTime delay
go t newDelay newTick (localTime + newTick) 0
else go lastAdj delay tick (localTime + tick) (n + 1)
adjustClock lastAdj localTime delay = do
baseTime <- clock
let newTick = period + (baseTime - localTime) `div` freq
lastPeriod = (baseTime - lastAdj) `div` freq
newDelay = max 0 (delay + period - lastPeriod)
return (baseTime, newTick, newDelay)