module Development.IDE.Core.Debouncer
( Debouncer
, registerEvent
, newAsyncDebouncer
, noopDebouncer
) where
import Control.Concurrent.Async
import Control.Concurrent.STM.Stats (atomically, atomicallyNamed)
import Control.Exception
import Control.Monad (join)
import Data.Foldable (traverse_)
import Data.Hashable
import qualified Focus
import qualified StmContainers.Map as STM
import System.Time.Extra
newtype Debouncer k = Debouncer { forall k. Debouncer k -> Seconds -> k -> IO () -> IO ()
registerEvent :: Seconds -> k -> IO () -> IO () }
newAsyncDebouncer :: (Eq k, Hashable k) => IO (Debouncer k)
newAsyncDebouncer :: forall k. (Eq k, Hashable k) => IO (Debouncer k)
newAsyncDebouncer = (Seconds -> k -> IO () -> IO ()) -> Debouncer k
forall k. (Seconds -> k -> IO () -> IO ()) -> Debouncer k
Debouncer ((Seconds -> k -> IO () -> IO ()) -> Debouncer k)
-> (Map k (Async ()) -> Seconds -> k -> IO () -> IO ())
-> Map k (Async ())
-> Debouncer k
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map k (Async ()) -> Seconds -> k -> IO () -> IO ()
forall k.
(Eq k, Hashable k) =>
Map k (Async ()) -> Seconds -> k -> IO () -> IO ()
asyncRegisterEvent (Map k (Async ()) -> Debouncer k)
-> IO (Map k (Async ())) -> IO (Debouncer k)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (Map k (Async ()))
forall key value. IO (Map key value)
STM.newIO
asyncRegisterEvent :: (Eq k, Hashable k) => STM.Map k (Async ()) -> Seconds -> k -> IO () -> IO ()
asyncRegisterEvent :: forall k.
(Eq k, Hashable k) =>
Map k (Async ()) -> Seconds -> k -> IO () -> IO ()
asyncRegisterEvent Map k (Async ())
d Seconds
0 k
k IO ()
fire = do
IO (IO ()) -> IO ()
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (IO (IO ()) -> IO ()) -> IO (IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ STM (IO ()) -> IO (IO ())
forall a. STM a -> IO a
atomically (STM (IO ()) -> IO (IO ())) -> STM (IO ()) -> IO (IO ())
forall a b. (a -> b) -> a -> b
$ do
Maybe (Async ())
prev <- Focus (Async ()) STM (Maybe (Async ()))
-> k -> Map k (Async ()) -> STM (Maybe (Async ()))
forall key value result.
Hashable key =>
Focus value STM result -> key -> Map key value -> STM result
STM.focus Focus (Async ()) STM (Maybe (Async ()))
forall (m :: * -> *) a. Monad m => Focus a m (Maybe a)
Focus.lookupAndDelete k
k Map k (Async ())
d
IO () -> STM (IO ())
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return (IO () -> STM (IO ())) -> IO () -> STM (IO ())
forall a b. (a -> b) -> a -> b
$ (Async () -> IO ()) -> Maybe (Async ()) -> IO ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ Async () -> IO ()
forall a. Async a -> IO ()
cancel Maybe (Async ())
prev
IO ()
fire
asyncRegisterEvent Map k (Async ())
d Seconds
delay k
k IO ()
fire = IO () -> IO ()
forall a. IO a -> IO a
mask_ (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
Async ()
a <- ((forall a. IO a -> IO a) -> IO ()) -> IO (Async ())
forall a. ((forall a. IO a -> IO a) -> IO a) -> IO (Async a)
asyncWithUnmask (((forall a. IO a -> IO a) -> IO ()) -> IO (Async ()))
-> ((forall a. IO a -> IO a) -> IO ()) -> IO (Async ())
forall a b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
unmask -> IO () -> IO ()
forall a. IO a -> IO a
unmask (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
Seconds -> IO ()
sleep Seconds
delay
IO ()
fire
STM () -> IO ()
forall a. STM a -> IO a
atomically (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ k -> Map k (Async ()) -> STM ()
forall key value. Hashable key => key -> Map key value -> STM ()
STM.delete k
k Map k (Async ())
d
Maybe (Async ())
prev <- String -> STM (Maybe (Async ())) -> IO (Maybe (Async ()))
forall a. String -> STM a -> IO a
atomicallyNamed String
"debouncer" (STM (Maybe (Async ())) -> IO (Maybe (Async ())))
-> STM (Maybe (Async ())) -> IO (Maybe (Async ()))
forall a b. (a -> b) -> a -> b
$ Focus (Async ()) STM (Maybe (Async ()))
-> k -> Map k (Async ()) -> STM (Maybe (Async ()))
forall key value result.
Hashable key =>
Focus value STM result -> key -> Map key value -> STM result
STM.focus (Focus (Async ()) STM (Maybe (Async ()))
forall (m :: * -> *) a. Monad m => Focus a m (Maybe a)
Focus.lookup Focus (Async ()) STM (Maybe (Async ()))
-> Focus (Async ()) STM ()
-> Focus (Async ()) STM (Maybe (Async ()))
forall a b.
Focus (Async ()) STM a
-> Focus (Async ()) STM b -> Focus (Async ()) STM a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Async () -> Focus (Async ()) STM ()
forall (m :: * -> *) a. Monad m => a -> Focus a m ()
Focus.insert Async ()
a) k
k Map k (Async ())
d
(Async () -> IO ()) -> Maybe (Async ()) -> IO ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ Async () -> IO ()
forall a. Async a -> IO ()
cancel Maybe (Async ())
prev
noopDebouncer :: Debouncer k
noopDebouncer :: forall k. Debouncer k
noopDebouncer = (Seconds -> k -> IO () -> IO ()) -> Debouncer k
forall k. (Seconds -> k -> IO () -> IO ()) -> Debouncer k
Debouncer ((Seconds -> k -> IO () -> IO ()) -> Debouncer k)
-> (Seconds -> k -> IO () -> IO ()) -> Debouncer k
forall a b. (a -> b) -> a -> b
$ \Seconds
_ k
_ IO ()
a -> IO ()
a