Interface SingleEmitter<T>
-
- Type Parameters:
T- the value type to emit
- All Known Implementing Classes:
SingleCreate.Emitter
public interface SingleEmitter<@NonNull T>Abstraction over an RxJavaSingleObserverthat allows associating a resource with it.All methods are safe to call from multiple threads, but note that there is no guarantee whose terminal event will win and get delivered to the downstream.
Calling
onSuccess(Object)multiple times has no effect. CallingonError(Throwable)multiple times or afteronSuccesswill route the exception into the global error handler viaRxJavaPlugins.onError(Throwable).The emitter allows the registration of a single resource, in the form of a
DisposableorCancellableviasetDisposable(Disposable)orsetCancellable(Cancellable)respectively. The emitter implementations will dispose/cancel this instance when the downstream cancels the flow or after the event generator logic callsonSuccess(Object),onError(Throwable), or whentryOnError(Throwable)succeeds.Only one
DisposableorCancellableobject can be associated with the emitter at a time. Calling eithersetmethod will dispose/cancel any previous object. If there is a need for handling multiple resources, one can create aCompositeDisposableand associate that with the emitter instead.The
Cancellableis logically equivalent toDisposablebut allows using cleanup logic that can throw a checked exception (such as manyclose()methods on Java IO components). Since the release of resources happens after the terminal events have been delivered or the sequence gets cancelled, exceptions throw withinCancellableare routed to the global error handler viaRxJavaPlugins.onError(Throwable).
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description booleanisDisposed()Returns true if the downstream disposed the sequence or the emitter was terminated viaonSuccess(Object),onError(Throwable), or a successfultryOnError(Throwable).voidonError(@NonNull java.lang.Throwable t)Signal an exception.voidonSuccess(@NonNull T t)Signal a success value.voidsetCancellable(@Nullable Cancellable c)Sets a Cancellable on this emitter; any previousDisposableorCancellablewill be disposed/cancelled.voidsetDisposable(@Nullable Disposable d)Sets aDisposableon this emitter; any previousDisposableorCancellablewill be disposed/cancelled.booleantryOnError(@NonNull java.lang.Throwable t)Attempts to emit the specifiedThrowableerror if the downstream hasn't cancelled the sequence or is otherwise terminated, returning false if the emission is not allowed to happen due to lifecycle restrictions.
-
-
-
Method Detail
-
onSuccess
void onSuccess(@NonNull @NonNull T t)
Signal a success value.- Parameters:
t- the value, not null
-
onError
void onError(@NonNull @NonNull java.lang.Throwable t)
Signal an exception.- Parameters:
t- the exception, notnull
-
setDisposable
void setDisposable(@Nullable @Nullable Disposable d)
Sets aDisposableon this emitter; any previousDisposableorCancellablewill be disposed/cancelled.This method is thread-safe.
- Parameters:
d- theDisposable,nullis allowed
-
setCancellable
void setCancellable(@Nullable @Nullable Cancellable c)
Sets a Cancellable on this emitter; any previousDisposableorCancellablewill be disposed/cancelled.This method is thread-safe.
- Parameters:
c- theCancellableresource,nullis allowed
-
isDisposed
boolean isDisposed()
Returns true if the downstream disposed the sequence or the emitter was terminated viaonSuccess(Object),onError(Throwable), or a successfultryOnError(Throwable).This method is thread-safe.
- Returns:
- true if the downstream disposed the sequence or the emitter was terminated
-
tryOnError
boolean tryOnError(@NonNull @NonNull java.lang.Throwable t)
Attempts to emit the specifiedThrowableerror if the downstream hasn't cancelled the sequence or is otherwise terminated, returning false if the emission is not allowed to happen due to lifecycle restrictions.Unlike
onError(Throwable), theRxjavaPlugins.onErroris not called if the error could not be delivered.History: 2.1.1 - experimental
- Parameters:
t- the throwable error to signal if possible- Returns:
- true if successful, false if the downstream is not able to accept further events
- Since:
- 2.2
-
-