{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ExistentialQuantification #-}
module Affection.MessageBus.Class
( Participant(..)
, genUUID
, UUID
) where
import Affection.MessageBus.Message
import Affection.Types
import Control.Monad.IO.Class (liftIO)
import Data.UUID
import Data.UUID.V4
import Affection.Logging
class (Message (Mesg prt us), Show (Mesg prt us)) => Participant prt us where
type Mesg prt us :: *
partSubscribers
:: prt
-> Affection us [Mesg prt us -> Affection us ()]
partSubscribe
:: prt
-> (Mesg prt us -> Affection us ())
-> Affection us UUID
partUnSubscribe
:: prt
-> UUID
-> Affection us ()
partEmit
:: prt
-> Mesg prt us
-> Affection us ()
partEmit p m = do
liftIO $ logIO Verbose $ "Emitting message: " ++ show m
l <- partSubscribers p
mapM_ ($ m) l
genUUID :: Affection us UUID
genUUID = liftIO nextRandom