| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.core; |
| 15 |
|
| 16 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 17 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 18 |
|
| 19 |
/** |
| 20 |
* Provides a mechanism for receiving push-based notification of a single value or an error. |
| 21 |
* <p> |
| 22 |
* When a {@code SingleObserver} is subscribed to a {@link SingleSource} through the {@link SingleSource#subscribe(SingleObserver)} method, |
| 23 |
* the {@code SingleSource} calls {@link #onSubscribe(Disposable)} with a {@link Disposable} that allows |
| 24 |
* disposing the sequence at any time. A well-behaved |
| 25 |
* {@code SingleSource} will call a {@code SingleObserver}'s {@link #onSuccess(Object)} method exactly once or the {@code SingleObserver}'s |
| 26 |
* {@link #onError} method exactly once as they are considered mutually exclusive <strong>terminal signals</strong>. |
| 27 |
* <p> |
| 28 |
* Calling the {@code SingleObserver}'s method must happen in a serialized fashion, that is, they must not |
| 29 |
* be invoked concurrently by multiple threads in an overlapping fashion and the invocation pattern must |
| 30 |
* adhere to the following protocol: |
| 31 |
* <pre><code> onSubscribe (onSuccess | onError)?</code></pre> |
| 32 |
* <p> |
| 33 |
* Subscribing a {@code SingleObserver} to multiple {@code SingleSource}s is not recommended. If such reuse |
| 34 |
* happens, it is the duty of the {@code SingleObserver} implementation to be ready to receive multiple calls to |
| 35 |
* its methods and ensure proper concurrent behavior of its business logic. |
| 36 |
* <p> |
| 37 |
* Calling {@link #onSubscribe(Disposable)}, {@link #onSuccess(Object)} or {@link #onError(Throwable)} with a |
| 38 |
* {@code null} argument is forbidden. |
| 39 |
* <p> |
| 40 |
* The implementations of the {@code onXXX} methods should avoid throwing runtime exceptions other than the following cases: |
| 41 |
* <ul> |
| 42 |
* <li>If the argument is {@code null}, the methods can throw a {@code NullPointerException}. |
| 43 |
* Note though that RxJava prevents {@code null}s to enter into the flow and thus there is generally no |
| 44 |
* need to check for nulls in flows assembled from standard sources and intermediate operators. |
| 45 |
* </li> |
| 46 |
* <li>If there is a fatal error (such as {@code VirtualMachineError}).</li> |
| 47 |
* </ul> |
| 48 |
* @see <a href="http://reactivex.io/documentation/observable.html">ReactiveX documentation: Observable</a> |
| 49 |
* @param <T> |
| 50 |
* the type of item the SingleObserver expects to observe |
| 51 |
* @since 2.0 |
| 52 |
*/ |
| 53 |
public interface SingleObserver<@NonNull T> { |
| 54 |
|
| 55 |
/** |
| 56 |
* Provides the {@link SingleObserver} with the means of cancelling (disposing) the |
| 57 |
* connection (channel) with the Single in both |
| 58 |
* synchronous (from within {@code onSubscribe(Disposable)} itself) and asynchronous manner. |
| 59 |
* @param d the Disposable instance whose {@link Disposable#dispose()} can |
| 60 |
* be called anytime to cancel the connection |
| 61 |
* @since 2.0 |
| 62 |
*/ |
| 63 |
void onSubscribe(@NonNull Disposable d); |
| 64 |
|
| 65 |
/** |
| 66 |
* Notifies the {@link SingleObserver} with a single item and that the {@link Single} has finished sending |
| 67 |
* push-based notifications. |
| 68 |
* <p> |
| 69 |
* The {@code Single} will not call this method if it calls {@link #onError}. |
| 70 |
* |
| 71 |
* @param t |
| 72 |
* the item emitted by the {@code Single} |
| 73 |
*/ |
| 74 |
void onSuccess(@NonNull T t); |
| 75 |
|
| 76 |
/** |
| 77 |
* Notifies the {@link SingleObserver} that the {@link Single} has experienced an error condition. |
| 78 |
* <p> |
| 79 |
* If the {@code Single} calls this method, it will not thereafter call {@link #onSuccess}. |
| 80 |
* |
| 81 |
* @param e |
| 82 |
* the exception encountered by the {@code Single} |
| 83 |
*/ |
| 84 |
void onError(@NonNull Throwable e); |
| 85 |
} |