rx.internal.operators.OperatorDebounceWithSelector Maven / Gradle / Ivy
/**
* 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.internal.operators;
import rx.*;
import rx.Observable.Operator;
import rx.exceptions.Exceptions;
import rx.functions.Func1;
import rx.internal.operators.OperatorDebounceWithTime.DebounceState;
import rx.observers.SerializedSubscriber;
import rx.subscriptions.SerialSubscription;
/**
* Delay the emission via another observable if no new source appears in the meantime.
*
* @param the value type of the main sequence
* @param the value type of the boundary sequence
*/
public final class OperatorDebounceWithSelector implements Operator {
final Func1 super T, ? extends Observable> selector;
public OperatorDebounceWithSelector(Func1 super T, ? extends Observable> selector) {
this.selector = selector;
}
@Override
public Subscriber super T> call(final Subscriber super T> child) {
final SerializedSubscriber s = new SerializedSubscriber(child);
final SerialSubscription serial = new SerialSubscription();
child.add(serial);
return new Subscriber(child) {
final DebounceState state = new DebounceState();
final Subscriber> self = this;
@Override
public void onStart() {
// debounce wants to receive everything as a firehose without backpressure
request(Long.MAX_VALUE);
}
@Override
public void onNext(T t) {
Observable debouncer;
try {
debouncer = selector.call(t);
} catch (Throwable e) {
Exceptions.throwOrReport(e, this);
return;
}
final int index = state.next(t);
Subscriber debounceSubscriber = new Subscriber() {
@Override
public void onNext(U t) {
onCompleted();
}
@Override
public void onError(Throwable e) {
self.onError(e);
}
@Override
public void onCompleted() {
state.emit(index, s, self);
unsubscribe();
}
};
serial.set(debounceSubscriber);
debouncer.unsafeSubscribe(debounceSubscriber);
}
@Override
public void onError(Throwable e) {
s.onError(e);
unsubscribe();
state.clear();
}
@Override
public void onCompleted() {
state.emitAndComplete(s, this);
}
};
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy