{-

STM, asynchronous, with task results discarded

Discarding results makes this function simpler than those that make results available. All we do is maintain a queue of tasks, and each step of the queue loop runs the action.

-}

module Unfork.Async.FireAndForget.STM
    (
        unforkAsyncSTM_,
    )
    where

import Unfork.Async.Core

import Prelude (IO, pure)

import Control.Monad.STM (STM)

{- |

    Turns an IO action into a fire-and-forget STM action

    Related functions:

      - 'Unfork.unforkAsyncSTM' does not discard the action result, and it allows polling or waiting for completion
      - 'Unfork.unforkAsyncIO_' gives the unforked action result as 'IO' instead of 'STM'

-}

unforkAsyncSTM_ ::
    (task -> IO result) -- ^ Action that needs to be run serially
    -> ((task -> STM ()) -> IO conclusion) -- ^ Continuation with the unforked action
    -> IO conclusion

unforkAsyncSTM_ :: (task -> IO result)
-> ((task -> STM ()) -> IO conclusion) -> IO conclusion
unforkAsyncSTM_ task -> IO result
action =
    Unfork task (STM ())
-> ((task -> STM ()) -> IO conclusion) -> IO conclusion
forall a c b. Unfork a c -> ((a -> c) -> IO b) -> IO b
unforkAsync Unfork :: forall a c q. (Ctx q -> a -> c) -> (q -> IO ()) -> Unfork a c
Unfork{ Ctx task -> task -> STM ()
forall q. Ctx q -> q -> STM ()
unforkedAction :: Ctx task -> task -> STM ()
unforkedAction :: forall q. Ctx q -> q -> STM ()
unforkedAction, task -> IO ()
executeOneTask :: task -> IO ()
executeOneTask :: task -> IO ()
executeOneTask }
  where
    unforkedAction :: Ctx q -> q -> STM ()
unforkedAction Ctx q
ctx q
arg = Ctx q -> q -> STM ()
forall q. Ctx q -> q -> STM ()
enqueue Ctx q
ctx q
arg
    executeOneTask :: task -> IO ()
executeOneTask task
a = do{ result
_ <- task -> IO result
action task
a; () -> IO ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure () }