Skip to content

subscribeOn + groupBy #869

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

Merged
merged 12 commits into from
Feb 14, 2014
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion rxjava-core/src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -7052,9 +7052,33 @@ public final Subscription subscribe(Subscriber<? super T> observer, Scheduler sc
* @return the source Observable modified so that its subscriptions and unsubscriptions happen
* on the specified {@link Scheduler}
* @see <a href="https://github.com/Netflix/RxJava/wiki/Observable-Utility-Operators#wiki-subscribeon">RxJava Wiki: subscribeOn()</a>
* @see #subscribeOn(rx.Scheduler, int)
*/
public final Observable<T> subscribeOn(Scheduler scheduler) {
return nest().lift(new OperatorSubscribeOn<T>(scheduler));
return nest().lift(new OperatorSubscribeOn<T>(scheduler, false));
}

/**
* Asynchronously subscribes and unsubscribes Observers to this Observable on the specified {@link Scheduler}
* and allows buffering some events emitted from the source in the time gap between the original and
* actual subscription, and any excess events will block the source until the actual subscription happens.
* <p>
* This overload should help mitigate issues when subscribing to a PublishSubject (and derivatives
* such as GroupedObservable in operator groupBy) and events fired between the original and actual subscriptions
* are lost.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/subscribeOn.png">
*
* @param scheduler
* the {@link Scheduler} to perform subscription and unsubscription actions on
* @param bufferSize the number of events to buffer before blocking the source while in the time gap,
* negative value indicates an unlimited buffer
* @return the source Observable modified so that its subscriptions and unsubscriptions happen
* on the specified {@link Scheduler}
* @see <a href="https://github.com/Netflix/RxJava/wiki/Observable-Utility-Operators#wiki-subscribeon">RxJava Wiki: subscribeOn()</a>
*/
public final Observable<T> subscribeOn(Scheduler scheduler, int bufferSize) {
return nest().lift(new OperatorSubscribeOn<T>(scheduler, true, bufferSize));
}

/**
Expand Down
8 changes: 8 additions & 0 deletions rxjava-core/src/main/java/rx/observers/TestSubscriber.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ public void awaitTerminalEvent(long timeout, TimeUnit unit) {
}
}

public void awaitTerminalEventAndUnsubscribeOnTimeout(long timeout, TimeUnit unit) {
try {
awaitTerminalEvent(timeout, unit);
} catch (RuntimeException e) {
unsubscribe();
}
}

public Thread getLastSeenThread() {
return lastSeenThread;
}
Expand Down
193 changes: 193 additions & 0 deletions rxjava-core/src/main/java/rx/operators/BufferUntilSubscriber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.operators;

import java.util.LinkedList;
import java.util.Queue;
import rx.Subscriber;
import rx.subscriptions.CompositeSubscription;

/**
* Buffers the incoming events until notified, then replays the
* buffered events and continues as a simple pass-through subscriber.
* @param <T> the streamed value type
*/
public class BufferUntilSubscriber<T> extends Subscriber<T> {
/** The actual subscriber. */
private final Subscriber<? super T> actual;
/** Indicate the pass-through mode. */
private volatile boolean passthroughMode;
/** Protect mode transition. */
private final Object gate = new Object();
/** The buffered items. */
private final Queue<Object> queue = new LinkedList<Object>();
/** The queue capacity. */
private final int capacity;
/** Null sentinel (in case queue type is changed). */
private static final Object NULL_SENTINEL = new Object();
/** Complete sentinel. */
private static final Object COMPLETE_SENTINEL = new Object();
/**
* Container for an onError event.
*/
private static final class ErrorSentinel {
final Throwable t;

public ErrorSentinel(Throwable t) {
this.t = t;
}

}
/**
* Constructor that wraps the actual subscriber and shares its subscription.
* @param capacity the queue capacity to accept before blocking, negative value indicates an unbounded queue
* @param actual
*/
public BufferUntilSubscriber(int capacity, Subscriber<? super T> actual) {
super(actual);
this.actual = actual;
this.capacity = capacity;
}
/**
* Constructor that wraps the actual subscriber and uses the given composite
* subscription.
* @param capacity the queue capacity to accept before blocking, negative value indicates an unbounded queue
* @param actual
* @param cs
*/
public BufferUntilSubscriber(int capacity, Subscriber<? super T> actual, CompositeSubscription cs) {
super(cs);
this.actual = actual;
this.capacity = capacity;
}

/**
* Call this method to replay the buffered events and continue as a pass-through subscriber.
* If already in pass-through mode, this method is a no-op.
*/
public void enterPassthroughMode() {
if (!passthroughMode) {
synchronized (gate) {
if (!passthroughMode) {
while (!queue.isEmpty()) {
Object o = queue.poll();
if (!actual.isUnsubscribed()) {
if (o == NULL_SENTINEL) {
actual.onNext(null);
} else
if (o == COMPLETE_SENTINEL) {
actual.onCompleted();
} else
if (o instanceof ErrorSentinel) {
actual.onError(((ErrorSentinel)o).t);
} else
if (o != null) {
@SuppressWarnings("unchecked")
T v = (T)o;
actual.onNext(v);
} else {
throw new NullPointerException();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If queue remains LinkedList, we can drop NULL_SENTINEL and this case here.

}
}
}
passthroughMode = true;
gate.notifyAll();
}
}
}
}
@Override
public void onNext(T t) {
if (!passthroughMode) {
synchronized (gate) {
if (!passthroughMode) {
if (capacity < 0 || queue.size() < capacity) {
queue.offer(t != null ? t : NULL_SENTINEL);
return;
}
try {
while (!passthroughMode) {
gate.wait();
}
if (actual.isUnsubscribed()) {
return;
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
actual.onError(ex);
return;
}
}
}
}
actual.onNext(t);
}

@Override
public void onError(Throwable e) {
if (!passthroughMode) {
synchronized (gate) {
if (!passthroughMode) {
if (capacity < 0 || queue.size() < capacity) {
queue.offer(new ErrorSentinel(e));
return;
}
try {
while (!passthroughMode) {
gate.wait();
}
if (actual.isUnsubscribed()) {
return;
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
actual.onError(ex);
return;
}
}
}
}
actual.onError(e);
}

@Override
public void onCompleted() {
if (!passthroughMode) {
synchronized (gate) {
if (!passthroughMode) {
if (capacity < 0 || queue.size() < capacity) {
queue.offer(COMPLETE_SENTINEL);
return;
}
try {
while (!passthroughMode) {
gate.wait();
}
if (actual.isUnsubscribed()) {
return;
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
actual.onError(ex);
return;
}
}
}
}
actual.onCompleted();
}

}
Loading