module Control.Monad.Loops.STM where
import Control.Concurrent
import Control.Concurrent.STM
import Control.Monad (forever)
import Data.Maybe
atomLoop :: STM a -> IO ()
atomLoop x = go
where go = atomically x >> go
forkAtomLoop :: STM a -> IO ThreadId
forkAtomLoop = forkIO . atomLoop
waitFor :: (a -> Bool) -> STM a -> STM a
waitFor p events = do
event <- events
if p event
then return event
else retry
waitForTrue :: STM Bool -> STM ()
waitForTrue p = waitFor id p >> return ()
waitForJust :: STM (Maybe a) -> STM a
waitForJust m = fmap fromJust (waitFor isJust m)
waitForEvent :: (a -> Bool) -> TChan a -> STM a
waitForEvent p events = waitFor p (readTChan events)