
module Mire.Reactive.Timer (
   module Export,
   makeTimer,
) where

import Mire.Prelude
import Reactive.Banana
import Reactive.Banana.Frameworks

import Control.Concurrent.Timer.Lifted
import Control.Concurrent.Suspend.Lifted as Export

timerAddHandler :: Delay -> MomentIO (AddHandler ())
timerAddHandler delay = liftIO $ do
   (addHandler, fire) <- newAddHandler
   timer <- repeatedTimer (fire ()) delay
   return $ AddHandler $ \s -> do
      t <- register addHandler s
      return (t >> stopTimer timer)

makeTimer :: Delay -> MomentIO (Event ())
makeTimer delay = timerAddHandler delay >>= fromAddHandler
