| 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.single; |
| 15 |
|
| 16 |
import java.util.concurrent.atomic.AtomicReference; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 20 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 21 |
|
| 22 |
public final class SingleObserveOn<T> extends Single<T> { |
| 23 |
|
| 24 |
final SingleSource<T> source; |
| 25 |
|
| 26 |
final Scheduler scheduler; |
| 27 |
|
| 28 |
public SingleObserveOn(SingleSource<T> source, Scheduler scheduler) { |
| 29 |
this.source = source; |
| 30 |
this.scheduler = scheduler; |
| 31 |
} |
| 32 |
|
| 33 |
@Override |
| 34 |
protected void subscribeActual(final SingleObserver<? super T> observer) { |
| 35 |
source.subscribe(new ObserveOnSingleObserver<>(observer, scheduler)); |
| 36 |
} |
| 37 |
|
| 38 |
static final class ObserveOnSingleObserver<T> extends AtomicReference<Disposable> |
| 39 |
implements SingleObserver<T>, Disposable, Runnable { |
| 40 |
private static final long serialVersionUID = 3528003840217436037L; |
| 41 |
|
| 42 |
final SingleObserver<? super T> downstream; |
| 43 |
|
| 44 |
final Scheduler scheduler; |
| 45 |
|
| 46 |
T value; |
| 47 |
Throwable error; |
| 48 |
|
| 49 |
ObserveOnSingleObserver(SingleObserver<? super T> actual, Scheduler scheduler) { |
| 50 |
this.downstream = actual; |
| 51 |
this.scheduler = scheduler; |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public void onSubscribe(Disposable d) { |
| 56 |
if (DisposableHelper.setOnce(this, d)) { |
| 57 |
downstream.onSubscribe(this); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public void onSuccess(T value) { |
| 63 |
this.value = value; |
| 64 |
Disposable d = scheduler.scheduleDirect(this); |
| 65 |
DisposableHelper.replace(this, d); |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public void onError(Throwable e) { |
| 70 |
this.error = e; |
| 71 |
Disposable d = scheduler.scheduleDirect(this); |
| 72 |
DisposableHelper.replace(this, d); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public void run() { |
| 77 |
Throwable ex = error; |
| 78 |
if (ex != null) { |
| 79 |
downstream.onError(ex); |
| 80 |
} else { |
| 81 |
downstream.onSuccess(value); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public void dispose() { |
| 87 |
DisposableHelper.dispose(this); |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public boolean isDisposed() { |
| 92 |
return DisposableHelper.isDisposed(get()); |
| 93 |
} |
| 94 |
} |
| 95 |
} |