module Data.Conduit.Lazy
( lazyConsume
) where
import Data.Conduit
import Data.Conduit.Internal (Pipe (..))
import System.IO.Unsafe (unsafeInterleaveIO)
import Control.Monad.Trans.Control (MonadBaseControl, liftBaseOp_)
import Control.Monad.Trans.Resource (MonadActive (monadActive))
lazyConsume :: (MonadBaseControl IO m, MonadActive m) => Pipe l i a () m r -> m [a]
lazyConsume (Done _) = return []
lazyConsume (HaveOutput src _ x) = do
xs <- lazyConsume src
return $ x : xs
lazyConsume (PipeM msrc) = liftBaseOp_ unsafeInterleaveIO $ do
a <- monadActive
if a
then msrc >>= lazyConsume
else return []
lazyConsume (NeedInput _ c) = lazyConsume (c ())
lazyConsume (Leftover p _) = lazyConsume p