reactor.core.publisher.FluxTakeUntilOther Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of redisson-all Show documentation
Show all versions of redisson-all Show documentation
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
/*
* 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.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.stream.Stream;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscription;
import reactor.core.CoreSubscriber;
import reactor.core.Scannable;
import reactor.util.annotation.Nullable;
import reactor.util.context.Context;
/**
* Relays values from the main Publisher until another Publisher signals an event.
*
* @param the value type of the main Publisher
* @param the value type of the other Publisher
* @see Reactive-Streams-Commons
*/
final class FluxTakeUntilOther extends InternalFluxOperator {
final Publisher other;
FluxTakeUntilOther(Flux extends T> source, Publisher other) {
super(source);
this.other = Operators.toFluxOrMono(Objects.requireNonNull(other, "other"));
}
@Override
public int getPrefetch() {
return Integer.MAX_VALUE;
}
@Override
public CoreSubscriber super T> subscribeOrReturn(CoreSubscriber super T> actual) {
TakeUntilMainSubscriber mainSubscriber = new TakeUntilMainSubscriber<>(actual);
TakeUntilOtherSubscriber otherSubscriber = new TakeUntilOtherSubscriber<>(mainSubscriber);
other.subscribe(otherSubscriber);
return mainSubscriber;
}
@Override
public Object scanUnsafe(Attr key) {
if (key == Attr.RUN_STYLE) return Attr.RunStyle.SYNC;
return super.scanUnsafe(key);
}
static final class TakeUntilOtherSubscriber implements InnerConsumer {
final TakeUntilMainSubscriber> main;
boolean once;
TakeUntilOtherSubscriber(TakeUntilMainSubscriber> main) {
this.main = main;
}
@Override
public Context currentContext() {
return main.currentContext();
}
@Override
@Nullable
public Object scanUnsafe(Attr key) {
if (key == Attr.CANCELLED) return main.other == Operators.cancelledSubscription();
if (key == Attr.PARENT) return main.other;
if (key == Attr.ACTUAL) return main;
if (key == Attr.RUN_STYLE) return Attr.RunStyle.SYNC;
return null;
}
@Override
public void onSubscribe(Subscription s) {
main.setOther(s);
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(U t) {
if (once) {
return;
}
once = true;
//in case more values are coming, cancel main.other (which is basically this one's upstream, see main.setOther)
main.cancelOther();
//now cancel main.main, and ensure that if this happens early an empty Subscription is passed down
main.cancelMainAndComplete();
}
@Override
public void onError(Throwable t) {
if (once) {
return;
}
once = true;
main.onError(t);
}
@Override
public void onComplete() {
if (once) {
return;
}
once = true;
//cancel main.main, and ensure that if this happens early an empty Subscription is passed down
main.cancelMainAndComplete();
}
}
static final class TakeUntilMainSubscriber implements InnerOperator {
final CoreSubscriber super T> actual;
volatile Subscription main;
@SuppressWarnings("rawtypes")
static final AtomicReferenceFieldUpdater MAIN =
AtomicReferenceFieldUpdater.newUpdater(TakeUntilMainSubscriber.class, Subscription.class, "main");
volatile Subscription other;
@SuppressWarnings("rawtypes")
static final AtomicReferenceFieldUpdater OTHER =
AtomicReferenceFieldUpdater.newUpdater(TakeUntilMainSubscriber.class, Subscription.class, "other");
TakeUntilMainSubscriber(CoreSubscriber super T> actual) {
this.actual = Operators.serialize(actual);
}
@Override
@Nullable
public Object scanUnsafe(Attr key) {
if (key == Attr.PARENT) return main;
if (key == Attr.CANCELLED) return main == Operators.cancelledSubscription();
if (key == Attr.RUN_STYLE) return Attr.RunStyle.SYNC;
return InnerOperator.super.scanUnsafe(key);
}
@Override
public final CoreSubscriber super T> actual() {
return actual;
}
@Override
public Stream extends Scannable> inners() {
return Stream.of(Scannable.from(other));
}
void setOther(Subscription s) {
if (!OTHER.compareAndSet(this, null, s)) {
s.cancel();
if (other != Operators.cancelledSubscription()) {
Operators.reportSubscriptionSet();
}
}
}
@Override
public void request(long n) {
main.request(n);
}
void cancelMainAndComplete() {
Subscription s = main;
if (s != Operators.cancelledSubscription()) {
s = MAIN.getAndSet(this, Operators.cancelledSubscription());
if (s != null && s != Operators.cancelledSubscription()) {
s.cancel();
}
if (s == null) {
// this indicates the Other completed early, even before `main` was set.
// let's pass an empty Subscription down and complete immediately
Operators.complete(actual);
}
else {
// if s wasn't null then Main Subscription was set and actual.onSubscribe already called
actual.onComplete();
}
}
}
void cancelOther() {
Subscription s = other;
if (s != Operators.cancelledSubscription()) {
s = OTHER.getAndSet(this, Operators.cancelledSubscription());
if (s != null && s != Operators.cancelledSubscription()) {
s.cancel();
}
}
}
@Override
public void cancel() {
//similar to cancelMainAndComplete() but at this stage we only care about cancellation upstream
Subscription s = main;
if (s != Operators.cancelledSubscription()) {
s = MAIN.getAndSet(this, Operators.cancelledSubscription());
if (s != null && s != Operators.cancelledSubscription()) {
s.cancel();
}
}
//we do cancel the other subscriber too
cancelOther();
}
@Override
public void onSubscribe(Subscription s) {
if (!MAIN.compareAndSet(this, null, s)) {
s.cancel();
if (main != Operators.cancelledSubscription()) {
Operators.reportSubscriptionSet();
}
} else {
actual.onSubscribe(this);
}
}
@Override
public void onNext(T t) {
actual.onNext(t);
}
@Override
public void onError(Throwable t) {
if (main == null) {
if (MAIN.compareAndSet(this, null, Operators.cancelledSubscription())) {
Operators.error(actual, t);
return;
}
}
cancelOther();
actual.onError(t);
}
@Override
public void onComplete() {
if (main == null) {
if (MAIN.compareAndSet(this, null, Operators.cancelledSubscription())) {
Operators.complete(actual);
return;
}
}
cancelOther();
actual.onComplete();
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy