Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for `flatten :: Event [a] -> Event a` in Reactive Banana

I'm looking for something like flatten :: Event [a] -> Event a (swap [] with Foldable f => f if you want) which would generate a separate event for each a in an Event's list, like split in an old version of sodium.

I suspect that this is somehow possible with switchE, but then I'd need a function of type [a] -> Event a.

I could possibly craft that myself with newEvent, but is there a function built-in to reactive-banana?

Edit:

Actually, I'm not so sure I can implement that with newEvent after all.

flatten :: Foldable f => f a -> Banana.MomentIO (Banana.Event a)
flatten xs = do
  (event, fire) <- Banana.newEvent
  liftIO $ forkIO $ mapM_ fire xs
  return event 

Will fire block until there are subscribers or will it just return immediately if there aren't any?

Edit 2:

Looking at the implementation of newAddHandler my implementation above won't work, because all events are possibly fired before any handlers can register.

like image 793
Sebastian Graf Avatar asked Apr 29 '16 16:04

Sebastian Graf


1 Answers

This appears to be impossible. According to the notes in Heinrich Apfelmus's blog, Event doesn't support simultaneous occurrences. This is a relatively recent change; the post is dated August last year and v1.0 was released in October. It certainly wasn't the case when I originally learned Reactive Banana a few years ago.

But Event [a] seems like a reasonable way to represent a set of coincidental events in the first place. Why do you need to flatten it?

like image 187
Benjamin Hodgson Avatar answered Sep 27 '22 19:09

Benjamin Hodgson