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

reactor.core.publisher.FluxDelaySubscription Maven / Gradle / Ivy

/*
 * Copyright (c) 2011-2017 Pivotal Software Inc, All Rights Reserved.
 *
 * 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 reactor.core.publisher;

import java.util.Objects;
import java.util.function.Consumer;

import org.reactivestreams.Publisher;
import org.reactivestreams.Subscription;
import reactor.core.CoreSubscriber;
import reactor.util.annotation.Nullable;
import reactor.util.context.Context;

/**
 * Delays the subscription to the main source until another Publisher
 * signals a value or completes.
 *
 * @param  the main source value type
 * @param  the other source type
 * @see Reactive-Streams-Commons
 */
final class FluxDelaySubscription extends FluxOperator
		implements Consumer> {

	final Publisher other;

	FluxDelaySubscription(Flux source, Publisher other) {
		super(source);
		this.other = Objects.requireNonNull(other, "other");
	}

	@Override
	public int getPrefetch() {
		return Integer.MAX_VALUE;
	}

	@Override
	public void subscribe(CoreSubscriber actual) {
		other.subscribe(new DelaySubscriptionOtherSubscriber<>(actual, this));
	}

	@Override
	public void accept(DelaySubscriptionOtherSubscriber s) {
		source.subscribe(new DelaySubscriptionMainSubscriber<>(s.actual, s));
	}

	static final class DelaySubscriptionOtherSubscriber
			extends Operators.DeferredSubscription implements InnerOperator {

		final Consumer> source;

		final CoreSubscriber actual;

		Subscription s;

		boolean done;

		DelaySubscriptionOtherSubscriber(CoreSubscriber actual,
				Consumer> source) {
			this.actual = actual;
			this.source = source;
		}

		@Override
		public Context currentContext() {
			return actual.currentContext();
		}

		@Override
		@Nullable
		public Object scanUnsafe(Attr key) {
			if (key == Attr.PARENT) return s;
			if (key == Attr.ACTUAL) return actual;
			if (key == Attr.TERMINATED) return done;

			return super.scanUnsafe(key);
		}

		@Override
		public CoreSubscriber actual() {
			return actual;
		}

		@Override
		public void cancel() {
			s.cancel();
			super.cancel();
		}

		@Override
		public void onSubscribe(Subscription s) {
			if (Operators.validate(this.s, s)) {
				this.s = s;

				actual.onSubscribe(this);

				s.request(Long.MAX_VALUE);
			}
		}

		@Override
		public void onNext(U t) {
			if (done) {
				return;
			}
			done = true;
			s.cancel();

			source.accept(this);
		}

		@Override
		public void onError(Throwable t) {
			if (done) {
				Operators.onErrorDropped(t, actual.currentContext());
				return;
			}
			done = true;
			actual.onError(t);
		}

		@Override
		public void onComplete() {
			if (done) {
				return;
			}
			done = true;

			source.accept(this);
		}
	}

	static final class DelaySubscriptionMainSubscriber
			implements InnerConsumer {

		final CoreSubscriber actual;

		final DelaySubscriptionOtherSubscriber arbiter;

		DelaySubscriptionMainSubscriber(CoreSubscriber actual,
				DelaySubscriptionOtherSubscriber arbiter) {
			this.actual = actual;
			this.arbiter = arbiter;
		}

		@Override
		public Context currentContext() {
			return arbiter.currentContext();
		}

		@Override
		@Nullable
		public Object scanUnsafe(Attr key) {
			if (key == Attr.ACTUAL) return actual;

			return null;
		}

		@Override
		public void onSubscribe(Subscription s) {
			arbiter.set(s);
		}

		@Override
		public void onNext(T t) {
			actual.onNext(t);
		}

		@Override
		public void onError(Throwable t) {
			actual.onError(t);
		}

		@Override
		public void onComplete() {
			actual.onComplete();
		}
	}
}