| 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 Observer} that allows associating |
| 22 |
* a resource with it. |
| 23 |
* <p> |
| 24 |
* The {@link #onNext(Object)}, {@link #onError(Throwable)}, {@link #tryOnError(Throwable)} |
| 25 |
* and {@link #onComplete()} methods should be called in a sequential manner, just like the |
| 26 |
* {@link Observer}'s methods should be. |
| 27 |
* Use the {@code ObservableEmitter} the {@link #serialize()} method returns instead of the original |
| 28 |
* {@code ObservableEmitter} instance provided by the generator routine if you want to ensure this. |
| 29 |
* The other methods are thread-safe. |
| 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 #onError(Throwable)}, |
| 35 |
* {@link #onComplete()} 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 ObservableEmitter<@NonNull T> extends Emitter<T> { |
| 51 |
|
| 52 |
/** |
| 53 |
* Sets a {@link Disposable} on this emitter; any previous {@code Disposable} |
| 54 |
* or {@link Cancellable} will be disposed/cancelled. |
| 55 |
* <p>This method is thread-safe. |
| 56 |
* @param d the {@code Disposable}, {@code null} is allowed |
| 57 |
*/ |
| 58 |
void setDisposable(@Nullable Disposable d); |
| 59 |
|
| 60 |
/** |
| 61 |
* Sets a {@link Cancellable} on this emitter; any previous {@link Disposable} |
| 62 |
* or {@code Cancellable} will be disposed/cancelled. |
| 63 |
* <p>This method is thread-safe. |
| 64 |
* @param c the {@code Cancellable} resource, {@code null} is allowed |
| 65 |
*/ |
| 66 |
void setCancellable(@Nullable Cancellable c); |
| 67 |
|
| 68 |
/** |
| 69 |
* Returns true if the downstream disposed the sequence or the |
| 70 |
* emitter was terminated via {@link #onError(Throwable)}, {@link #onComplete} or a |
| 71 |
* successful {@link #tryOnError(Throwable)}. |
| 72 |
* <p>This method is thread-safe. |
| 73 |
* @return true if the downstream disposed the sequence or the emitter was terminated |
| 74 |
*/ |
| 75 |
boolean isDisposed(); |
| 76 |
|
| 77 |
/** |
| 78 |
* Ensures that calls to {@code onNext}, {@code onError} and {@code onComplete} are properly serialized. |
| 79 |
* @return the serialized {@link ObservableEmitter} |
| 80 |
*/ |
| 81 |
@NonNull |
| 82 |
ObservableEmitter<T> serialize(); |
| 83 |
|
| 84 |
/** |
| 85 |
* Attempts to emit the specified {@link Throwable} error if the downstream |
| 86 |
* hasn't cancelled the sequence or is otherwise terminated, returning false |
| 87 |
* if the emission is not allowed to happen due to lifecycle restrictions. |
| 88 |
* <p> |
| 89 |
* Unlike {@link #onError(Throwable)}, the {@link io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable) RxjavaPlugins.onError} |
| 90 |
* is not called if the error could not be delivered. |
| 91 |
* <p>History: 2.1.1 - experimental |
| 92 |
* @param t the {@code Throwable} error to signal if possible |
| 93 |
* @return true if successful, false if the downstream is not able to accept further |
| 94 |
* events |
| 95 |
* @since 2.2 |
| 96 |
*/ |
| 97 |
boolean tryOnError(@NonNull Throwable t); |
| 98 |
} |