Class SafeSubscriber<T>
- Type Parameters:
T
- the type of item expected by theSubscriber
- All Implemented Interfaces:
Observer<T>
,Subscription
SafeSubscriber
is a wrapper around Subscriber
that ensures that the Subscriber
complies with the Observable contract.
The following is taken from the Rx Design Guidelines document:
Messages sent to instances of the
IObserver
interface follow the following grammar:
OnNext* (OnCompleted | OnError)?
This grammar allows observable sequences to send any amount (0 or more) of
OnNext
messages to the subscriber, optionally followed by a single success (OnCompleted
) or failure (OnError
) message.The single message indicating that an observable sequence has finished ensures that consumers of the observable sequence can deterministically establish that it is safe to perform cleanup operations.
A single failure further ensures that abort semantics can be maintained for operators that work on multiple observable sequences (see paragraph 6.6).
This wrapper does the following:
- Allows only single execution of either
onError
oronCompleted
. - Ensures that once an
onCompleted
oronError
is performed, no further calls can be executed - If
unsubscribe
is called, the upstreamObservable
is notified and the event delivery will be stopped in a best effort manner (i.e., further onXXX calls may still slip through). - When
onError
oronCompleted
occur, unsubscribes from theObservable
(if executing asynchronously).
SafeSubscriber
will not synchronize onNext
execution. Use SerializedSubscriber
to do
that.-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
The logic foronError
without theisFinished
check so it can be called from withinonCompleted
.Subscriber
<? super T> Returns theSubscriber
underlying thisSafeSubscriber
.void
Notifies the Subscriber that theObservable
has finished sending push-based notifications.void
Notifies the Subscriber that theObservable
has experienced an error condition.void
Provides the Subscriber with a new item to observe.Methods inherited from class rx.Subscriber
add, isUnsubscribed, onStart, request, setProducer, unsubscribe
-
Field Details
-
actual
-
done
boolean done
-
-
Constructor Details
-
SafeSubscriber
-
-
Method Details
-
onCompleted
public void onCompleted()Notifies the Subscriber that theObservable
has finished sending push-based notifications.The
Observable
will not call this method if it callsonError(java.lang.Throwable)
. -
onError
Notifies the Subscriber that theObservable
has experienced an error condition.If the
Observable
calls this method, it will not thereafter callonNext(T)
oronCompleted()
.- Parameters:
e
- the exception encountered by the Observable
-
onNext
Provides the Subscriber with a new item to observe.The
Observable
may call this method 0 or more times.The
Observable
will not call this method again after it calls eitheronCompleted()
oronError(java.lang.Throwable)
.- Parameters:
args
- the item emitted by the Observable
-
_onError
The logic foronError
without theisFinished
check so it can be called from withinonCompleted
.- See Also:
-
getActual
Returns theSubscriber
underlying thisSafeSubscriber
.- Returns:
- the
Subscriber
that was used to create thisSafeSubscriber
-