haskellreflex

Reflex: Delaying events based on a behavior


Using reflex-frp and reflex-dom I need to delay events based on a behavior. I have:

delayEvent :: (MonadWidget t m, Reflex t) 
           => Event t () -> Behavior t NominalDiffTime -> m (Event t ())
delayEvent e b = switch . current <$> widgetHold (return never) eDelsM
  where
    eDelsM = (`delay` e) <$> b <@ e -- Event t (m (Event t ()))

however the return event never fires. Is there a simple mistake I'm making here or does the whole approach need rethinking?


Solution

  • Apparently, the delay will never fire because when the widget defined by eDelsM is active from event e0, it will be waiting until the next event, e1 to create a delay. However, by the time the next instance of the event fires, e1, a new instance of the eDelsM widget will become active and start waiting for the next instance of the event, e2. The solution is to trigger a new event as soon as eDelsM is recreated.

    eDelsM = (\t -> getPostBuild >>= delay t) <$> b <@ e