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

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

Go to download

Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC

There is a newer version: 3.40.2
Show newest version
/*
 * Copyright (c) 2016-2023 VMware Inc. or its affiliates, 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
 *
 *   https://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 InternalFluxOperator
		implements Consumer> {

	final Publisher other;

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

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

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

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

	@Override
	public Object scanUnsafe(Attr key) {
		if (key == Attr.RUN_STYLE) return Attr.RunStyle.SYNC;
		return super.scanUnsafe(key);
	}

	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;
			if (key == Attr.RUN_STYLE) return Attr.RunStyle.SYNC;

			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;
			if (key == Attr.RUN_STYLE) return Attr.RunStyle.SYNC;

			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();
		}
	}
}