| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.internal.operators.observable; |
| 15 |
|
| 16 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 20 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 21 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 22 |
|
| 23 |
public final class ObservableUnsubscribeOn<T> extends AbstractObservableWithUpstream<T, T> { |
| 24 |
final Scheduler scheduler; |
| 25 |
public ObservableUnsubscribeOn(ObservableSource<T> source, Scheduler scheduler) { |
| 26 |
super(source); |
| 27 |
this.scheduler = scheduler; |
| 28 |
} |
| 29 |
|
| 30 |
@Override |
| 31 |
public void subscribeActual(Observer<? super T> t) { |
| 32 |
source.subscribe(new UnsubscribeObserver<>(t, scheduler)); |
| 33 |
} |
| 34 |
|
| 35 |
static final class UnsubscribeObserver<T> extends AtomicBoolean implements Observer<T>, Disposable { |
| 36 |
|
| 37 |
private static final long serialVersionUID = 1015244841293359600L; |
| 38 |
|
| 39 |
final Observer<? super T> downstream; |
| 40 |
final Scheduler scheduler; |
| 41 |
|
| 42 |
Disposable upstream; |
| 43 |
|
| 44 |
UnsubscribeObserver(Observer<? super T> actual, Scheduler scheduler) { |
| 45 |
this.downstream = actual; |
| 46 |
this.scheduler = scheduler; |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public void onSubscribe(Disposable d) { |
| 51 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 52 |
this.upstream = d; |
| 53 |
downstream.onSubscribe(this); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public void onNext(T t) { |
| 59 |
if (!get()) { |
| 60 |
downstream.onNext(t); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public void onError(Throwable t) { |
| 66 |
if (get()) { |
| 67 |
RxJavaPlugins.onError(t); |
| 68 |
return; |
| 69 |
} |
| 70 |
downstream.onError(t); |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public void onComplete() { |
| 75 |
if (!get()) { |
| 76 |
downstream.onComplete(); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public void dispose() { |
| 82 |
if (compareAndSet(false, true)) { |
| 83 |
scheduler.scheduleDirect(new DisposeTask()); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public boolean isDisposed() { |
| 89 |
return get(); |
| 90 |
} |
| 91 |
|
| 92 |
final class DisposeTask implements Runnable { |
| 93 |
@Override |
| 94 |
public void run() { |
| 95 |
upstream.dispose(); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |