module Rhs.Schedule (Interval, pauseThread, pauseThreadUntil, at) where import Sound.OpenSoundControl.Time (UTC, utc) import Control.Concurrent (threadDelay) import Control.Monad (when) type Interval = Double type Action = UTC -> IO (Maybe Interval) -- | A threadDelay variant with interval given in seconds. pauseThread :: Interval -> IO () pauseThread n = when (n>0) (threadDelay (floor (n * 1e6))) -- | Pause until specified UTC time. pauseThreadUntil :: UTC -> IO () pauseThreadUntil t = utc >>= pauseThread . (t -) -- | Rescheduler resched :: Interval -> Action -> (Maybe Interval) -> IO () resched _ _ Nothing = return () resched t f (Just i) = pauseThreadUntil t' >> f t' >>= resched t' f where t' = t + i -- | Apply action at indicated time and reschedule after interval. at :: UTC -> Action -> IO () at t f = pauseThreadUntil t >> f t >>= resched t f