reflex-frp / reflex-dom

Web applications without callbacks or side-effects. Reflex-DOM brings the power of functional reactive programming (FRP) to the web. Build HTML and other Document Object Model (DOM) data with a pure functional interface.
https://reflex-frp.org
BSD 3-Clause "New" or "Revised" License
358 stars 145 forks source link

suggested helper function: bindEvent #259

Open rprije opened 6 years ago

rprije commented 6 years ago

Hi. I've found this function quite useful. The naming reflects its similarity to monadic bind but I'm open to suggestions for alternative names:

bindEvent :: (MonadFix m, Adjustable t m, MonadHold t m) => m (Event t a) -> (a -> m (Event t b)) -> m (Event t b) bindEvent first secondFun = do rec let becomeSecondEv = fmap secondFun firstEv (firstEv, secondEvEv) <- runWithReplace first becomeSecondEv switchEvent secondEvEv

where switchEvent is from https://github.com/reflex-frp/reflex/issues/49: switchEvent :: (Reflex t, MonadHold t m) => Event t (Event t a) -> m (Event t a) switchEvent = fmap switch . hold never

I've used it for creating login widgets that behave similarly to Google's (username and password entry are two separate widgets where the second's creation is triggered by the first).

The type is convenient for creating long chains of widgets that dynamically update while carrying along the payload from previous widgets' events.

dfordivam commented 5 years ago

@rprije you may want to implement this in reflex using Reflex.Network APIs..