This question is perhaps aimed at the creator of ReactFX, but others are welcome to answer.
I am currently starting to use ReactFX for data sharing and event handling within a JavaFX application. My question is how can a class subscribe to listen to events from two(or more) different EventStreams
. Suppose that in a Controller class
I have a textfield
that may be updated with a new text(String)
, so this class would implement Consumer<String>
. But then you may also want for this textfield
to be updated with a new Integer
(for example) coming from a totally different source, so it would have to implement Consumer<Integer>
, only you can't do that because it already implements Consumer<String>
.
I thought about creating a bundle class with an id field(with an Enum
for example) and an Object field
containing the data, lets name it ReactFXEventBundle
. Only instances of this class will be able to be used as Events
, where the consumer can identify the type of Event by analyzing the id field. Therefore, all Consumer classes would implement Consumer<ReactFXEventBundle>
. Would this be the right approach?
To make sure we are on the same page, I assume your situation looks something like this
and you want to route both
strings
andintegers
to TextField'stextProperty()
. This is how you do that:In words, you convert the stream of integers to a stream of strings, merge the two string streams and feed the merged stream into the text property of the field. (
feedTo(property)
is just a shorthand forsubscribe(property::set)
).Note that your controller class doesn't have to implement
Consumer
directly. The consumer instance is created from theproperty::set
method reference.