never executed always true always false
1 module UnliftIO.MessageBox.Util.Future
2 ( Future (Future),
3 tryNow,
4 awaitFuture,
5 )
6 where
7
8 import UnliftIO (MonadIO (liftIO), MonadUnliftIO)
9 import UnliftIO.Concurrent (threadDelay)
10
11 -- | A wrapper around an IO action that returns value
12 -- in the future.
13 newtype Future a = Future
14 { -- | Return 'Just' the value or 'Nothing',
15 -- when the value is not available yet.
16 fromFuture :: IO (Maybe a)
17 }
18
19 -- | Return 'Just' the value or 'Nothing',
20 -- when the value is not available yet.
21 --
22 -- Once the value is available, that value
23 -- will be returned everytime this function is
24 -- invoked.
25 {-# INLINE tryNow #-}
26 tryNow :: MonadUnliftIO m => Future a -> m (Maybe a)
27 tryNow = liftIO . fromFuture
28
29 -- | Poll a Future until the value is present.
30 awaitFuture :: MonadUnliftIO m => Future b -> m b
31 awaitFuture !f =
32 tryNow f >>= maybe (threadDelay 10 >> awaitFuture f) return