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 <- forall a b. IO a -> IO b -> IO (Either a b)
race IO ()
watchDog IO a
job
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ case Either () a
result of
Left ()
_ -> forall a. Maybe a
Nothing
Right a
a -> forall a. a -> Maybe a
Just a
a