Search code examples
javajavafxreactive-programmingreactfx

ReactFX EventStreams and Replay Behavior?


Is there any functionality for an EventStream in ReactFX to cache the last value so it can be replayed to new subscribers? The RxJava Observable has a lot of procedures to do these kinds of tasks. I was hoping the EventStream might have a similiar functionality... unless I'm overlooking a reason why I would not want to do this in a GUI and I should stick to publish-only paradigms.

EventStream<Boolean> selectedEvt = EventStreams.changesOf(selected.selectedProperty())
.map(v -> v.getNewValue()).cache(1);

Solution

  • ReactFX does not have those. The idea is that if you want to remember a value, use an ObservableValue/Val instead of an EventStream. Turn the ObservableValue/Val into an EventStream when necessary. Your above example can be rewritten more simply using valuesOf:

    EventStream<Boolean> selectedEvt = EventStreams.valuesOf(selected.selectedProperty());
    

    Streams created by valuesOf emit the current value of the underlying ObservableValue immediately after subscription, so they mimic the replay behavior you describe.