| Maintainer | Toshio Ito <debug.ito@gmail.com> |
|---|---|
| Safe Haskell | None |
| Language | Haskell2010 |
Data.Conduit.FoldDebounce
Description
Synopsis:
module Main (main) where
import Data.Conduit (ConduitT, yield, runConduit, (.|))
import qualified Data.Conduit.List as CL
import Data.Void (Void)
import Control.Concurrent (threadDelay)
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.Resource (ResourceT, runResourceT)
import qualified Data.Conduit.FoldDebounce as F
fastSource :: Int -> ConduitT () Int (ResourceT IO) ()
fastSource max_num = fastStream' 0 where
fastStream' count = do
yield count
if count >= max_num
then return ()
else do
liftIO $ threadDelay 100000
fastStream' (count + 1)
printSink :: Show a => ConduitT a Void (ResourceT IO) ()
printSink = CL.mapM_ (liftIO . putStrLn . show)
main :: IO ()
main = do
putStrLn "-- Before debounce"
runResourceT $ runConduit $ fastSource 10 .| printSink
let debouncer = F.debounce F.Args { F.cb = undefined, -- anything will do
F.fold = (\list num -> list ++ [num]),
F.init = [] }
F.def { F.delay = 500000 }
putStrLn "-- After debounce"
runResourceT $ runConduit $ debouncer (fastSource 10) .| printSinkResult:
-- Before debounce 0 1 2 3 4 5 6 7 8 9 10 -- After debounce [0,1,2,3,4] [5,6,7,8,9] [10]
This module regulates (slows down) data stream from conduit source using Control.FoldDebounce.
The data from the original source (type i) are pulled and folded
together to create an output data (type o). The output data then
comes out of the debounced source in a predefined interval
(specified by delay option).
See Control.FoldDebounce for detail.
Synopsis
- debounce :: (MonadResource m, MonadUnliftIO m) => Args i o -> Opts i o -> ConduitT () i m () -> ConduitT () o m ()
- data Args i o = Args {}
- data Opts i o
- def :: Default a => a
- delay :: Opts i o -> Int
- alwaysResetTimer :: Opts i o -> Bool
- forStack :: Args i [i]
- forMonoid :: Monoid i => Args i i
- forVoid :: Args i ()
Documentation
Arguments
| :: (MonadResource m, MonadUnliftIO m) | |
| => Args i o | mandatory argument for FoldDebounce. |
| -> Opts i o | optional argument for FoldDebounce |
| -> ConduitT () i m () | original source |
| -> ConduitT () o m () | debounced source |
Debounce conduit source with Control.FoldDebounce. The data
stream from the original source (type i) is debounced and folded
into the data stream of the type o.
Note that the original source is connected to a sink in another thread. You may need some synchronization if the original source has side-effects.
Re-exports
Mandatory parameters for new.
Constructors
| Args | |
Fields
| |
Accessors for Opts
The time (in microsecond) to wait after receiving an event before sending it, in case more events happen in the interim.
Default: 1 second (1000000)
alwaysResetTimer :: Opts i o -> Bool #
Normally, when an event is received and it's the first of a series, a timer is started, and when that timer expires, all events are sent. If you set this parameter to True, then the timer is reset after each event is received.
Default: False
Preset parameters
Args for stacks. Input events are accumulated in a stack, i.e.,
the last event is at the head of the list.