| 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 io.reactivex.rxjava3.core.*; |
| 17 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 18 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 19 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 20 |
|
| 21 |
public final class CompletableDisposeOn extends Completable { |
| 22 |
|
| 23 |
final CompletableSource source; |
| 24 |
|
| 25 |
final Scheduler scheduler; |
| 26 |
|
| 27 |
public CompletableDisposeOn(CompletableSource source, Scheduler scheduler) { |
| 28 |
this.source = source; |
| 29 |
this.scheduler = scheduler; |
| 30 |
} |
| 31 |
|
| 32 |
@Override |
| 33 |
protected void subscribeActual(final CompletableObserver observer) { |
| 34 |
source.subscribe(new DisposeOnObserver(observer, scheduler)); |
| 35 |
} |
| 36 |
|
| 37 |
static final class DisposeOnObserver implements CompletableObserver, Disposable, Runnable { |
| 38 |
final CompletableObserver downstream; |
| 39 |
|
| 40 |
final Scheduler scheduler; |
| 41 |
|
| 42 |
Disposable upstream; |
| 43 |
|
| 44 |
volatile boolean disposed; |
| 45 |
|
| 46 |
DisposeOnObserver(CompletableObserver observer, Scheduler scheduler) { |
| 47 |
this.downstream = observer; |
| 48 |
this.scheduler = scheduler; |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public void onComplete() { |
| 53 |
if (disposed) { |
| 54 |
return; |
| 55 |
} |
| 56 |
downstream.onComplete(); |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public void onError(Throwable e) { |
| 61 |
if (disposed) { |
| 62 |
RxJavaPlugins.onError(e); |
| 63 |
return; |
| 64 |
} |
| 65 |
downstream.onError(e); |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public void onSubscribe(final Disposable d) { |
| 70 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 71 |
this.upstream = d; |
| 72 |
|
| 73 |
downstream.onSubscribe(this); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public void dispose() { |
| 79 |
disposed = true; |
| 80 |
scheduler.scheduleDirect(this); |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public boolean isDisposed() { |
| 85 |
return disposed; |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public void run() { |
| 90 |
upstream.dispose(); |
| 91 |
upstream = DisposableHelper.DISPOSED; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
} |