Can I send multiple messages to a single event using elm-html?

I would like to send two messages to different addresses of the same event. In particular, I was faced with the fact that I had several pop-ups, and when the user clicks "Add", I would like to:

  • Close popup (send message to popup component)
  • Tell the parent component that the item should be added (send message to the parent component)

onClick has the following type "Address a → a → Attribute". Some of them seem to be unable to do this. On the other hand, it looks like such a pattern (one action leading to a multiple message) is often found in practice.

At the moment, I resorted to sending one message informing the parent about the added item, and then the parent in the pop-up menu of updates of update functions using the "Hide" action.

addButton = button [onClick context.addTaskAddress model.taskDescription] [text "Add"] 

and then in the update function of the parent component

 popup = AddTaskPopup.update AddTaskPopup.Hide model.popup 
+5
source share
1 answer

You can set up an intermediate proxy mailbox that receives a list of address and message pairs, and then calls it a push of a button.

 proxy : Mailbox (List (Address a, a)) proxy = mailbox [] 

You can then receive a broadcast signal that listens to the proxy mailbox and creates a bunch of Signal.send tasks to notify other addresses.

 port broadcast : Signal (Task x (List ())) port broadcast = let tasks = List.map (uncurry Signal.send) in Signal.map (Task.sequence << tasks) proxy.signal 

Now your onClick code will look something like this: where you pass the list of addresses and pairs of actions:

 onClick proxy.address [(address, Action1), (address, Action2)] 

Please note that in the above code, address refers to the address included in the browsing function, while proxy.address refers to the proxy mailbox we just installed.

This is a general purpose solution that will work for any number of signals.

+3
source

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


All Articles