| 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.*; |
| 17 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 18 |
import io.reactivex.rxjava3.functions.*; |
| 19 |
|
| 20 |
/** |
| 21 |
* Abstraction over an RxJava {@link SingleObserver} that allows associating |
| 22 |
* a resource with it. |
| 23 |
* <p> |
| 24 |
* All methods are safe to call from multiple threads, but note that there is no guarantee |
| 25 |
* whose terminal event will win and get delivered to the downstream. |
| 26 |
* <p> |
| 27 |
* Calling {@link #onSuccess(Object)} multiple times has no effect. |
| 28 |
* Calling {@link #onError(Throwable)} multiple times or after {@code onSuccess} will route the |
| 29 |
* exception into the global error handler via {@link io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable)}. |
| 30 |
* <p> |
| 31 |
* The emitter allows the registration of a single resource, in the form of a {@link Disposable} |
| 32 |
* or {@link Cancellable} via {@link #setDisposable(Disposable)} or {@link #setCancellable(Cancellable)} |
| 33 |
* respectively. The emitter implementations will dispose/cancel this instance when the |
| 34 |
* downstream cancels the flow or after the event generator logic calls {@link #onSuccess(Object)}, |
| 35 |
* {@link #onError(Throwable)}, or when {@link #tryOnError(Throwable)} succeeds. |
| 36 |
* <p> |
| 37 |
* Only one {@code Disposable} or {@code Cancellable} object can be associated with the emitter at |
| 38 |
* a time. Calling either {@code set} method will dispose/cancel any previous object. If there |
| 39 |
* is a need for handling multiple resources, one can create a {@link io.reactivex.rxjava3.disposables.CompositeDisposable} |
| 40 |
* and associate that with the emitter instead. |
| 41 |
* <p> |
| 42 |
* The {@link Cancellable} is logically equivalent to {@code Disposable} but allows using cleanup logic that can |
| 43 |
* throw a checked exception (such as many {@code close()} methods on Java IO components). Since |
| 44 |
* the release of resources happens after the terminal events have been delivered or the sequence gets |
| 45 |
* cancelled, exceptions throw within {@code Cancellable} are routed to the global error handler via |
| 46 |
* {@link io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable)}. |
| 47 |
* |
| 48 |
* @param <T> the value type to emit |
| 49 |
*/ |
| 50 |
public interface SingleEmitter<@NonNull T> { |
| 51 |
|
| 52 |
/** |
| 53 |
* Signal a success value. |
| 54 |
* @param t the value, not null |
| 55 |
*/ |
| 56 |
void onSuccess(@NonNull T t); |
| 57 |
|
| 58 |
/** |
| 59 |
* Signal an exception. |
| 60 |
* @param t the exception, not {@code null} |
| 61 |
*/ |
| 62 |
void onError(@NonNull Throwable t); |
| 63 |
|
| 64 |
/** |
| 65 |
* Sets a {@link Disposable} on this emitter; any previous {@code Disposable} |
| 66 |
* or {@link Cancellable} will be disposed/cancelled. |
| 67 |
* <p>This method is thread-safe. |
| 68 |
* @param d the {@code Disposable}, {@code null} is allowed |
| 69 |
*/ |
| 70 |
void setDisposable(@Nullable Disposable d); |
| 71 |
|
| 72 |
/** |
| 73 |
* Sets a Cancellable on this emitter; any previous {@link Disposable} |
| 74 |
* or {@link Cancellable} will be disposed/cancelled. |
| 75 |
* <p>This method is thread-safe. |
| 76 |
* @param c the {@code Cancellable} resource, {@code null} is allowed |
| 77 |
*/ |
| 78 |
void setCancellable(@Nullable Cancellable c); |
| 79 |
|
| 80 |
/** |
| 81 |
* Returns true if the downstream disposed the sequence or the |
| 82 |
* emitter was terminated via {@link #onSuccess(Object)}, {@link #onError(Throwable)}, |
| 83 |
* or a successful {@link #tryOnError(Throwable)}. |
| 84 |
* <p>This method is thread-safe. |
| 85 |
* @return true if the downstream disposed the sequence or the emitter was terminated |
| 86 |
*/ |
| 87 |
boolean isDisposed(); |
| 88 |
|
| 89 |
/** |
| 90 |
* Attempts to emit the specified {@link Throwable} error if the downstream |
| 91 |
* hasn't cancelled the sequence or is otherwise terminated, returning false |
| 92 |
* if the emission is not allowed to happen due to lifecycle restrictions. |
| 93 |
* <p> |
| 94 |
* Unlike {@link #onError(Throwable)}, the {@link io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable) RxjavaPlugins.onError} |
| 95 |
* is not called if the error could not be delivered. |
| 96 |
* <p>History: 2.1.1 - experimental |
| 97 |
* @param t the throwable error to signal if possible |
| 98 |
* @return true if successful, false if the downstream is not able to accept further |
| 99 |
* events |
| 100 |
* @since 2.2 |
| 101 |
*/ |
| 102 |
boolean tryOnError(@NonNull Throwable t); |
| 103 |
} |