rx.operators.OperationSkipUntil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxjava-core Show documentation
Show all versions of rxjava-core Show documentation
rxjava-core developed by Netflix
/**
* 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.concurrent.atomic.AtomicBoolean;
import rx.Observable;
import rx.Observable.OnSubscribeFunc;
import rx.Observer;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;
import rx.subscriptions.SerialSubscription;
/**
* Skip elements from the source Observable until the secondary
* observable fires an element.
*
* @see MSDN: Observable.SkipUntil
*/
public class OperationSkipUntil implements OnSubscribeFunc {
protected final Observable source;
protected final Observable other;
public OperationSkipUntil(Observable source, Observable other) {
this.source = source;
this.other = other;
}
@Override
public Subscription onSubscribe(Observer super T> t1) {
return new ResultManager(t1).init();
}
/** Manage the source and other observers. */
private class ResultManager implements Subscription, Observer {
final Observer super T> observer;
final CompositeSubscription cancel;
final Object guard = new Object();
final AtomicBoolean running = new AtomicBoolean();
public ResultManager(Observer super T> observer) {
this.observer = observer;
this.cancel = new CompositeSubscription();
}
public ResultManager init() {
SerialSubscription toSource = new SerialSubscription();
SerialSubscription toOther = new SerialSubscription();
cancel.add(toSource);
cancel.add(toOther);
toSource.setSubscription(source.subscribe(this));
toOther.setSubscription(other.subscribe(new OtherObserver(toOther)));
return this;
}
@Override
public void unsubscribe() {
cancel.unsubscribe();
}
@Override
public boolean isUnsubscribed() {
return cancel.isUnsubscribed();
}
@Override
public void onNext(T args) {
if (running.get()) {
observer.onNext(args);
}
}
@Override
public void onError(Throwable e) {
synchronized (guard) {
observer.onError(e);
unsubscribe();
}
}
@Override
public void onCompleted() {
synchronized (guard) {
observer.onCompleted();
unsubscribe();
}
}
/** Observe the other stream. */
private class OtherObserver implements Observer {
final Subscription self;
public OtherObserver(Subscription self) {
this.self = self;
}
@Override
public void onNext(U args) {
running.set(true);
self.unsubscribe();
}
@Override
public void onError(Throwable e) {
ResultManager.this.onError(e);
}
@Override
public void onCompleted() {
self.unsubscribe();
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy