@@ -28,7 +28,8 @@ import Streamly.Internal.Data.Scanl (Scanl(..))
2828import Streamly.Internal.Data.Stream (Stream (.. ), Step (.. ))
2929import Streamly.Internal.Data.SVar.Type (adaptState )
3030import Streamly.Internal.Data.Tuple.Strict (Tuple3' (.. ))
31-
31+ import qualified Streamly.Data.Fold as Fold
32+ import qualified Streamly.Data.Stream as Stream
3233import qualified Data.Map.Strict as Map
3334
3435import Streamly.Internal.Data.Fold.Channel.Type
@@ -166,6 +167,11 @@ parDistributeScan cfg getFolds (Stream sstep state) =
166167
167168 where
168169
170+ spanChans tid =
171+ Fold. tee
172+ (Fold. filter (\ (_, t) -> t /= tid) Fold. toList)
173+ (Fold. filter (\ (_, t) -> t == tid) Fold. toList)
174+
169175 -- XXX can be written as a fold
170176 processOutputs chans events done = do
171177 case events of
@@ -177,9 +183,11 @@ parDistributeScan cfg getFolds (Stream sstep state) =
177183 liftIO $ mapM_ (`throwTo` ThreadAbort ) (fmap snd chans)
178184 mapM_ cleanup (fmap fst chans)
179185 liftIO $ throwM ex
180- FoldDone tid b ->
181- let ch = filter (\ (_, t) -> t /= tid) chans
182- in processOutputs ch xs (b: done)
186+ FoldDone tid b -> do
187+ (ch, chToClose) <-
188+ Stream. fold (spanChans tid) (Stream. fromList chans)
189+ Prelude. mapM_ (finalize . fst ) chToClose
190+ processOutputs ch xs (b: done)
183191 FoldPartial b ->
184192 processOutputs chans xs (b: done)
185193
0 commit comments