apiary-eventsource (empty) → 0.11.3
raw patch · 4 files changed
+113/−0 lines, 4 filesdep +apiarydep +basedep +blaze-buildersetup-changed
Dependencies added: apiary, base, blaze-builder, conduit, wai-eventsource, wai-extra
Files
- LICENSE +20/−0
- Setup.hs +2/−0
- apiary-eventsource.cabal +40/−0
- src/Web/Apiary/EventSource.hs +51/−0
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2014 Hirotomo Moriwaki++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ apiary-eventsource.cabal view
@@ -0,0 +1,40 @@+name: apiary-eventsource+version: 0.11.3+synopsis: eventsource support for apiary web framework.+description:+ example: <https://github.com/philopon/apiary/blob/master/examples/eventsource.hs>+license: MIT+license-file: LICENSE+author: HirotomoMoriwaki<philopon.dependence@gmail.com>+maintainer: HirotomoMoriwaki<philopon.dependence@gmail.com>+Homepage: https://github.com/philopon/apiary+Bug-reports: https://github.com/philopon/apiary/issues+copyright: (c) 2014 Hirotomo Moriwaki+category: Web+build-type: Simple+stability: experimental+-- extra-source-files: +cabal-version: >=1.10++flag wai2apiary+ default: False+ manual: False++library+ exposed-modules: Web.Apiary.EventSource+ build-depends: base >=4.6 && <4.8+ , apiary >=0.11.3 && <0.12+ , blaze-builder >=0.3 && <0.4+ if flag(wai2apiary)+ build-depends: wai-eventsource >=2.0 && <3.0+ , conduit >=1.1 && <1.2+ else+ build-depends: wai-extra >=3.0 && <3.1++ hs-source-dirs: src+ ghc-options: -O2 -Wall+ default-language: Haskell2010++source-repository head+ type: git+ location: git://github.com/philopon/apiary.git
+ src/Web/Apiary/EventSource.hs view
@@ -0,0 +1,51 @@+{-# LANGUAGE NoMonomorphismRestriction #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE CPP #-}++module Web.Apiary.EventSource + ( eventSourceIO, eventSourceChan+ , module Network.Wai.EventSource.EventStream+ ) where++import Data.Function+import Control.Concurrent.Chan (Chan, dupChan, readChan)++import Network.Wai.EventSource.EventStream (ServerEvent(..))+import qualified Network.Wai.EventSource.EventStream as E+import Web.Apiary++#ifdef VERSION_wai_eventsource+import Blaze.ByteString.Builder (Builder)+import Data.Conduit++ioToSource :: IO ServerEvent -> Source IO (Flush Builder)+ioToSource a =+ loop+ where+ loop = do+ x <- liftIO a+ case E.eventToBuilder x of+ Nothing -> return ()+ Just y -> do+ yield $ Chunk y+ yield Flush+ loop+#else+ioToSource :: IO ServerEvent -> StreamingBody+ioToSource src send flush = fix $ \loop -> do+ se <- src+ case E.eventToBuilder se of+ Nothing -> return ()+ Just b -> send b >> flush >> loop+#endif++eventSourceIO :: Monad m => IO ServerEvent -> ActionT m ()+eventSourceIO io = do+ status status200+ contentType "text/event-stream"+ stream $ ioToSource io++eventSourceChan :: MonadIO m => Chan ServerEvent -> ActionT m ()+eventSourceChan chan = do+ chan' <- liftIO $ dupChan chan+ eventSourceIO (readChan chan')