rx.observers.SerializedSubscriber Maven / Gradle / Ivy
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.observers;
import rx.*;
/**
* Enforces single-threaded, serialized, ordered execution of {@link #onNext}, {@link #onCompleted}, and
* {@link #onError}.
*
* When multiple threads are emitting and/or notifying they will be serialized by:
*
* - Allowing only one thread at a time to emit
* - Adding notifications to a queue if another thread is already emitting
* - Not holding any locks or blocking any threads while emitting
*
*
* @param
* the type of items expected to be emitted to the {@code Subscriber}
*/
public class SerializedSubscriber extends Subscriber {
private final Observer s;
public SerializedSubscriber(Subscriber s) {
this(s, true);
}
/**
* Constructor for wrapping and serializing a subscriber optionally sharing the same underlying subscription
* list.
*
* @param s
* the subscriber to wrap and serialize
* @param shareSubscriptions
* if {@code true}, the same subscription list is shared between this subscriber and {@code s}.
* @since 1.0.7
*/
public SerializedSubscriber(Subscriber s, boolean shareSubscriptions) {
super(s, shareSubscriptions);
this.s = new SerializedObserver(s);
}
/**
* Notifies the Subscriber that the {@code Observable} has finished sending push-based notifications.
*
* The {@code Observable} will not call this method if it calls {@link #onError}.
*/
@Override
public void onCompleted() {
s.onCompleted();
}
/**
* Notifies the Subscriber that the {@code Observable} has experienced an error condition.
*
* If the {@code Observable} calls this method, it will not thereafter call {@link #onNext} or
* {@link #onCompleted}.
*
* @param e
* the exception encountered by the Observable
*/
@Override
public void onError(Throwable e) {
s.onError(e);
}
/**
* Provides the Subscriber with a new item to observe.
*
* The {@code Observable} may call this method 0 or more times.
*
* The {@code Observable} will not call this method again after it calls either {@link #onCompleted} or
* {@link #onError}.
*
* @param t
* the item emitted by the Observable
*/
@Override
public void onNext(T t) {
s.onNext(t);
}
}