@@ -4,14 +4,15 @@
4module Control.Debounce.Internal (
4module Control.Debounce.Internal (
5 DebounceSettings(..)
5 DebounceSettings(..)
6 , DebounceEdge(..)
6 , DebounceEdge(..)
7 , mkDebounceInternal
9 , mkDebounceInternal
10import Control.Concurrent (forkIO)
12import Control.Concurrent (forkIO)
11import Control.Concurrent.MVar (takeMVar, tryPutMVar, tryTakeMVar, MVar)
13import Control.Concurrent.MVar (takeMVar, tryPutMVar, tryTakeMVar, MVar)
12import Control.Exception (SomeException, handle, mask_)
14import Control.Exception (SomeException, handle, mask_)
13−import Control.Monad (forever, void, when)
15+import Control.Monad (forever, void)
14−import Data.Maybe (isJust)
16-- | Settings to control how debouncing should work.
17-- | Settings to control how debouncing should work.
@@ -42,7 +43,7 @@ data DebounceSettings = DebounceSettings
42 -- ^ Whether to perform the action on the leading edge or trailing edge of
43 -- ^ Whether to perform the action on the leading edge or trailing edge of
43 -- the timeout.
44 -- the timeout.
45− -- Default: trailingEdge.
46+ -- Default: 'trailingEdge'.
47 -- @since 0.1.6
48 -- @since 0.1.6
@@ -61,6 +62,22 @@ data DebounceEdge =
61 -- happens during the cooldown, it has no effect.
62 -- happens during the cooldown, it has no effect.
62 deriving (Show, Eq)
63 deriving (Show, Eq)
66+-- | Perform the action immediately, and then begin a cooldown period.
67+-- If the trigger happens again during the cooldown, wait until the end of the cooldown
68+-- and then perform the action again, then enter a new cooldown period.
71+leadingEdge :: DebounceEdge
74+-- | Start a cooldown period and perform the action when the period ends. If another trigger
75+-- happens during the cooldown, it has no effect.
78+trailingEdge :: DebounceEdge
79+trailingEdge = Trailing
64mkDebounceInternal :: MVar () -> (Int -> IO ()) -> DebounceSettings -> IO (IO ())
81mkDebounceInternal :: MVar () -> (Int -> IO ()) -> DebounceSettings -> IO (IO ())
65mkDebounceInternal baton delayFn (DebounceSettings freq action edge) = do
82mkDebounceInternal baton delayFn (DebounceSettings freq action edge) = do
66 mask_ $ void $ forkIO $ forever $ do
83 mask_ $ void $ forkIO $ forever $ do