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. See the fast-logger package for an
10module Control.Debounce
13 , defaultDebounceSettings
21import Control.Concurrent (forkIO, threadDelay)
22import Control.Concurrent.MVar (newEmptyMVar, takeMVar, tryPutMVar)
23import Control.Exception (SomeException, handle, mask_)
24import Control.Monad (forever, void)
26-- | Settings to control how debouncing should work.
28-- This should be constructed using @defaultDebounceSettings@ and record
29-- update syntax, e.g.:
32-- let set = defaultDebounceSettings { debounceAction = flushLog }
36data DebounceSettings = DebounceSettings
38 -- ^ Microseconds lag required between subsequence calls to the debounced
41 -- Default: 1 second (1000000)
44 , debounceAction :: IO ()
45 -- ^ Action to be performed.
47 -- Note: all exceptions thrown by this action will be silently discarded.
49 -- Default: does nothing.
54-- | Default value for creating a @DebounceSettings@.
57defaultDebounceSettings :: DebounceSettings
58defaultDebounceSettings = DebounceSettings
59 { debounceFreq = 1000000
60 , debounceAction = return ()
63-- | Generate an action which will trigger the debounced action to be
64-- performed. The action will either be performed immediately, or after the
65-- current cooldown period has expired.
68mkDebounce :: DebounceSettings -> IO (IO ())
69mkDebounce (DebounceSettings freq action) = do
71 mask_ $ void $ forkIO $ forever $ do
75 return $ void $ tryPutMVar baton ()
77ignoreExc :: IO () -> IO ()
78ignoreExc = 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