1{-# OPTIONS_GHC -Wno-deprecations #-}
3-- Module : Streamly.Internal.Data.Fold.SVar
4-- Copyright : (c) 2017 Composewell Technologies
5-- License : BSD-3-Clause
6-- Maintainer : streamly@composewell.com
7-- Stability : experimental
11module Streamly.Internal.Data.Fold.SVar
20import Control.Monad (void)
21import Control.Monad.IO.Class (MonadIO(liftIO))
22import Streamly.Internal.Data.Fold (Fold(..))
24import qualified Streamly.Internal.Data.Fold as FL (Step(Done, Partial))
26import Streamly.Internal.Data.SVar
28-- | A fold to write a stream to an SVar. Unlike 'toSVar' this does not allow
29-- for concurrent evaluation of the stream, as the fold receives the input one
30-- element at a time, it just forwards the elements to the SVar. However, we
31-- can safely execute the fold in an independent thread, the SVar can act as a
32-- buffer decoupling the sender from the receiver. Also, we can have multiple
33-- folds running concurrently pusing the streams to the SVar.
36write :: MonadIO m => SVar t m a -> Maybe WorkerInfo -> Fold m a ()
37write svar winfo = Fold step initial return final
41 initial = return $ FL.Partial ()
43 -- XXX we can have a separate fold for unlimited buffer case to avoid a
44 -- branch in the step here.
47 decrementBufferLimit svar
48 void $ send svar (ChildYield x)
49 return $ FL.Partial ()
51 final () = liftIO $ sendStop svar winfo
53-- | Like write, but applies a yield limit.
55{-# INLINE writeLimited #-}
56writeLimited :: MonadIO m
57 => SVar t m a -> Maybe WorkerInfo -> Fold m a ()
58writeLimited svar winfo = Fold step initial (const (return ())) final
62 initial = return $ FL.Partial True
66 yieldLimitOk <- decrementYieldLimit svar
69 decrementBufferLimit svar
70 void $ send svar (ChildYield x)
71 return $ FL.Partial True
73 cleanupSVarFromWorker svar
76 step False _ = return $ FL.Done ()
78 final True = liftIO $ sendStop svar winfo
79 final False = return ()
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list