Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .singleOptional() to Mono and Flux #3317

Merged
merged 6 commits into from
Jan 18, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
import reactor.util.context.Context;

/**
* Expects and emits a single item from the source wrapped into an Optional, emits
* an empty Optional instead for empty source or signals
* IndexOutOfBoundsException for a multi-item source.
* Emits a single item from the source wrapped into an Optional, emits
chemicL marked this conversation as resolved.
Show resolved Hide resolved
* an empty Optional instead for empty source.
*
* @param <T> the value type
* @see <a href="https://github.com/reactor/reactive-streams-commons">Reactive-Streams-Commons</a>
Expand All @@ -53,8 +52,6 @@ static final class SingleOptionalSubscriber<T> extends Operators.MonoInnerProduc

Subscription s;

int count;

boolean done;

@Override
Expand Down Expand Up @@ -96,24 +93,12 @@ public void onSubscribe(Subscription s) {

@Override
public void onNext(T t) {
if (isCancelled()) {
//this helps differentiating a duplicate malformed signal "done" from a count > 1 "done"
Operators.onDiscard(t, actual().currentContext());
return;
}
if (done) {
Operators.onNextDropped(t, actual().currentContext());
return;
}
if (++count > 1) {
Operators.onDiscard(t, actual().currentContext());
//mark as both cancelled and done
cancel();
onError(new IndexOutOfBoundsException("Source emitted more than one item"));
}
else {
setValue(Optional.of(t));
}
done = true;
complete(Optional.of(t));
chemicL marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
Expand All @@ -134,14 +119,7 @@ public void onComplete() {
return;
}
done = true;

int c = count;
if (c == 0) {
complete(Optional.empty());
}
else if (c == 1) {
complete();
}
complete(Optional.empty());
}

}
Expand Down