{-|
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 Data.Set (Set)
import Control.Concurrent.Async

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 -> [Async ()]
subscriptions :: [Async ()], AutoRefreshServer -> [AutoRefreshSession]
sessions :: ![AutoRefreshSession], AutoRefreshServer -> Set ByteString
subscribedTables :: !(Set ByteString) }

newAutoRefreshServer :: AutoRefreshServer
newAutoRefreshServer :: AutoRefreshServer
newAutoRefreshServer = AutoRefreshServer :: [Async ()]
-> [AutoRefreshSession] -> Set ByteString -> AutoRefreshServer
AutoRefreshServer { $sel:subscriptions:AutoRefreshServer :: [Async ()]
subscriptions = [], $sel:sessions:AutoRefreshServer :: [AutoRefreshSession]
sessions = [], $sel:subscribedTables:AutoRefreshServer :: Set ByteString
subscribedTables = Set ByteString
forall a. Monoid a => a
mempty }