{-# 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
{ _regExec :: Exec
, _regSubId :: UUID
, _regStream :: StreamId t
, _regChan :: Chan SubAction
}
instance Subscription (RegularSubscription s) where
nextSubEvent s = readChan (_regChan s)
unsubscribe s = publishWith (_regExec s) (SendPackage pkg)
where
pkg = createUnsubscribePackage (_regSubId s)
instance SubscriptionStream (RegularSubscription t) t where
subscriptionStream = _regStream
newRegularSubscription
:: Exec
-> StreamId t
-> Bool
-> Maybe Credentials
-> IO (RegularSubscription t)
newRegularSubscription exec streamId tos cred
= do (subId, chan) <- volatile exec streamId tos cred
let sub =
RegularSubscription
{ _regExec = exec
, _regSubId = subId
, _regStream = streamId
, _regChan = chan
}
pure sub