{-# LANGUAGE FlexibleContexts #-}

module Network.AMQP.Lifted
       ( consumeMsgs
       , consumeMsgs'
       )
       where

import qualified Network.AMQP as A
import Network.AMQP.Types
import Control.Monad.Trans.Control
import Data.Text (Text)
import Control.Monad

-- | Lifted version of 'Network.AMQP.consumeMsgs' (please look there for documentation).

--

-- In addition, while the callback function @(('Message', 'Envelope') -> m ())@

-- has access to the captured state, all its side-effects in m are discarded.

consumeMsgs :: MonadBaseControl IO m
            => A.Channel
            -> Text -- ^ Specifies the name of the queue to consume from.

            -> A.Ack
            -> ((A.Message, A.Envelope) -> m ())
            -> m A.ConsumerTag
consumeMsgs :: forall (m :: * -> *).
MonadBaseControl IO m =>
Channel -> Text -> Ack -> ((Message, Envelope) -> m ()) -> m Text
consumeMsgs Channel
chan Text
queueName Ack
ack (Message, Envelope) -> m ()
callback =
    forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b a) -> m a
liftBaseWith forall a b. (a -> b) -> a -> b
$ \RunInBase m IO
runInIO ->
        Channel -> Text -> Ack -> ((Message, Envelope) -> IO ()) -> IO Text
A.consumeMsgs Channel
chan Text
queueName Ack
ack (forall (f :: * -> *) a. Functor f => f a -> f ()
void forall b c a. (b -> c) -> (a -> b) -> a -> c
. RunInBase m IO
runInIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Message, Envelope) -> m ()
callback)

-- | an extended version of @consumeMsgs@ that allows you to define a consumer cancellation callback and include arbitrary arguments.

consumeMsgs' :: MonadBaseControl IO m
             => A.Channel
             -> Text -- ^ Specifies the name of the queue to consume from.

             -> A.Ack
             -> ((A.Message, A.Envelope) -> m ())
             -> (ConsumerTag -> m ())
             -> FieldTable
             -> m A.ConsumerTag
consumeMsgs' :: forall (m :: * -> *).
MonadBaseControl IO m =>
Channel
-> Text
-> Ack
-> ((Message, Envelope) -> m ())
-> (Text -> m ())
-> FieldTable
-> m Text
consumeMsgs' Channel
chan Text
queueName Ack
ack (Message, Envelope) -> m ()
callback Text -> m ()
cancelled FieldTable
args =
    forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b a) -> m a
liftBaseWith forall a b. (a -> b) -> a -> b
$ \RunInBase m IO
runInIO ->
        Channel
-> Text
-> Ack
-> ((Message, Envelope) -> IO ())
-> (Text -> IO ())
-> FieldTable
-> IO Text
A.consumeMsgs' Channel
chan Text
queueName Ack
ack (forall (f :: * -> *) a. Functor f => f a -> f ()
void forall b c a. (b -> c) -> (a -> b) -> a -> c
. RunInBase m IO
runInIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Message, Envelope) -> m ()
callback) (forall (f :: * -> *) a. Functor f => f a -> f ()
void forall b c a. (b -> c) -> (a -> b) -> a -> c
. RunInBase m IO
runInIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> m ()
cancelled) FieldTable
args