{-# LANGUAGE Arrows #-}
{-# LANGUAGE RecordWildCards #-}

module FRP.Rhine.Clock.Util where

-- time-domain
import Data.TimeDomain

-- rhine
import FRP.Rhine.Clock
import FRP.Rhine.Clock.Proxy

-- * Auxiliary definitions and utilities

{- | Given a clock value and an initial time,
   generate a stream of time stamps.
-}
genTimeInfo ::
  (Monad m, Clock m cl) =>
  ClockProxy cl ->
  Time cl ->
  MSF m (Time cl, Tag cl) (TimeInfo cl)
genTimeInfo :: forall (m :: Type -> Type) cl.
(Monad m, Clock m cl) =>
ClockProxy cl -> Time cl -> MSF m (Time cl, Tag cl) (TimeInfo cl)
genTimeInfo ClockProxy cl
_ Time cl
initialTime = proc (Time cl
absolute, Tag cl
tag) -> do
  Time cl
lastTime <- forall (m :: Type -> Type) a. Monad m => a -> MSF m a a
iPre Time cl
initialTime -< Time cl
absolute
  forall (a :: Type -> Type -> Type) b. Arrow a => a b b
returnA
    -<
      TimeInfo
        { sinceLast :: Diff (Time cl)
sinceLast = Time cl
absolute forall time. TimeDomain time => time -> time -> Diff time
`diffTime` Time cl
lastTime
        , sinceInit :: Diff (Time cl)
sinceInit = Time cl
absolute forall time. TimeDomain time => time -> time -> Diff time
`diffTime` Time cl
initialTime
        , Time cl
Tag cl
tag :: Tag cl
absolute :: Time cl
tag :: Tag cl
absolute :: Time cl
..
        }