{-# LANGUAGE CPP #-}
module Network.Wai.EventSource.EventStream (
ServerEvent (..),
eventToBuilder,
) where
import Data.ByteString.Builder
#if __GLASGOW_HASKELL__ < 710
import Data.Monoid
#endif
import Data.Word8 (_colon, _lf)
data ServerEvent
= ServerEvent
{ ServerEvent -> Maybe Builder
eventName :: Maybe Builder
, ServerEvent -> Maybe Builder
eventId :: Maybe Builder
, ServerEvent -> [Builder]
eventData :: [Builder]
}
|
{ :: Builder
}
| RetryEvent
{ ServerEvent -> Int
eventRetry :: Int
}
| CloseEvent
nl :: Builder
nl :: Builder
nl = Word8 -> Builder
word8 Word8
_lf
nameField, idField, dataField, retryField, commentField :: Builder
nameField :: Builder
nameField = String -> Builder
string7 String
"event:"
idField :: Builder
idField = String -> Builder
string7 String
"id:"
dataField :: Builder
dataField = String -> Builder
string7 String
"data:"
retryField :: Builder
retryField = String -> Builder
string7 String
"retry:"
= Word8 -> Builder
word8 Word8
_colon
field :: Builder -> Builder -> Builder
field :: Builder -> Builder -> Builder
field Builder
l Builder
b = Builder
l Builder -> Builder -> Builder
forall a. Monoid a => a -> a -> a
`mappend` Builder
b Builder -> Builder -> Builder
forall a. Monoid a => a -> a -> a
`mappend` Builder
nl
eventToBuilder :: ServerEvent -> Maybe Builder
eventToBuilder :: ServerEvent -> Maybe Builder
eventToBuilder (CommentEvent Builder
txt) = Builder -> Maybe Builder
forall a. a -> Maybe a
Just (Builder -> Maybe Builder) -> Builder -> Maybe Builder
forall a b. (a -> b) -> a -> b
$ Builder -> Builder -> Builder
field Builder
commentField Builder
txt
eventToBuilder (RetryEvent Int
n) = Builder -> Maybe Builder
forall a. a -> Maybe a
Just (Builder -> Maybe Builder) -> Builder -> Maybe Builder
forall a b. (a -> b) -> a -> b
$ Builder -> Builder -> Builder
field Builder
retryField (String -> Builder
string8 (String -> Builder) -> (Int -> String) -> Int -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String
forall a. Show a => a -> String
show (Int -> Builder) -> Int -> Builder
forall a b. (a -> b) -> a -> b
$ Int
n)
eventToBuilder ServerEvent
CloseEvent = Maybe Builder
forall a. Maybe a
Nothing
eventToBuilder (ServerEvent Maybe Builder
n Maybe Builder
i [Builder]
d) =
Builder -> Maybe Builder
forall a. a -> Maybe a
Just (Builder -> Maybe Builder) -> Builder -> Maybe Builder
forall a b. (a -> b) -> a -> b
$
Maybe Builder -> Builder -> Builder
name Maybe Builder
n (Maybe Builder -> Builder -> Builder
evid Maybe Builder
i (Builder -> Builder) -> Builder -> Builder
forall a b. (a -> b) -> a -> b
$ [Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat ((Builder -> Builder) -> [Builder] -> [Builder]
forall a b. (a -> b) -> [a] -> [b]
map (Builder -> Builder -> Builder
field Builder
dataField) [Builder]
d)) Builder -> Builder -> Builder
forall a. Monoid a => a -> a -> a
`mappend` Builder
nl
where
name :: Maybe Builder -> Builder -> Builder
name Maybe Builder
Nothing = Builder -> Builder
forall a. a -> a
id
name (Just Builder
n') = Builder -> Builder -> Builder
forall a. Monoid a => a -> a -> a
mappend (Builder -> Builder -> Builder
field Builder
nameField Builder
n')
evid :: Maybe Builder -> Builder -> Builder
evid Maybe Builder
Nothing = Builder -> Builder
forall a. a -> a
id
evid (Just Builder
i') = Builder -> Builder -> Builder
forall a. Monoid a => a -> a -> a
mappend (Builder -> Builder -> Builder
field Builder
idField Builder
i')