{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE Safe #-}
module BroadcastChan.Throw
( BChanError(..)
, readBChan
, writeBChan
, BroadcastChan
, Direction(..)
, In
, Out
, newBroadcastChan
, newBChanListener
, closeBChan
, isClosedBChan
, getBChanContents
, Action(..)
, Handler(..)
, parMapM_
, parFoldMap
, parFoldMapM
, foldBChan
, foldBChanM
) where
import Control.Monad (when)
import Control.Exception (Exception, throwIO)
import Data.Typeable (Typeable)
import BroadcastChan hiding (writeBChan, readBChan)
import qualified BroadcastChan as Internal
data BChanError
= WriteFailed
| ReadFailed
deriving (BChanError -> BChanError -> Bool
(BChanError -> BChanError -> Bool)
-> (BChanError -> BChanError -> Bool) -> Eq BChanError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BChanError -> BChanError -> Bool
$c/= :: BChanError -> BChanError -> Bool
== :: BChanError -> BChanError -> Bool
$c== :: BChanError -> BChanError -> Bool
Eq, ReadPrec [BChanError]
ReadPrec BChanError
Int -> ReadS BChanError
ReadS [BChanError]
(Int -> ReadS BChanError)
-> ReadS [BChanError]
-> ReadPrec BChanError
-> ReadPrec [BChanError]
-> Read BChanError
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [BChanError]
$creadListPrec :: ReadPrec [BChanError]
readPrec :: ReadPrec BChanError
$creadPrec :: ReadPrec BChanError
readList :: ReadS [BChanError]
$creadList :: ReadS [BChanError]
readsPrec :: Int -> ReadS BChanError
$creadsPrec :: Int -> ReadS BChanError
Read, Int -> BChanError -> ShowS
[BChanError] -> ShowS
BChanError -> String
(Int -> BChanError -> ShowS)
-> (BChanError -> String)
-> ([BChanError] -> ShowS)
-> Show BChanError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BChanError] -> ShowS
$cshowList :: [BChanError] -> ShowS
show :: BChanError -> String
$cshow :: BChanError -> String
showsPrec :: Int -> BChanError -> ShowS
$cshowsPrec :: Int -> BChanError -> ShowS
Show, Typeable)
instance Exception BChanError
readBChan :: BroadcastChan Out a -> IO a
readBChan :: BroadcastChan Out a -> IO a
readBChan BroadcastChan Out a
ch = do
Maybe a
result <- BroadcastChan Out a -> IO (Maybe a)
forall (m :: * -> *) a.
MonadIO m =>
BroadcastChan Out a -> m (Maybe a)
Internal.readBChan BroadcastChan Out a
ch
case Maybe a
result of
Maybe a
Nothing -> BChanError -> IO a
forall e a. Exception e => e -> IO a
throwIO BChanError
ReadFailed
Just a
x -> a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
{-# INLINE readBChan #-}
writeBChan :: BroadcastChan In a -> a -> IO ()
writeBChan :: BroadcastChan In a -> a -> IO ()
writeBChan BroadcastChan In a
ch a
val = do
Bool
success <- BroadcastChan In a -> a -> IO Bool
forall (m :: * -> *) a.
MonadIO m =>
BroadcastChan In a -> a -> m Bool
Internal.writeBChan BroadcastChan In a
ch a
val
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not Bool
success) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ BChanError -> IO ()
forall e a. Exception e => e -> IO a
throwIO BChanError
WriteFailed
{-# INLINE writeBChan #-}