@@ -1,3 +1,4 @@
1+{-# LANGUAGE ScopedTypeVariables #-}
12module Reflex.Dom.Time where
23
34import Reflex
@@ -50,3 +51,12 @@ delay :: MonadWidget t m => NominalDiffTime -> Event t a -> m (Event t a)
5051delay dt e = performEventAsync $ ffor e $ \a cb -> liftIO $ void $ forkIO $ do
5152 threadDelay $ ceiling $ dt * 1000000
5253 cb a
54+
55+-- | Block occurrences of an Event until th given number of seconds elapses without
56+-- the Event firing, at which point the last occurrence of the Event will fire.
57+debounce :: MonadWidget t m => NominalDiffTime -> Event t a -> m (Event t a)
58+debounce dt e = do
59+ n :: Dynamic t Integer <- count e
60+ let tagged = attachDynWith (,) n e
61+ delayed <- delay dt tagged
62+ return $ attachWithMaybe (\n' (t, v) -> if n' == t then Just v else Nothing) (current n) delayed