Reflex: create a drop delay event

I use Reflex inside Haskell, and try to create an event that fires (for questions) a given period of time after another, say 2 seconds. However, the counter must reset whenever the source event occurs, so if the source event fires twice for 1 second, only one second event is needed: 2 seconds after the last original event.

I managed to implement this behavior with

delayedReset :: MonadWidget t m => Event t () -> m (Event t ())
delayedReset ev = fmap (switch . current) . widgetHold (return never) $ timer <$ ev
  where
    timer = delay 2 =<< getPostBuild

but it seems unnecessary to use widgetHold; it seems that we really only need a MonadHold restriction. Is there a more idiomatic way to write this function?

+4
source share
1 answer

I seem to have missed the pretty obvious:

delayedReset :: MonadWidget t m => Event t () -> m (Event t ())
delayedReset ev = do
    endEv <- delay 2 ev
    countDyn <- foldDyn ($) (0 :: Int) $ leftmost
      [ (+1) <$ ev, subtract 1 <$ endEv ]
    return $ attachPromptlyDynWithMaybe ifZero countDyn endEv
  where
    ifZero 0 a = Just a
    ifZero _ _ = Nothing

, .

, , . , , .

0

Source: https://habr.com/ru/post/1692555/


All Articles