2017-11-26 21:26:41 +00:00
|
|
|
module Affection.MessageBus.Util where
|
|
|
|
|
|
|
|
import Affection.MessageBus.Class
|
|
|
|
import Affection.MessageBus.Message
|
|
|
|
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
|