-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Subject Factory with Multicast #1515
Support Subject Factory with Multicast #1515
Conversation
/cc @headinthebox and @mattpodwysocki This is based on our email conversation and is a proposed change to make |
RxJava-pull-requests #1436 SUCCESS |
Here is another example to demonstrate why this change is probably a good thing. Output without this pull request:
With this pull request (what a user would expect):
Example code: import java.util.List;
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;
public class DebounceBuffer {
public static void main(String args[]) {
/* The following will emit a buffered list as it is debounced */
// first we multicast the stream ... using refCount so it handles the subscribe/unsubscribe
Observable<Integer> burstStream = intermittentBursts().take(20).publish().refCount();
// then we get the debounced version
Observable<Integer> debounced = burstStream.debounce(10, TimeUnit.MILLISECONDS);
// then the buffered one that uses the debounced stream to demark window start/stop
Observable<List<Integer>> buffered = burstStream.buffer(debounced);
// then we subscribe to the buffered stream so it does what we want
buffered.toBlocking().forEach(System.out::println);
System.out.println("Start again ...");
buffered.toBlocking().forEach(System.out::println);
}
public static Observable<Integer> intermittentBursts() {
return Observable.create((Subscriber<? super Integer> s) -> {
while (!s.isUnsubscribed()) {
// burst some number of items
for (int i = 0; i < Math.random() * 20; i++) {
s.onNext(i);
}
try {
// sleep for a random amount of time
Thread.sleep((long) (Math.random() * 1000));
} catch (Exception e) {
// do nothing
}
}
}).subscribeOn(Schedulers.newThread()); // use newThread since we are using sleep to block
}
} |
+1 here for the change. It proves subjects are nasty things. And that anything that has side-effects should be ()=>... I will loose some quizzes in my Rx course, because it is fun to show what happens when you reconnect to an infinite vs a finite sequence. |
:-) Thanks for the review. Merging this... |
Support Subject Factory with Multicast
This is a proposed change to allow using a
Subject
factory withmulticast
and for methods such aspublish()
andreplay()
to use the factory so that each timeConnectableObservable.connect()
is invoked, a newSubject
is created and used. Otherwise, anObservable
sequence can not be reused.Use Case
Output without these changes:
Output with changes:
Test Case