Interface ObservableEmitter<T>
-
- Type Parameters:
T
- the value type to emit
- All Superinterfaces:
Emitter<T>
- All Known Implementing Classes:
ObservableCreate.CreateEmitter
,ObservableCreate.SerializedEmitter
public interface ObservableEmitter<@NonNull T> extends Emitter<T>
Abstraction over an RxJavaObserver
that allows associating a resource with it.The
Emitter.onNext(Object)
,Emitter.onError(Throwable)
,tryOnError(Throwable)
andEmitter.onComplete()
methods should be called in a sequential manner, just like theObserver
's methods should be. Use theObservableEmitter
theserialize()
method returns instead of the originalObservableEmitter
instance provided by the generator routine if you want to ensure this. The other methods are thread-safe.The emitter allows the registration of a single resource, in the form of a
Disposable
orCancellable
viasetDisposable(Disposable)
orsetCancellable(Cancellable)
respectively. The emitter implementations will dispose/cancel this instance when the downstream cancels the flow or after the event generator logic callsEmitter.onError(Throwable)
,Emitter.onComplete()
or whentryOnError(Throwable)
succeeds.Only one
Disposable
orCancellable
object can be associated with the emitter at a time. Calling eitherset
method will dispose/cancel any previous object. If there is a need for handling multiple resources, one can create aCompositeDisposable
and associate that with the emitter instead.The
Cancellable
is logically equivalent toDisposable
but 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 withinCancellable
are routed to the global error handler viaRxJavaPlugins.onError(Throwable)
.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description boolean
isDisposed()
Returns true if the downstream disposed the sequence or the emitter was terminated viaEmitter.onError(Throwable)
,Emitter.onComplete()
or a successfultryOnError(Throwable)
.@NonNull ObservableEmitter<T>
serialize()
Ensures that calls toonNext
,onError
andonComplete
are properly serialized.void
setCancellable(@Nullable Cancellable c)
Sets aCancellable
on this emitter; any previousDisposable
orCancellable
will be disposed/cancelled.void
setDisposable(@Nullable Disposable d)
Sets aDisposable
on this emitter; any previousDisposable
orCancellable
will be disposed/cancelled.boolean
tryOnError(@NonNull java.lang.Throwable t)
Attempts to emit the specifiedThrowable
error 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.-
Methods inherited from interface io.reactivex.rxjava3.core.Emitter
onComplete, onError, onNext
-
-
-
-
Method Detail
-
setDisposable
void setDisposable(@Nullable @Nullable Disposable d)
Sets aDisposable
on this emitter; any previousDisposable
orCancellable
will be disposed/cancelled.This method is thread-safe.
- Parameters:
d
- theDisposable
,null
is allowed
-
setCancellable
void setCancellable(@Nullable @Nullable Cancellable c)
Sets aCancellable
on this emitter; any previousDisposable
orCancellable
will be disposed/cancelled.This method is thread-safe.
- Parameters:
c
- theCancellable
resource,null
is allowed
-
isDisposed
boolean isDisposed()
Returns true if the downstream disposed the sequence or the emitter was terminated viaEmitter.onError(Throwable)
,Emitter.onComplete()
or a successfultryOnError(Throwable)
.This method is thread-safe.
- Returns:
- true if the downstream disposed the sequence or the emitter was terminated
-
serialize
@NonNull @NonNull ObservableEmitter<T> serialize()
Ensures that calls toonNext
,onError
andonComplete
are properly serialized.- Returns:
- the serialized
ObservableEmitter
-
tryOnError
boolean tryOnError(@NonNull @NonNull java.lang.Throwable t)
Attempts to emit the specifiedThrowable
error 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
Emitter.onError(Throwable)
, theRxjavaPlugins.onError
is not called if the error could not be delivered.History: 2.1.1 - experimental
- Parameters:
t
- theThrowable
error to signal if possible- Returns:
- true if successful, false if the downstream is not able to accept further events
- Since:
- 2.2
-
-