module Network.Wai.EventSource (
ServerEvent (..),
eventSourceAppChan,
eventSourceAppIO,
eventStreamAppRaw,
) where
import Control.Concurrent.Chan (Chan, dupChan, readChan)
import Control.Monad.IO.Class (liftIO)
import Data.Function (fix)
import Network.HTTP.Types (hContentType, status200)
import Network.Wai (Application, responseStream)
import Network.Wai.EventSource.EventStream
eventSourceAppChan :: Chan ServerEvent -> Application
eventSourceAppChan :: Chan ServerEvent -> Application
eventSourceAppChan Chan ServerEvent
chan Request
req Response -> IO ResponseReceived
sendResponse = do
Chan ServerEvent
chan' <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Chan a -> IO (Chan a)
dupChan Chan ServerEvent
chan
IO ServerEvent -> Application
eventSourceAppIO (forall a. Chan a -> IO a
readChan Chan ServerEvent
chan') Request
req Response -> IO ResponseReceived
sendResponse
eventSourceAppIO :: IO ServerEvent -> Application
eventSourceAppIO :: IO ServerEvent -> Application
eventSourceAppIO IO ServerEvent
src Request
_ Response -> IO ResponseReceived
sendResponse =
Response -> IO ResponseReceived
sendResponse forall a b. (a -> b) -> a -> b
$ Status -> ResponseHeaders -> StreamingBody -> Response
responseStream
Status
status200
[(HeaderName
hContentType, ByteString
"text/event-stream")]
forall a b. (a -> b) -> a -> b
$ \Builder -> IO ()
sendChunk IO ()
flush -> do
IO ()
flush
forall a. (a -> a) -> a
fix forall a b. (a -> b) -> a -> b
$ \IO ()
loop -> do
ServerEvent
se <- IO ServerEvent
src
case ServerEvent -> Maybe Builder
eventToBuilder ServerEvent
se of
Maybe Builder
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just Builder
b -> Builder -> IO ()
sendChunk Builder
b forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IO ()
flush forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IO ()
loop
eventStreamAppRaw :: ((ServerEvent -> IO()) -> IO () -> IO ()) -> Application
eventStreamAppRaw :: ((ServerEvent -> IO ()) -> IO () -> IO ()) -> Application
eventStreamAppRaw (ServerEvent -> IO ()) -> IO () -> IO ()
handler Request
_ Response -> IO ResponseReceived
sendResponse =
Response -> IO ResponseReceived
sendResponse forall a b. (a -> b) -> a -> b
$ Status -> ResponseHeaders -> StreamingBody -> Response
responseStream
Status
status200
[(HeaderName
hContentType, ByteString
"text/event-stream")]
forall a b. (a -> b) -> a -> b
$ \Builder -> IO ()
sendChunk IO ()
flush -> (ServerEvent -> IO ()) -> IO () -> IO ()
handler (forall {m :: * -> *}.
Monad m =>
(Builder -> m ()) -> ServerEvent -> m ()
sendEvent Builder -> IO ()
sendChunk) IO ()
flush
where
sendEvent :: (Builder -> m ()) -> ServerEvent -> m ()
sendEvent Builder -> m ()
sendChunk ServerEvent
event =
case ServerEvent -> Maybe Builder
eventToBuilder ServerEvent
event of
Maybe Builder
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just Builder
b -> Builder -> m ()
sendChunk Builder
b