{-# LANGUAGE Arrows #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeFamilies #-}
module FRP.Rhine.Clock.Select where
import FRP.Rhine.Clock
import FRP.Rhine.Schedule
import Data.MonadicStreamFunction.Async (concatS)
import Data.Maybe (catMaybes, maybeToList)
import Data.Semigroup
data SelectClock cl a = SelectClock
{ mainClock :: cl
, select :: Tag cl -> Maybe a
}
instance (Monad m, Clock m cl) => Clock m (SelectClock cl a) where
type Time (SelectClock cl a) = Time cl
type Tag (SelectClock cl a) = a
initClock SelectClock {..} = do
(runningClock, initialTime) <- initClock mainClock
let
runningSelectClock = filterS $ proc _ -> do
(time, tag) <- runningClock -< ()
returnA -< (time, ) <$> select tag
return (runningSelectClock, initialTime)
schedSelectClocks
:: (Monad m, Semigroup cl, Clock m cl)
=> Schedule m (SelectClock cl a) (SelectClock cl b)
schedSelectClocks = Schedule {..}
where
initSchedule subClock1 subClock2 = do
(runningClock, initialTime) <- initClock
$ mainClock subClock1 <> mainClock subClock2
let
runningSelectClocks = concatS $ proc _ -> do
(time, tag) <- runningClock -< ()
returnA -< catMaybes
[ (time, ) . Left <$> select subClock1 tag
, (time, ) . Right <$> select subClock2 tag ]
return (runningSelectClocks, initialTime)
schedSelectClockAndMain
:: (Monad m, Semigroup cl, Clock m cl)
=> Schedule m cl (SelectClock cl a)
schedSelectClockAndMain = Schedule {..}
where
initSchedule mainClock' SelectClock {..} = do
(runningClock, initialTime) <- initClock
$ mainClock' <> mainClock
let
runningSelectClock = concatS $ proc _ -> do
(time, tag) <- runningClock -< ()
returnA -< catMaybes
[ Just (time, Left tag)
, (time, ) . Right <$> select tag ]
return (runningSelectClock, initialTime)
filterS :: Monad m => MSF m () (Maybe b) -> MSF m () b
filterS = concatS . (>>> arr maybeToList)