|
| 1 | +/* |
| 2 | + Copyright (c) 2020, 2022, Oracle and/or its affiliates. |
| 3 | +
|
| 4 | + This software is dual-licensed to you under the Universal Permissive License |
| 5 | + (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License |
| 6 | + 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose |
| 7 | + either license. |
| 8 | +
|
| 9 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + you may not use this file except in compliance with the License. |
| 11 | + You may obtain a copy of the License at |
| 12 | +
|
| 13 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +
|
| 15 | + Unless required by applicable law or agreed to in writing, software |
| 16 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + See the License for the specific language governing permissions and |
| 19 | + limitations under the License. |
| 20 | +*/ |
| 21 | + |
| 22 | +package oracle.r2dbc.impl; |
| 23 | + |
| 24 | +import org.reactivestreams.Publisher; |
| 25 | +import reactor.core.publisher.Flux; |
| 26 | +import reactor.core.publisher.Mono; |
| 27 | + |
| 28 | +/** |
| 29 | + * Factory methods that create a {@code Publisher}. These methods cover special |
| 30 | + * cases which are not already supported by Project Reactor. |
| 31 | + */ |
| 32 | +class Publishers { |
| 33 | + |
| 34 | + private Publishers() {} |
| 35 | + |
| 36 | + /** |
| 37 | + * A publisher that immediately emits onNext and onComplete to subscribers |
| 38 | + */ |
| 39 | + private static final Publisher<Object> COMPLETED_PUBLISHER = |
| 40 | + Mono.just(new Object()); |
| 41 | + |
| 42 | + /** |
| 43 | + * <p> |
| 44 | + * Returns a publisher that emits the concatenated signals of a |
| 45 | + * {@code publisher} and {@code onTerminationPublisher}. If the |
| 46 | + * {@code onTerminationPublisher} emits an error, it will suppress any error |
| 47 | + * emitted by the first {@code publisher}. If a subscription to the returned |
| 48 | + * publisher is cancelled, the {@code onTerminationPublisher} is subscribed to |
| 49 | + * but it can not emit any error through the cancelled subscription. |
| 50 | + * </p><p> |
| 51 | + * The returned publisher behaves similarly to: <pre>{@code |
| 52 | + * Flux.concatDelayError( |
| 53 | + * publisher, |
| 54 | + * onTerminationPublisher) |
| 55 | + * .doOnCancel(onTerminationPublisher::subscribe) |
| 56 | + * }</pre> |
| 57 | + * However, the code above can result in: |
| 58 | + * <pre> |
| 59 | + * reactor.core.Exceptions$StaticThrowable: Operator has been terminated |
| 60 | + * </pre> |
| 61 | + * This seems to happen when the concatDelayError publisher receives a cancel |
| 62 | + * from a downstream subscriber after it has already received onComplete from |
| 63 | + * a upstream publisher. |
| 64 | + * </p> |
| 65 | + * @param publisher First publisher which is subscribed to. |
| 66 | + * @param onTerminationPublisher Publisher which is subscribed to when the |
| 67 | + * first publisher terminates, or a subcription is cancelled. |
| 68 | + * @return The concatenated publisher. |
| 69 | + * @param <T> Type of objects emitted to onNext |
| 70 | + */ |
| 71 | + static <T> Publisher<T> concatTerminal( |
| 72 | + Publisher<T> publisher, Publisher<Void> onTerminationPublisher) { |
| 73 | + return Flux.usingWhen( |
| 74 | + COMPLETED_PUBLISHER, |
| 75 | + ignored -> publisher, |
| 76 | + ignored -> onTerminationPublisher); |
| 77 | + } |
| 78 | +} |
0 commit comments