Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 15th, 2012  |  syntax: None  |  size: 0.67 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to create an ever-retrying Enumerator
  2. enumConnectAgain :: forall b m. MonadIO m => IO Handle -> Enumerator ByteString m b
  3. enumConnectAgain open step =
  4.     fix $ again -> do
  5.         mh <- liftIO (Ex.try open)
  6.         case mh of
  7.           Left (ex :: SomeException) -> again
  8.           Right h                    -> loop h step
  9.  
  10.     where
  11.     loop :: Handle -> Step ByteString m b -> Iteratee ByteString m b
  12.     loop h step@(Continue k) = do
  13.         mstr <- liftIO (Ex.try $ B.hGetSome h 1024)
  14.         case mstr of
  15.           Left (ex :: SomeException) -> enumConnectAgain open step
  16.           Right str                  -> k (Chunks [str]) >>== loop h
  17.     loop h step = returnI step