All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rx.internal.operators.OperatorSubscribeOn Maven / Gradle / Ivy

The newest version!
/**
 * 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.internal.operators;

import rx.*;
import rx.Observable.OnSubscribe;
import rx.Scheduler.Worker;
import rx.functions.Action0;

/**
 * Subscribes Observers on the specified {@code Scheduler}.
 * 

* * * @param the value type of the actual source */ public final class OperatorSubscribeOn implements OnSubscribe { final Scheduler scheduler; final Observable source; final boolean requestOn; public OperatorSubscribeOn(Observable source, Scheduler scheduler, boolean requestOn) { this.scheduler = scheduler; this.source = source; this.requestOn = requestOn; } @Override public void call(final Subscriber subscriber) { final Worker inner = scheduler.createWorker(); SubscribeOnSubscriber parent = new SubscribeOnSubscriber(subscriber, requestOn, inner, source); subscriber.add(parent); subscriber.add(inner); inner.schedule(parent); } static final class SubscribeOnSubscriber extends Subscriber implements Action0 { final Subscriber actual; final boolean requestOn; final Worker worker; Observable source; Thread t; SubscribeOnSubscriber(Subscriber actual, boolean requestOn, Worker worker, Observable source) { this.actual = actual; this.requestOn = requestOn; this.worker = worker; this.source = source; } @Override public void onNext(T t) { actual.onNext(t); } @Override public void onError(Throwable e) { try { actual.onError(e); } finally { worker.unsubscribe(); } } @Override public void onCompleted() { try { actual.onCompleted(); } finally { worker.unsubscribe(); } } @Override public void call() { Observable src = source; source = null; t = Thread.currentThread(); src.unsafeSubscribe(this); } @Override public void setProducer(final Producer p) { actual.setProducer(new Producer() { @Override public void request(final long n) { if (t == Thread.currentThread() || !requestOn) { p.request(n); } else { worker.schedule(new Action0() { @Override public void call() { p.request(n); } }); } } }); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy