1{-# LANGUAGE ScopedTypeVariables #-}
2-- | Debounce an action, ensuring it doesn't occur more than once for a given
5-- This is useful as an optimization, for example to ensure that logs are only
6-- flushed to disk at most once per second.
11-- printString <- 'mkDebounce' 'defaultDebounceSettings'
12-- { 'debounceAction' = putStrLn "Running action"
13-- , 'debounceFreq' = 5000000 -- 5 seconds
23-- See the fast-logger package ("System.Log.FastLogger") for real-world usage.
26module Control.Debounce
29 , defaultDebounceSettings
37import Control.Concurrent (forkIO, threadDelay)
38import Control.Concurrent.MVar (newEmptyMVar, takeMVar, tryPutMVar)
39import Control.Exception (SomeException, handle, mask_)
40import Control.Monad (forever, void)
42-- | Settings to control how debouncing should work.
44-- This should be constructed using 'defaultDebounceSettings' and record
45-- update syntax, e.g.:
48-- let settings = 'defaultDebounceSettings' { 'debounceAction' = flushLog }
52data DebounceSettings = DebounceSettings
54 -- ^ Microseconds lag required between subsequence calls to the debounced
57 -- Default: 1 second (1000000)
60 , debounceAction :: IO ()
61 -- ^ Action to be performed.
63 -- Note: all exceptions thrown by this action will be silently discarded.
65 -- Default: does nothing.
70-- | Default value for creating a 'DebounceSettings'.
73defaultDebounceSettings :: DebounceSettings
74defaultDebounceSettings = DebounceSettings
75 { debounceFreq = 1000000
76 , debounceAction = return ()
79-- | Generate an action which will trigger the debounced action to be
80-- performed. The action will either be performed immediately, or after the
81-- current cooldown period has expired.
84mkDebounce :: DebounceSettings -> IO (IO ())
85mkDebounce (DebounceSettings freq action) = do
87 mask_ $ void $ forkIO $ forever $ do
91 return $ void $ tryPutMVar baton ()
93ignoreExc :: IO () -> IO ()
94ignoreExc = handle $ \(_ :: SomeException) -> 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