{-# LANGUAGE FlexibleInstances #-}
{-# OPTIONS -Wno-orphans #-}
module Database.EventStore.Internal.Subscription.Regular where
import Database.EventStore.Internal.Communication
import Database.EventStore.Internal.Control
import Database.EventStore.Internal.Exec
import Database.EventStore.Internal.Operation.Volatile
import Database.EventStore.Internal.Prelude
import Database.EventStore.Internal.Stream
import Database.EventStore.Internal.Subscription.Api
import Database.EventStore.Internal.Subscription.Types
import Database.EventStore.Internal.Subscription.Packages
data RegularSubscription t =
RegularSubscription
{ forall t. RegularSubscription t -> Exec
_regExec :: Exec
, forall t. RegularSubscription t -> UUID
_regSubId :: UUID
, forall t. RegularSubscription t -> StreamId t
_regStream :: StreamId t
, forall t. RegularSubscription t -> Chan SubAction
_regChan :: Chan SubAction
}
instance Subscription (RegularSubscription s) where
nextSubEvent :: RegularSubscription s -> IO SubAction
nextSubEvent RegularSubscription s
s = forall (m :: * -> *) a. MonadBase IO m => Chan a -> m a
readChan (forall t. RegularSubscription t -> Chan SubAction
_regChan RegularSubscription s
s)
unsubscribe :: RegularSubscription s -> IO ()
unsubscribe RegularSubscription s
s = forall p a (m :: * -> *).
(Pub p, Typeable a, MonadIO m) =>
p -> a -> m ()
publishWith (forall t. RegularSubscription t -> Exec
_regExec RegularSubscription s
s) (Package -> SendPackage
SendPackage Package
pkg)
where
pkg :: Package
pkg = UUID -> Package
createUnsubscribePackage (forall t. RegularSubscription t -> UUID
_regSubId RegularSubscription s
s)
instance SubscriptionStream (RegularSubscription t) t where
subscriptionStream :: RegularSubscription t -> StreamId t
subscriptionStream = forall t. RegularSubscription t -> StreamId t
_regStream
newRegularSubscription
:: Exec
-> StreamId t
-> Bool
-> Maybe Credentials
-> IO (RegularSubscription t)
newRegularSubscription :: forall t.
Exec
-> StreamId t
-> Bool
-> Maybe Credentials
-> IO (RegularSubscription t)
newRegularSubscription Exec
exec StreamId t
streamId Bool
tos Maybe Credentials
cred
= do (UUID
subId, Chan SubAction
chan) <- forall t.
Exec
-> StreamId t
-> Bool
-> Maybe Credentials
-> IO (UUID, Chan SubAction)
volatile Exec
exec StreamId t
streamId Bool
tos Maybe Credentials
cred
let sub :: RegularSubscription t
sub =
RegularSubscription
{ _regExec :: Exec
_regExec = Exec
exec
, _regSubId :: UUID
_regSubId = UUID
subId
, _regStream :: StreamId t
_regStream = StreamId t
streamId
, _regChan :: Chan SubAction
_regChan = Chan SubAction
chan
}
forall (f :: * -> *) a. Applicative f => a -> f a
pure RegularSubscription t
sub