Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fork-like function for Conduit

I want calculating hash of http post body and parsing it simultaneously when receiving, so I need a function like this:

unionSinks :: Monad m => Consumer a m r1 -> Consumer a m r2 -> Consumer a m (r1, r2)
unionSinks = ...

sinkBody :: Monad m => FromJSON v => Consumer ByteString m (Digest SHA1, v)
sinkBody = sinkHash `unionSinks` sinkParser json

When I developed image uploading I used passthroughSink in similar way. But I don't needed the result of conduit, which saves image to file, in this case.

Generally, I know how to implement fork using MVar-like things, but I don't sure that this is optimal solution.

like image 890
Kayo Avatar asked Jul 09 '26 19:07

Kayo


1 Answers

Indeed you need ZipSink (or more general ZipConduit):

import Control.Applicative
import Data.Conduit

unionSinks :: (Monad m) => Sink a m r1 -> Sink a m r2 -> Sink a m (r1, r2)
unionSinks s1 s2 = getZipSink ((,) <$> ZipSink s1 <*> ZipSink s2)
like image 175
Petr Avatar answered Jul 13 '26 15:07

Petr