import rx.Observable;
import rx.subjects.PublishSubject;
public class Subjects {
public static void main(String[] args) {
PublishSubject<String> p1 = PublishSubject.create();
PublishSubject<String> p2 = PublishSubject.create();
PublishSubject<String> p3 = PublishSubject.create();
Observable<String> stream = Observable.merge(p1, p2, p3);
stream.subscribe(i -> System.out.println("ONE => " + i));
p1.onNext("1a");
p2.onNext("2a");
p3.onNext("3a");
PublishSubject<String> p4 = PublishSubject.create();
Observable.merge(stream, p4).subscribe(i -> System.out.println(" TWO => " + i));
p4.onNext("4a");
p4.onNext("4b");
p1.onNext("1b");
p2.onNext("2b");
p3.onNext("3b");
}
}
ONE => 1a
ONE => 2a
ONE => 3a
TWO => 4a
TWO => 4b
ONE => 1b
TWO => 1b
ONE => 2b
TWO => 2b
ONE => 3b
TWO => 3b
On October 10, 2014 at 7:42:22 PM, Ben Christensen (benjchr...@gmail.com) wrote:
Here is merging 3 Subjects together into a stream.public static void main(String[] args) {
PublishSubject<Integer> p1 = PublishSubject.create();
PublishSubject<Integer> p2 = PublishSubject.create();
PublishSubject<Integer> p3 = PublishSubject.create();
Observable<Integer> stream = Observable.merge(p1, p2, p3);
stream.subscribe(i -> System.out.println(i));
p1.onNext(1);
p2.onNext(2);
p3.onNext(3);
}
Note that the merged result is an Observable and not a Subject.
On October 10, 2014 at 7:40:06 PM, Tod Antilla (tod.a...@gmail.com) wrote:
Thats a nice wrapper around a single PublishSubject.I need the the equivalent of merging several of those event buses, the salient point being the result of the merge must continue to be capable of publishing new events.If its possible to get something like the simple example at the beginning of the post to work, its possible with a more complex implementation. I can do it from entirely from scratch, but it seems odd that the merges that are possible with pure Observers are not possible with PublishSubject.More likely spending most of my time as a javascript front end developer is keeping me from seeing the basis of the compiler error and how to work around it.
PublishSubject<String> p4 = PublishSubject.create();
Observable.merge(stream, p4).subscribe(i -> System.out.println(" TWO => " + i));
On October 10, 2014 at 7:45:50 PM, Tod Antilla (tod.a...@gmail.com) wrote:
Yep. I tried that before I originally posted, but could not find in the docs on how to convert the result to a PublishSubject.
On Fri, Oct 10, 2014 at 7:42 PM, Ben Christensen <benjchr...@gmail.com> wrote:
Here is merging 3 Subjects together into a stream.
public static void main(String[] args) {
PublishSubject<Integer> p1 = PublishSubject.create();
PublishSubject<Integer> p2 = PublishSubject.create();
PublishSubject<Integer> p3 = PublishSubject.create();
Observable<Integer> stream = Observable.merge(p1, p2, p3);
stream.subscribe(i -> System.out.println(i));
p1.onNext(1);
p2.onNext(2);
p3.onNext(3);
}
Note that the merged result is an Observable and not a Subject.--
Ben Christensen
+1.310.782.5511 @benjchristensen
On October 10, 2014 at 7:40:06 PM, Tod Antilla (tod.a...@gmail.com) wrote:
Thats a nice wrapper around a single PublishSubject.I need the the equivalent of merging several of those event buses, the salient point being the result of the merge must continue to be capable of publishing new events.If its possible to get something like the simple example at the beginning of the post to work, its possible with a more complex implementation. I can do it from entirely from scratch, but it seems odd that the merges that are possible with pure Observers are not possible with PublishSubject.More likely spending most of my time as a javascript front end developer is keeping me from seeing the basis of the compiler error and how to work around it.
On Fri, Oct 10, 2014 at 7:21 PM, Ben Christensen <benjchr...@gmail.com> wrote: