{-|
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)

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 Text
tables :: Set Text
        -- | 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 -> [AutoRefreshSession]
sessions :: [AutoRefreshSession], AutoRefreshServer -> Set Text
subscribedTables :: Set Text }

newAutoRefreshServer :: AutoRefreshServer
newAutoRefreshServer :: AutoRefreshServer
newAutoRefreshServer = AutoRefreshServer :: [AutoRefreshSession] -> Set Text -> AutoRefreshServer
AutoRefreshServer { $sel:sessions:AutoRefreshServer :: [AutoRefreshSession]
sessions = [], $sel:subscribedTables:AutoRefreshServer :: Set Text
subscribedTables = Set Text
forall a. Monoid a => a
mempty }