{-|
Module: IHP.AutoRefresh.Types
Description: Types & Data Structures for IHP AutoRefresh
Copyright: (c) digitally induced GmbH, 2020
-}
module IHP.AutoRefresh.Types where

import IHP.Prelude
import IHP.Controller.RequestContext
import Control.Concurrent.MVar (MVar)
import qualified IHP.PGListener as PGListener

data AutoRefreshState = AutoRefreshDisabled | AutoRefreshEnabled { AutoRefreshState -> UUID
sessionId :: !UUID }
data AutoRefreshSession = AutoRefreshSession
        { AutoRefreshSession -> UUID
id :: !UUID
        -- | A callback to rerun an action within a given request context
        , AutoRefreshSession -> RequestContext -> IO ()
renderView :: !(RequestContext -> IO ())
        -- | MVar that is filled whenever some table changed
        , AutoRefreshSession -> MVar ()
event :: !(MVar ())
        -- | All tables this auto refresh session watches
        , AutoRefreshSession -> Set ByteString
tables :: !(Set ByteString)
        -- | The last rendered html of this action. Initially this is the result of the initial page rendering
        , AutoRefreshSession -> LByteString
lastResponse :: !LByteString
        -- | Keep track of the last ping to this session to close it after too much time has passed without anything happening
        , AutoRefreshSession -> UTCTime
lastPing :: !UTCTime
        }

data AutoRefreshServer = AutoRefreshServer
        { AutoRefreshServer -> [Subscription]
subscriptions :: [PGListener.Subscription]
        , AutoRefreshServer -> [AutoRefreshSession]
sessions :: ![AutoRefreshSession]
        , AutoRefreshServer -> Set ByteString
subscribedTables :: !(Set ByteString)
        , AutoRefreshServer -> PGListener
pgListener :: PGListener.PGListener
        }

newAutoRefreshServer :: PGListener.PGListener -> AutoRefreshServer
newAutoRefreshServer :: PGListener -> AutoRefreshServer
newAutoRefreshServer PGListener
pgListener = AutoRefreshServer { subscriptions :: [Subscription]
subscriptions = [], sessions :: [AutoRefreshSession]
sessions = [], subscribedTables :: Set ByteString
subscribedTables = Set ByteString
forall a. Monoid a => a
mempty, PGListener
pgListener :: PGListener
pgListener :: PGListener
pgListener }