Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upCannot make Address for dispatching events to multiple Addresses? #277
Comments
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
TheSeamau5
Jun 13, 2015
Contributor
I wonder what is your use case that requires two addresses, but from using this API a bit, I've realized that the fewer address/mailboxes I have in a system, the better. The following does not answer your concern but perhaps it can help with solving the underlying problem behind your question.
When you have a component that emits multiple events (of potentially different types), you can always send all these events to a single address, provided the values you want to send are wrapped in some sort of union type like:
type ButtonAction
= Click (Int, Int)
| Hover Float
| NoOpAnd then, you can have a single address of type
Address ButtonActionYou can even take this logic one step further. Suppose you have a container that can have buttons, checkboxes, input boxes. You can then make a union type for each of these actions (themselves union types).
type ContainerAction
= Button ButtonAction
| Checkbox CheckboxAction
| Inputbox InputboxAction
| NoOpYou use forwardTo to go from the button to the container.
containerAddress : Address ContainerAction
buttonAddress : Address ButtonAction
buttonAddress =
Signal.forwardTo Button containerAddress
checkboxAddress : Address CheckboxAction
checkboxAddress =
Signal.forwardTo Checkbox containerAddress
inputboxAddress : Address InputboxAction
inputboxAddress =
Signal.forwardTo Inputbox containerAddressAnd then do the same for further container types, etc... Basically, you end up with a big tree of Union Types and use pattern matching to navigate through these actions in your update logic.
In the end, you end up with one single giant mailbox for all of your actions (and perhaps another one for all of your tasks).
|
I wonder what is your use case that requires two addresses, but from using this API a bit, I've realized that the fewer address/mailboxes I have in a system, the better. The following does not answer your concern but perhaps it can help with solving the underlying problem behind your question. When you have a component that emits multiple events (of potentially different types), you can always send all these events to a single address, provided the values you want to send are wrapped in some sort of union type like: type ButtonAction
= Click (Int, Int)
| Hover Float
| NoOpAnd then, you can have a single address of type Address ButtonActionYou can even take this logic one step further. Suppose you have a container that can have buttons, checkboxes, input boxes. You can then make a union type for each of these actions (themselves union types). type ContainerAction
= Button ButtonAction
| Checkbox CheckboxAction
| Inputbox InputboxAction
| NoOpYou use containerAddress : Address ContainerAction
buttonAddress : Address ButtonAction
buttonAddress =
Signal.forwardTo Button containerAddress
checkboxAddress : Address CheckboxAction
checkboxAddress =
Signal.forwardTo Checkbox containerAddress
inputboxAddress : Address InputboxAction
inputboxAddress =
Signal.forwardTo Inputbox containerAddressAnd then do the same for further container types, etc... Basically, you end up with a big tree of Union Types and use pattern matching to navigate through these actions in your update logic. In the end, you end up with one single giant mailbox for all of your actions (and perhaps another one for all of your tasks). |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
jinjor
Jun 14, 2015
Contributor
Great answer! thanks.
I'm using WebSocket in Elm.
Until the useful modules are ready, I temporary use many ports.
I often write some boilerplate like below...
sendChatMB : Signal.Mailbox String
sendChatMB = Signal.mailbox ""
port sendChat : Signal String
port sendChat = sendChatMB.signal
chatInput = input [onEnter sendChatMB.address] []
That is the reason I need many addresses, but I never think this is a good practice.
Now I see your strategy is much clearer.
Maybe the signals for porting can be made by filtering the integrated Action stream.
Ok, I'll try refactoring it :)
|
Great answer! thanks. I'm using WebSocket in Elm.
That is the reason I need many addresses, but I never think this is a good practice. Now I see your strategy is much clearer. Ok, I'll try refactoring it :) |
jinjor
closed this
Jun 14, 2015
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
Thanks for writing up a great answer @TheSeamau5! Much appreciated :) |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
amitaibu
commented
Sep 1, 2015
|
@TheSeamau5 your answers are always the best, thanks! |
jinjor commentedJun 13, 2015
Hi, I have a question.
I am making a new component that triggers multiple events.
The parent of this component has two Addresses
addressAandaddressB.When the parent receives
EventA, then it should be sent toaddressA.In the same way,
EventBshould be sent toaddressB.But I cannot find a way to create that kind of Address.
Signal.forwardTois useful but only for single address.I also tried to define another version of
forwardTo, but got a compile error.I saw the
Addressas a data constructor cannot be used outside.(Is this an expected beavior?)
So, is there any good way to do such a thing?