Maintainer | Toshio Ito <debug.ito@gmail.com> |
---|---|
Safe Haskell | None |
Language | Haskell2010 |
Synopsis:
module Main (main) where import Data.Conduit (Source, Sink, yield, ($$)) import qualified Data.Conduit.List as CL 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 -> Source (ResourceT IO) Int 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 => Sink a (ResourceT IO) () printSink = CL.mapM_ (liftIO . putStrLn . show) main :: IO () main = do putStrLn "-- Before debounce" runResourceT $ 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 $ debouncer (fastSource 10) $$ printSink
Result:
-- 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.
- debounce :: (MonadResource m, MonadBaseControl IO m) => Args i o -> Opts i o -> Source m i -> Source m o
- 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
:: (MonadResource m, MonadBaseControl IO m) | |
=> Args i o | mandatory argument for FoldDebounce. |
-> Opts i o | optional argument for FoldDebounce |
-> Source m i | original |
-> Source m o | debounced |
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
data Args i o :: * -> * -> *
Mandatory parameters for new
.
Args | |
|
data Opts i o :: * -> * -> *
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.