{-# OPTIONS_HADDOCK prune, not-home #-}
module Discord.Gateway
( Gateway(..)
, GatewayException(..)
, startGatewayThread
, module Discord.Types
) where
import Prelude hiding (log)
import Control.Concurrent.Chan (newChan, dupChan, Chan)
import Control.Concurrent (forkIO, ThreadId, MVar)
import Discord.Types (Auth, Event, GatewaySendable)
import Discord.Gateway.EventLoop (connectionLoop, GatewayException(..))
import Discord.Gateway.Cache
data Gateway = Gateway
{ _events :: Chan (Either GatewayException Event)
, _cache :: MVar (Either GatewayException Cache)
, _gatewayCommands :: Chan GatewaySendable
}
startGatewayThread :: Auth -> Chan String -> IO (Gateway, ThreadId)
startGatewayThread auth log = do
eventsWrite <- newChan
eventsCache <- dupChan eventsWrite
sends <- newChan
cache <- emptyCache :: IO (MVar (Either GatewayException Cache))
tid <- forkIO $ connectionLoop auth eventsWrite sends log
cacheAddEventLoopFork cache eventsCache log
pure (Gateway eventsWrite cache sends, tid)