0

In regular JavaFX i would create a binding on 2 or more observables like this:

xxxProperty().bind(Bindings.createObjectBinding(() -> {...}, observable1, observable2, ...));

In ReactFx I was shown that I can create a binding like this:

xxxProperty().bind(val.map(value -> {...}));

but how do i create a binding on 2 or more ReactFX observables (Val and/or Var)?

Mark
  • 2,167
  • 4
  • 32
  • 64

1 Answers1

1

For the general case, use one of these:

static <T> Val<T> create(Supplier<? extends T> computeValue, EventStream<?> invalidations)
static <T> Val<T> create(Supplier<? extends T> computeValue, Observable... dependencies)

For combining 2-6 values, there are convenience methods provided:

static <A,B,R> Val<R> combine(ObservableValue<A> src1, ObservableValue<B> src2, BiFunction<? super A,? super B,? extends R> f)
static <A,B,C,R> Val<R> combine(ObservableValue<A> src1, ObservableValue<B> src2, ObservableValue<C> src3, TriFunction<? super A,? super B,? super C,? extends R> f)
static <A,B,C,D,R> Val<R> combine(ObservableValue<A> src1, ObservableValue<B> src2, ObservableValue<C> src3, ObservableValue<D> src4, TetraFunction<? super A,? super B,? super C,? super D,? extends R> f)
static <A,B,C,D,E,R> Val<R> combine(ObservableValue<A> src1, ObservableValue<B> src2, ObservableValue<C> src3, ObservableValue<D> src4, ObservableValue<E> src5, PentaFunction<? super A,? super B,? super C,? super D,? super E,? extends R> f)
static <A,B,C,D,E,F,R> Val<R> combine(ObservableValue<A> src1, ObservableValue<B> src2, ObservableValue<C> src3, ObservableValue<D> src4, ObservableValue<E> src5, ObservableValue<F> src6, HexaFunction<? super A,? super B,? super C,? super D,? super E,? super F,? extends R> f)

All of these are static methods on Val.

Tomas Mikula
  • 6,537
  • 25
  • 39
  • Very nice! If i want something similar for a `Var` i just do the above and then create a Var from the resulting Val? Also let me thank you personally for ReacFX. it appears to solve quite a few problems for me. My only complaint is the lack of javadoc or good tutorial. It's an advanced library (i would say) so that would help a lot. – Mark Feb 07 '17 at 21:31
  • Depends on what you mean by "something similar for a `Var`". When you `map` or `combine` values from several `Var`s using function `f`, then in general `f` is not reversible, so it's not (in general) well defined what should be done to the input `Var`s when you call `setValue` on the output `Var`. If in your case it _is_ well defined, then use [`Val#asVar`](http://www.reactfx.org/javadoc/2.0-M5/org/reactfx/value/Val.html#asVar-java.util.function.Consumer-) to do it (i.e. change the inputs so that as a result, the output will change to the desired value). – Tomas Mikula Feb 08 '17 at 03:54
  • Or perhaps you just want to be able to manually override the computed value. In that case, just create a new `Var` and update it according to the combined `Val`, as well as manually. Glad you are liking ReactFX. Yeah, documentation is an eternal struggle... – Tomas Mikula Feb 08 '17 at 03:57
  • Yes `Val.asVar` is what i meant. didn't consider reversibility. Do you have collaborators on the project? maybe get people to help with the docs. – Mark Feb 08 '17 at 10:12
  • Be careful with `combine`. If any of the values is `null`, a listener on it will never trigger. Any values that can be `null` must be wrapped with `val.orElseConst(x)`... this is a bit of pitfall that has caught me a few times already. – john16384 Apr 15 '19 at 14:03