1616package rx .javafx .sources ;
1717
1818import javafx .collections .FXCollections ;
19- import javafx .collections .ObservableList ;
2019import javafx .collections .ObservableSet ;
2120import rx .Observable ;
21+ import rx .annotations .Beta ;
2222import rx .observables .JavaFxObservable ;
23- import java . util . HashSet ;
23+
2424import java .util .Arrays ;
25+ import java .util .HashSet ;
26+
2527
2628/**
2729 * A CompositeObservable can merge multiple Observables that can be added/removed at any time,
2830 * affecting all Subscribers regardless of when they subscribed. This is especially helpful for merging
29- * multiple UI event sources.
31+ * multiple UI event sources. You can also pass a Transformer to perform
32+ * further operations on the combined Observable that is returned
3033 *
3134 * @param <T>
3235 */
36+ @ Beta
3337public final class CompositeObservable <T > {
3438
3539 private final ObservableSet <Observable <T >> sources ;
36- private final int initialCapacity ;
40+ private final Observable < T > output ;
3741
42+ /**
43+ * Creates a new CompositeObservable
44+ */
3845 public CompositeObservable () {
39- this (- 1 );
46+ this (null );
4047 }
4148
42- public CompositeObservable (int initialCapacity ) {
43- this .initialCapacity = initialCapacity ;
49+ /**
50+ * Creates a new CompositeObservable with the provided transformations applied to the returned Observable
51+ * yield from `toObservable()`. For instance, you can pass `obs -> obs.replay(1).refCount()` to make this CompositeObservable
52+ * replay one emission or `obs -> obs.share()` to multicast it.
53+ * @param transformer
54+ */
55+ public CompositeObservable (Observable .Transformer <T ,T > transformer ) {
4456 sources = FXCollections .synchronizedObservableSet (FXCollections .observableSet (new HashSet <>()));
45- }
4657
47- public Observable <T > toObservable () {
4858 Observable <T > updatingSource = Observable .merge (
4959 Observable .from (sources ).flatMap (obs -> obs .takeWhile (v -> sources .contains (obs ))),
5060 JavaFxObservable .fromObservableSetAdds (sources ).flatMap (obs -> obs .takeWhile (v -> sources .contains (obs )))
5161 );
5262
53- if (initialCapacity > 0 ) {
54- return updatingSource . cacheWithInitialCapacity ( initialCapacity ) ;
63+ if (transformer == null ) {
64+ output = updatingSource ;
5565 } else {
56- return updatingSource ;
66+ output = updatingSource . compose ( transformer ) ;
5767 }
5868 }
5969
70+ /**
71+ * Returns the `Observable` combining all the source Observables, with any transformations that were specified
72+ * on construction.
73+ * @return
74+ */
75+ public Observable <T > toObservable () {
76+ return output ;
77+ }
6078 public void add (Observable <T > observable ) {
6179 sources .add (observable );
6280 }
@@ -72,7 +90,7 @@ public void removeAll(Observable<T>... observables) {
7290 public void clear () {
7391 sources .clear ();
7492 }
75- public ObservableSet <Observable <T >> getBackingSet () {
93+ public ObservableSet <Observable <T >> getSources () {
7694 return FXCollections .unmodifiableObservableSet (sources );
7795 }
7896}
0 commit comments