pituicat/src/Affection/MessageBus/Util.hs

32 lines
1.1 KiB
Haskell
Raw Normal View History

2017-11-26 21:26:41 +00:00
module Affection.MessageBus.Util where
import Affection.MessageBus.Class
import Affection.MessageBus.Message
2017-11-27 22:30:11 +00:00
import Affection.Types
2017-11-26 21:26:41 +00:00
import Control.Concurrent.STM as STM
2017-11-27 04:27:52 +00:00
-- | Build a new broadcast channel
2017-11-26 21:26:41 +00:00
newBroadcastChannel :: (Message msg) => IO (Channel msg)
2017-11-27 00:43:43 +00:00
newBroadcastChannel = atomically $ Channel <$> newBroadcastTChan
2017-11-27 04:27:52 +00:00
-- | Duplicate a broadcast channel, so it can be accessed by subsystems
dupChannel :: (Message msg)
=> Channel msg -- ^ Original 'Channel'
-> IO (Channel msg) -- ^ 'Channel' duplicate
dupChannel (Channel c) = atomically $ Channel <$> dupTChan c
-- | Try to read a 'Channel' wihtout deleting the message. Returns 'Nothing' on
-- an empty 'Channel'
tryPeekChannel :: (Message msg)
=> Channel msg -- ^ Channel to read from
-> IO (Maybe msg) -- ^ Resulting message (or not)
tryPeekChannel (Channel c) = atomically $ tryPeekTChan c
-- | Write a message to a 'Channel' and thus broadcast it to all connected
-- subsystems
writeChannel :: (Message msg)
=> Channel msg -- ^ 'Channel' to write to
-> msg -- ^ The Message to emit
-> IO ()
writeChannel (Channel c) m = atomically $ writeTChan c m