on develop help sign in
Add debounce to Reflex.Dom.Time
commitb630f158076fc9215ecb708ab159ff3348e7a39e authorAli Abrar <aliabrar@gmail.com> dateWed 23 Sep 2015, 14:04 (11 years ago) parentc61c99a
1 file changed+10
src/Reflex/Dom/Time.hs+10
src/Reflex/Dom/Time.hs+10
@@ -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