All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rx.internal.operators.OperatorDebounceWithSelector Maven / Gradle / Ivy

There is a newer version: 1.3.8
Show newest version
/**
 * 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.Observable;
import rx.Observable.Operator;
import rx.exceptions.Exceptions;
import rx.Subscriber;
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> selector;
    
    public OperatorDebounceWithSelector(Func1> selector) {
        this.selector = selector;
    }
    
    @Override
    public Subscriber call(final Subscriber child) {
        final SerializedSubscriber s = new SerializedSubscriber(child);
        final SerialSubscription ssub = new SerialSubscription();
        child.add(ssub);
        
        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();
                    }
                };
                ssub.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);
            }
        };
    }
    
}