| 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.maybe; |
| 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 |
/** |
| 23 |
* Signals the onSuccess, onError or onComplete events on a the specific scheduler. |
| 24 |
* |
| 25 |
* @param <T> the value type delivered |
| 26 |
*/ |
| 27 |
public final class MaybeObserveOn<T> extends AbstractMaybeWithUpstream<T, T> { |
| 28 |
|
| 29 |
final Scheduler scheduler; |
| 30 |
|
| 31 |
public MaybeObserveOn(MaybeSource<T> source, Scheduler scheduler) { |
| 32 |
super(source); |
| 33 |
this.scheduler = scheduler; |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
protected void subscribeActual(MaybeObserver<? super T> observer) { |
| 38 |
source.subscribe(new ObserveOnMaybeObserver<>(observer, scheduler)); |
| 39 |
} |
| 40 |
|
| 41 |
static final class ObserveOnMaybeObserver<T> |
| 42 |
extends AtomicReference<Disposable> |
| 43 |
implements MaybeObserver<T>, Disposable, Runnable { |
| 44 |
|
| 45 |
private static final long serialVersionUID = 8571289934935992137L; |
| 46 |
|
| 47 |
final MaybeObserver<? super T> downstream; |
| 48 |
|
| 49 |
final Scheduler scheduler; |
| 50 |
|
| 51 |
T value; |
| 52 |
Throwable error; |
| 53 |
|
| 54 |
ObserveOnMaybeObserver(MaybeObserver<? super T> actual, Scheduler scheduler) { |
| 55 |
this.downstream = actual; |
| 56 |
this.scheduler = scheduler; |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public void dispose() { |
| 61 |
DisposableHelper.dispose(this); |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public boolean isDisposed() { |
| 66 |
return DisposableHelper.isDisposed(get()); |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public void onSubscribe(Disposable d) { |
| 71 |
if (DisposableHelper.setOnce(this, d)) { |
| 72 |
downstream.onSubscribe(this); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public void onSuccess(T value) { |
| 78 |
this.value = value; |
| 79 |
DisposableHelper.replace(this, scheduler.scheduleDirect(this)); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public void onError(Throwable e) { |
| 84 |
this.error = e; |
| 85 |
DisposableHelper.replace(this, scheduler.scheduleDirect(this)); |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public void onComplete() { |
| 90 |
DisposableHelper.replace(this, scheduler.scheduleDirect(this)); |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public void run() { |
| 95 |
Throwable ex = error; |
| 96 |
if (ex != null) { |
| 97 |
error = null; |
| 98 |
downstream.onError(ex); |
| 99 |
} else { |
| 100 |
T v = value; |
| 101 |
if (v != null) { |
| 102 |
value = null; |
| 103 |
downstream.onSuccess(v); |
| 104 |
} else { |
| 105 |
downstream.onComplete(); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |