on master help sign in
Add Network.Wai.EventSource.eventStreamAppRaw.

This allows for more flexible ways of emitting events than the other two alternatives.

commitc0e94e1aa8377d00ef2a6bcd808faae391f0310c authorXavier Shay <xavier.shay@ferocia.com.au> dateSat 3 Aug 2019, 12:50 (7 years ago) parenta8874ee
2 files changed+26
wai-extra/Network/Wai/EventSource.hs+15 wai-extra/example/Main.hs+11
wai-extra/Network/Wai/EventSource.hs+15
@@ -39,3 +39,18 @@ eventSourceAppIO src _ sendResponse =
3939 case eventToBuilder se of
4040 Nothing -> return ()
4141 Just b -> sendChunk b >> flush >> loop
42+
43+-- | Make a new WAI EventSource application with a handler that emits events.
44+--
45+-- @since 3.0.28
46+eventStreamAppRaw :: ((ServerEvent -> IO()) -> IO () -> IO ()) -> Application
47+eventStreamAppRaw handler _ sendResponse =
48+ sendResponse $ responseStream
49+ status200
50+ [(hContentType, "text/event-stream")]
51+ $ \sendChunk flush -> handler (sendEvent sendChunk) flush
52+ where
53+ sendEvent sendChunk event =
54+ case eventToBuilder event of
55+ Nothing -> return ()
56+ Just b -> sendChunk b
wai-extra/example/Main.hs+11
@@ -37,6 +37,17 @@ eventIO = do
3737 Nothing
3838 [string8 . show $ time]
3939
40+eventRaw :: (ServerEvent -> IO ()) -> IO () -> IO ()
41+eventRaw = handle 0
42+ where
43+ handle counter emit flush = do
44+ threadDelay 1000000
45+ emit $ ServerEvent (Just $ string8 "raw")
46+ Nothing
47+ [string8 . show $ counter]
48+ flush
49+ handle (counter + 1) emit flush
50+
4051main :: IO ()
4152main = do
4253 chan <- newChan