{-# options_haddock prune #-}
module Polysemy.Conc.Queue (
module Polysemy.Conc.Queue,
module Polysemy.Conc.Effect.Queue,
) where
import qualified Polysemy.Conc.Data.QueueResult as QueueResult
import qualified Polysemy.Conc.Effect.Queue as Queue
import Polysemy.Conc.Effect.Queue (
Queue,
close,
closed,
peek,
read,
readTimeout,
tryPeek,
tryRead,
tryWrite,
write,
writeTimeout,
)
import Polysemy.Conc.Queue.Result (resultToMaybe)
loopOr ::
Member (Queue d) r =>
Sem r Bool ->
(d -> Sem r Bool) ->
Sem r ()
loopOr :: forall d (r :: [(* -> *) -> * -> *]).
Member (Queue d) r =>
Sem r Bool -> (d -> Sem r Bool) -> Sem r ()
loopOr Sem r Bool
na d -> Sem r Bool
action =
Sem r ()
spin
where
spin :: Sem r ()
spin =
Sem r (QueueResult d)
forall d (r :: [(* -> *) -> * -> *]).
MemberWithError (Queue d) r =>
Sem r (QueueResult d)
Queue.read Sem r (QueueResult d) -> (QueueResult d -> Sem r ()) -> Sem r ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
QueueResult.Success d
d -> Sem r Bool -> Sem r () -> Sem r ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
whenM (d -> Sem r Bool
action d
d) Sem r ()
spin
QueueResult d
QueueResult.NotAvailable -> Sem r Bool -> Sem r () -> Sem r ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
whenM Sem r Bool
na Sem r ()
spin
QueueResult d
QueueResult.Closed -> Sem r ()
forall (f :: * -> *). Applicative f => f ()
unit
loop ::
Member (Queue d) r =>
(d -> Sem r ()) ->
Sem r ()
loop :: forall d (r :: [(* -> *) -> * -> *]).
Member (Queue d) r =>
(d -> Sem r ()) -> Sem r ()
loop d -> Sem r ()
action =
Sem r Bool -> (d -> Sem r Bool) -> Sem r ()
forall d (r :: [(* -> *) -> * -> *]).
Member (Queue d) r =>
Sem r Bool -> (d -> Sem r Bool) -> Sem r ()
loopOr (Bool -> Sem r Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True) \ d
d -> Bool
True Bool -> Sem r () -> Sem r Bool
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ d -> Sem r ()
action d
d
readMaybe ::
Member (Queue d) r =>
Sem r (Maybe d)
readMaybe :: forall d (r :: [(* -> *) -> * -> *]).
Member (Queue d) r =>
Sem r (Maybe d)
readMaybe =
QueueResult d -> Maybe d
forall d. QueueResult d -> Maybe d
resultToMaybe (QueueResult d -> Maybe d)
-> Sem r (QueueResult d) -> Sem r (Maybe d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Sem r (QueueResult d)
forall d (r :: [(* -> *) -> * -> *]).
MemberWithError (Queue d) r =>
Sem r (QueueResult d)
read