on master help sign in
Merge pull request #767 from xaviershay/event-source-raw

EventSource builder to enable recursive emitters

commit0f20662ad88b3116ac0fb2b80674613d0741c8b1 authorMichael Snoyman <michael@snoyman.com> dateMon 12 Aug 2019, 13:29 (7 years ago) parenta8874eeb1fd904
4 files changed+33−1
wai-extra/ChangeLog.md+4 wai-extra/Network/Wai/EventSource.hs+17 wai-extra/example/Main.hs+11 wai-extra/wai-extra.cabal+1−1
wai-extra/ChangeLog.md+4
@@ -1,5 +1,9 @@
11# Changelog for wai-extra
22
3+## 3.0.28
4+
5+* Add `Network.Wai.EventSource.eventStreamAppRaw` [#767](https://github.com/yesodweb/wai/pull/767)
6+
37## 3.0.27
48
59* Add custom request log formatter which includes response headers [#762](https://github.com/yesodweb/wai/pull/762)
wai-extra/Network/Wai/EventSource.hs+17
@@ -5,6 +5,8 @@
55 headers:
66
77 > [ ("X-Accel-Buffering", "no"), ("Cache-Control", "no-cache")]
8+
9+ There is a small example using these functions in the @example@ directory.
810-}
911module Network.Wai.EventSource (
1012 ServerEvent(..),
@@ -39,3 +41,18 @@ eventSourceAppIO src _ sendResponse =
3941 case eventToBuilder se of
4042 Nothing -> return ()
4143 Just b -> sendChunk b >> flush >> loop
44+
45+-- | Make a new WAI EventSource application with a handler that emits events.
46+--
47+-- @since 3.0.28
48+eventStreamAppRaw :: ((ServerEvent -> IO()) -> IO () -> IO ()) -> Application
49+eventStreamAppRaw handler _ sendResponse =
50+ sendResponse $ responseStream
51+ status200
52+ [(hContentType, "text/event-stream")]
53+ $ \sendChunk flush -> handler (sendEvent sendChunk) flush
54+ where
55+ sendEvent sendChunk event =
56+ case eventToBuilder event of
57+ Nothing -> return ()
58+ 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
wai-extra/wai-extra.cabal+1−1
@@ -1,5 +1,5 @@
11Name: wai-extra
2−Version: 3.0.27
2+Version: 3.0.28
33Synopsis: Provides some basic WAI handlers and middleware.
44description:
55 Provides basic WAI handler and middleware functionality: