Class SafeSubscriber<T>
- java.lang.Object
-
- rx.Subscriber<T>
-
- rx.observers.SafeSubscriber<T>
-
- Type Parameters:
T- the type of item expected by theSubscriber
- All Implemented Interfaces:
Observer<T>,Subscription
public class SafeSubscriber<T> extends Subscriber<T>
SafeSubscriberis a wrapper aroundSubscriberthat ensures that theSubscribercomplies with the Observable contract.The following is taken from the Rx Design Guidelines document:
Messages sent to instances of the
IObserverinterface follow the following grammar:OnNext* (OnCompleted | OnError)?This grammar allows observable sequences to send any amount (0 or more) of
OnNextmessages 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
onErrororonCompleted. - Ensures that once an
onCompletedoronErroris performed, no further calls can be executed - If
unsubscribeis called, the upstreamObservableis notified and the event delivery will be stopped in a best effort manner (i.e., further onXXX calls may still slip through). - When
onErrororonCompletedoccur, unsubscribes from theObservable(if executing asynchronously).
SafeSubscriberwill not synchronizeonNextexecution. UseSerializedSubscriberto do that.
-
-
Field Summary
Fields Modifier and Type Field Description private Subscriber<? super T>actual(package private) booleandone
-
Constructor Summary
Constructors Constructor Description SafeSubscriber(Subscriber<? super T> actual)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void_onError(java.lang.Throwable e)The logic foronErrorwithout theisFinishedcheck so it can be called from withinonCompleted.Subscriber<? super T>getActual()Returns theSubscriberunderlying thisSafeSubscriber.voidonCompleted()Notifies the Subscriber that theObservablehas finished sending push-based notifications.voidonError(java.lang.Throwable e)Notifies the Subscriber that theObservablehas experienced an error condition.voidonNext(T args)Provides the Subscriber with a new item to observe.-
Methods inherited from class rx.Subscriber
add, isUnsubscribed, onStart, request, setProducer, unsubscribe
-
-
-
-
Field Detail
-
actual
private final Subscriber<? super T> actual
-
done
boolean done
-
-
Constructor Detail
-
SafeSubscriber
public SafeSubscriber(Subscriber<? super T> actual)
-
-
Method Detail
-
onCompleted
public void onCompleted()
Notifies the Subscriber that theObservablehas finished sending push-based notifications.The
Observablewill not call this method if it callsonError(java.lang.Throwable).
-
onError
public void onError(java.lang.Throwable e)
Notifies the Subscriber that theObservablehas experienced an error condition.If the
Observablecalls this method, it will not thereafter callonNext(T)oronCompleted().- Parameters:
e- the exception encountered by the Observable
-
onNext
public void onNext(T args)
Provides the Subscriber with a new item to observe.The
Observablemay call this method 0 or more times.The
Observablewill not call this method again after it calls eitheronCompleted()oronError(java.lang.Throwable).- Parameters:
args- the item emitted by the Observable
-
_onError
protected void _onError(java.lang.Throwable e)
The logic foronErrorwithout theisFinishedcheck so it can be called from withinonCompleted.- See Also:
- the report of this bug
-
getActual
public Subscriber<? super T> getActual()
Returns theSubscriberunderlying thisSafeSubscriber.- Returns:
- the
Subscriberthat was used to create thisSafeSubscriber
-
-