1{-# OPTIONS_HADDOCK hide #-}
4-- Module : Streamly.Internal.Data.Time
5-- Copyright : (c) 2017 Harendra Kumar
8-- Maintainer : streamly@composewell.com
9-- Stability : experimental
12-- Time utilities for reactive programming.
14module Streamly.Internal.Data.Time
16 "Please use the \"rate\" combinator instead of the functions in this module"
23import Control.Monad (when)
24import Control.Concurrent (threadDelay)
26-- | Run an action forever periodically at the given frequency specified in per
30{-# DEPRECATED periodic "Please use the \"rate\" combinator instead" #-}
31periodic :: Int -> IO () -> IO ()
32periodic freq action = do
34 threadDelay (1000000 `div` freq)
37-- | Run a computation on every clock tick, the clock runs at the specified
38-- frequency. It allows running a computation at high frequency efficiently by
39-- maintaining a local clock and adjusting it with the provided base clock at
40-- longer intervals. The first argument is a base clock returning some notion
41-- of time in microseconds. The second argument is the frequency in per second
42-- (Hz). The third argument is the action to run, the action is provided the
43-- local time as an argument.
46{-# DEPRECATED withClock "Please use the \"rate\" combinator instead" #-}
47withClock :: IO Int -> Int -> (Int -> IO ()) -> IO ()
48withClock clock freq action = do
50 go t period period t 0
54 period = 1000000 `div` freq
56 -- Note that localTime is roughly but not exactly equal to (lastAdj + tick
57 -- * n). That is because we do not abruptly adjust the clock skew instead
58 -- we adjust the tick size.
59 go lastAdj delay tick localTime n = do
61 when (delay > 0) $ threadDelay delay
65 (t, newTick, newDelay) <- adjustClock lastAdj localTime delay
66 go t newDelay newTick (localTime + newTick) 0
67 else go lastAdj delay tick (localTime + tick) (n + 1)
69 -- Adjust the tick size rather than the clock to avoid abrupt changes
70 -- resulting in jittery behavior at the end of every interval.
71 adjustClock lastAdj localTime delay = do
73 let newTick = period + (baseTime - localTime) `div` freq
74 lastPeriod = (baseTime - lastAdj) `div` freq
75 newDelay = max 0 (delay + period - lastPeriod)
76 return (baseTime, newTick, newDelay)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list