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