on master help sign in
All the simple changes from review
commit17f92cd27b7c3f657cd30db47963dea7d74147fb authorTom McLaughlin <pyro777@gmail.com> dateThu 4 Jul 2019, 13:48 (7 years ago) parent2a6d861
3 files changed+17−10
auto-update/Control/Debounce.hs+1−1 auto-update/Control/Debounce/Internal.hs+14−7 auto-update/test/Control/DebounceSpec.hs+2−2
auto-update/Control/Debounce.hs+1−1
@@ -56,4 +56,4 @@ defaultDebounceSettings = DI.DebounceSettings
5656mkDebounce :: DI.DebounceSettings -> IO (IO ())
5757mkDebounce settings = do
5858 baton <- newEmptyMVar
59− DI.mkDebounce baton threadDelay settings
59+ DI.mkDebounceInternal baton threadDelay settings
auto-update/Control/Debounce/Internal.hs+14−7
@@ -1,9 +1,10 @@
11{-# LANGUAGE ScopedTypeVariables #-}
22
3+-- | Unstable API which exposes internals for testing.
34module Control.Debounce.Internal (
45 DebounceSettings(..)
56 , DebounceEdge(..)
6− , mkDebounce
7+ , mkDebounceInternal
78 ) where
89
910import Control.Concurrent (forkIO)
@@ -44,9 +45,13 @@ data DebounceSettings = DebounceSettings
4445 --
4546 -- Default: Leading.
4647 --
47− -- @since 0.2.0
48+ -- @since 0.1.6
4849 }
4950
51+-- | Setting to control whether the action happens at the leading or trailing
52+-- edge of the timeout.
53+--
54+-- @since 0.1.6
5055data DebounceEdge =
5156 Leading
5257 -- ^ The action will either be performed immediately, or after the current cooldown period has expired.
@@ -57,16 +62,18 @@ data DebounceEdge =
5762 -- the debounced function was invoked again during the cooldown.
5863 deriving (Show, Eq)
5964
60−mkDebounce :: MVar () -> (Int -> IO ()) -> DebounceSettings -> IO (IO ())
61−mkDebounce baton delayFn (DebounceSettings freq action edge) = do
65+mkDebounceInternal :: MVar () -> (Int -> IO ()) -> DebounceSettings -> IO (IO ())
66+mkDebounceInternal baton delayFn (DebounceSettings freq action edge) = do
6267 mask_ $ void $ forkIO $ forever $ do
6368 takeMVar baton
64− if edge `elem` [Leading, LeadingAndTrailing] then ignoreExc action >> runDelay False baton
65− else runDelay True baton
69+ case edge of
70+ Leading -> ignoreExc action >> runDelay False
71+ LeadingAndTrailing -> ignoreExc action >> runDelay False
72+ Trailing -> runDelay True
6673
6774 return $ void $ tryPutMVar baton ()
6875
69− where runDelay hasDelayedInitialActivation baton = do
76+ where runDelay hasDelayedInitialActivation = do
7077 delayFn freq
7178
7279 case edge of
auto-update/test/Control/DebounceSpec.hs+2−2
@@ -126,7 +126,7 @@ getDebounce edge = do
126126
127127 baton <- newEmptyMVar
128128
129− debounced <- DI.mkDebounce baton waitAction defaultDebounceSettings {
129+ debounced <- DI.mkDebounceInternal baton waitAction defaultDebounceSettings {
130130 debounceFreq = 5000000 -- unused
131131 , debounceAction = action
132132 , debounceEdge = edge
@@ -141,7 +141,7 @@ pause = threadDelay 100000
141141waitForBatonToBeTaken :: MVar () -> IO ()
142142waitForBatonToBeTaken baton = waitUntil 5 $ tryReadMVar baton >>= (`shouldBe` Nothing)
143143
144−-- | Wait up to n seconds for an actual to complete without throwing an HUnitFailure
144+-- | Wait up to n seconds for an action to complete without throwing an HUnitFailure
145145waitUntil :: Int -> IO a -> IO ()
146146waitUntil n action = recovering policy [handler] (\_status -> void action)
147147 where policy = constantDelay 1000 `mappend` limitRetries (n * 1000) -- 1ms * n * 1000 tries = n seconds