module Unfork.Async.WithResult.Future where

import qualified Control.Concurrent.MVar as MVar

{- |

    The result of an action unforked by 'Unfork.unforkAsyncIO'

    At first the result will be unavailable, during which time 'await' will block and 'poll' will return 'Nothing'. When the action completes, 'await' will return its result and 'poll' will return 'Just'.

-}

data Future result =
    Future
        (MVar.MVar result)

{- |

    Block until an action completes

-}

await :: Future result -> IO result
await :: Future result -> IO result
await (Future MVar result
v) = MVar result -> IO result
forall a. MVar a -> IO a
MVar.readMVar MVar result
v

{- |

    Returns 'Just' an action's result, or 'Nothing' if the action is not yet complete

-}

poll :: Future result -> IO (Maybe result)
poll :: Future result -> IO (Maybe result)
poll (Future MVar result
v) = MVar result -> IO (Maybe result)
forall a. MVar a -> IO (Maybe a)
MVar.tryReadMVar MVar result
v