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

hu.akarnokd.rxjava2.internal.operators.nbp.NbpOnSubscribeRedo Maven / Gradle / Ivy

There is a newer version: 2.0.0-RC3
Show newest version
/**
 * Copyright 2015 David Karnok and 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 hu.akarnokd.rxjava2.internal.operators.nbp;

import java.util.concurrent.atomic.*;

import hu.akarnokd.rxjava2.*;
import hu.akarnokd.rxjava2.NbpObservable.*;
import hu.akarnokd.rxjava2.disposables.*;
import hu.akarnokd.rxjava2.functions.*;
import hu.akarnokd.rxjava2.internal.subscribers.nbp.NbpToNotificationSubscriber;
import hu.akarnokd.rxjava2.subjects.nbp.NbpBehaviorSubject;

public final class NbpOnSubscribeRedo implements NbpOnSubscribe {
    final NbpObservable source;
    final Function>>, ? extends NbpObservable> manager;

    public NbpOnSubscribeRedo(NbpObservable source,
            Function>>, ? extends NbpObservable> manager) {
        this.source = source;
        this.manager = manager;
    }
    
    @Override
    public void accept(NbpSubscriber s) {
        
        // FIXE use BehaviorSubject? (once available)
        NbpBehaviorSubject>> subject = NbpBehaviorSubject.create();
        
        final RedoSubscriber parent = new RedoSubscriber(s, subject, source);

        s.onSubscribe(parent.arbiter);

        NbpObservable action = manager.apply(subject);
        
        action.subscribe(new NbpToNotificationSubscriber(new Consumer>>() {
            @Override
            public void accept(Try> o) {
                parent.handle(o);
            }
        }));
        
        // trigger first subscription
        parent.handle(Notification.next(0));
    }
    
    static final class RedoSubscriber extends AtomicBoolean implements NbpSubscriber {
        /** */
        private static final long serialVersionUID = -1151903143112844287L;
        final NbpSubscriber actual;
        final NbpBehaviorSubject>> subject;
        final NbpObservable source;
        final MultipleAssignmentDisposable arbiter;
        
        volatile int wip;
        @SuppressWarnings("rawtypes")
        static final AtomicIntegerFieldUpdater WIP =
                AtomicIntegerFieldUpdater.newUpdater(RedoSubscriber.class, "wip");
        
        public RedoSubscriber(NbpSubscriber actual, NbpBehaviorSubject>> subject, NbpObservable source) {
            this.actual = actual;
            this.subject = subject;
            this.source = source;
            this.arbiter = new MultipleAssignmentDisposable();
            this.lazySet(true);
        }
        
        @Override
        public void onSubscribe(Disposable s) {
            arbiter.set(s);
        }
        
        @Override
        public void onNext(T t) {
            actual.onNext(t);
        }
        
        @Override
        public void onError(Throwable t) {
            if (compareAndSet(false, true)) {
                subject.onNext(Try.>ofError(t));
            }
        }
        
        @Override
        public void onComplete() {
            if (compareAndSet(false, true)) {
                subject.onNext(Notification.complete());
            }
        }
        
        void handle(Try> notification) {
            if (compareAndSet(true, false)) {
                if (notification.hasError()) {
                    arbiter.dispose();
                    actual.onError(notification.error());
                } else {
                    Optional o = notification.value();
                    
                    if (o.isPresent()) {
                        
                        if (WIP.getAndIncrement(this) == 0) {
                            int missed = 1;
                            for (;;) {
                                if (arbiter.isDisposed()) {
                                    return;
                                }
                                source.subscribe(this);
                            
                                missed = WIP.addAndGet(this, -missed);
                                if (missed == 0) {
                                    break;
                                }
                            }
                        }
                        
                    } else {
                        arbiter.dispose();
                        actual.onComplete();
                    }
                }
            }
        }
    }
}