| 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.completable; |
| 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 |
public final class CompletableSubscribeOn extends Completable { |
| 23 |
final CompletableSource source; |
| 24 |
|
| 25 |
final Scheduler scheduler; |
| 26 |
|
| 27 |
public CompletableSubscribeOn(CompletableSource source, Scheduler scheduler) { |
| 28 |
this.source = source; |
| 29 |
this.scheduler = scheduler; |
| 30 |
} |
| 31 |
|
| 32 |
@Override |
| 33 |
protected void subscribeActual(final CompletableObserver observer) { |
| 34 |
|
| 35 |
final SubscribeOnObserver parent = new SubscribeOnObserver(observer, source); |
| 36 |
observer.onSubscribe(parent); |
| 37 |
|
| 38 |
Disposable f = scheduler.scheduleDirect(parent); |
| 39 |
|
| 40 |
parent.task.replace(f); |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
static final class SubscribeOnObserver |
| 45 |
extends AtomicReference<Disposable> |
| 46 |
implements CompletableObserver, Disposable, Runnable { |
| 47 |
|
| 48 |
private static final long serialVersionUID = 7000911171163930287L; |
| 49 |
|
| 50 |
final CompletableObserver downstream; |
| 51 |
|
| 52 |
final SequentialDisposable task; |
| 53 |
|
| 54 |
final CompletableSource source; |
| 55 |
|
| 56 |
SubscribeOnObserver(CompletableObserver actual, CompletableSource source) { |
| 57 |
this.downstream = actual; |
| 58 |
this.source = source; |
| 59 |
this.task = new SequentialDisposable(); |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public void run() { |
| 64 |
source.subscribe(this); |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public void onSubscribe(Disposable d) { |
| 69 |
DisposableHelper.setOnce(this, d); |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public void onError(Throwable e) { |
| 74 |
downstream.onError(e); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public void onComplete() { |
| 79 |
downstream.onComplete(); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public void dispose() { |
| 84 |
DisposableHelper.dispose(this); |
| 85 |
task.dispose(); |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public boolean isDisposed() { |
| 90 |
return DisposableHelper.isDisposed(get()); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
} |