module Auth.Biscuit.Timer
( timer
) where
import Control.Concurrent (threadDelay)
import Control.Concurrent.Async (race)
timer :: Int
-> IO a
-> IO (Maybe a)
timer :: forall a. Int -> IO a -> IO (Maybe a)
timer Int
timeout IO a
job = do
let watchDog :: IO ()
watchDog = Int -> IO ()
threadDelay Int
timeout
Either () a
result <- IO () -> IO a -> IO (Either () a)
forall a b. IO a -> IO b -> IO (Either a b)
race IO ()
watchDog IO a
job
Maybe a -> IO (Maybe a)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe a -> IO (Maybe a)) -> Maybe a -> IO (Maybe a)
forall a b. (a -> b) -> a -> b
$ case Either () a
result of
Left ()
_ -> Maybe a
forall a. Maybe a
Nothing
Right a
a -> a -> Maybe a
forall a. a -> Maybe a
Just a
a