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