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

hu.akarnokd.rxjava2.internal.operators.PublisherRedo 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;

import java.util.concurrent.atomic.*;

import org.reactivestreams.*;

import hu.akarnokd.rxjava2.*;
import hu.akarnokd.rxjava2.functions.*;
import hu.akarnokd.rxjava2.internal.subscribers.ToNotificationSubscriber;
import hu.akarnokd.rxjava2.internal.subscriptions.SubscriptionArbiter;
import hu.akarnokd.rxjava2.subjects.BehaviorSubject;

public final class PublisherRedo implements Publisher {
    final Publisher source;
    final Function>>, ? extends Publisher> manager;

    public PublisherRedo(Publisher source,
            Function>>, ? extends Publisher> manager) {
        this.source = source;
        this.manager = manager;
    }
    
    @Override
    public void subscribe(Subscriber s) {
        
        // FIXE use BehaviorSubject? (once available)
        BehaviorSubject>> subject = BehaviorSubject.create();
        
        final RedoSubscriber parent = new RedoSubscriber(s, subject, source);

        s.onSubscribe(parent.arbiter);

        Publisher action = manager.apply(subject);
        
        action.subscribe(new ToNotificationSubscriber(new Consumer>>() {
            @Override
            public void accept(Try> v) {
                parent.handle(v);
            }
        }));
        
        // trigger first subscription
        parent.handle(Notification.next((Object)0));
    }
    
    static final class RedoSubscriber extends AtomicBoolean implements Subscriber {
        /** */
        private static final long serialVersionUID = -1151903143112844287L;
        final Subscriber actual;
        final BehaviorSubject>> subject;
        final Publisher source;
        final SubscriptionArbiter arbiter;
        
        volatile int wip;
        @SuppressWarnings("rawtypes")
        static final AtomicIntegerFieldUpdater WIP =
                AtomicIntegerFieldUpdater.newUpdater(RedoSubscriber.class, "wip");
        
        public RedoSubscriber(Subscriber actual, BehaviorSubject>> subject, Publisher source) {
            this.actual = actual;
            this.subject = subject;
            this.source = source;
            this.arbiter = new SubscriptionArbiter();
            this.lazySet(true);
        }
        
        @Override
        public void onSubscribe(Subscription s) {
            arbiter.setSubscription(s);
        }
        
        @Override
        public void onNext(T t) {
            actual.onNext(t);
            arbiter.produced(1L);
        }
        
        @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.cancel();
                    actual.onError(notification.error());
                } else {
                    Optional o = notification.value();
                    
                    if (o.isPresent()) {
                        
                        if (WIP.getAndIncrement(this) == 0) {
                            int missed = 1;
                            for (;;) {
                                if (arbiter.isCancelled()) {
                                    return;
                                }
                                source.subscribe(this);
                            
                                missed = WIP.addAndGet(this, -missed);
                                if (missed == 0) {
                                    break;
                                }
                            }
                        }
                        
                    } else {
                        arbiter.cancel();
                        actual.onComplete();
                    }
                }
            }
        }
    }
}